-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
reqRef
and ssrReqRef
for refs to be reset per-request
* ...and update docs more extensively to identify potential footguns
- Loading branch information
Showing
23 changed files
with
221 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: Gotchas | ||
description: '@nuxtjs/composition-api provides a way to use the Vue 3 Composition API with Nuxt-specific features.' | ||
category: Getting started | ||
fullscreen: True | ||
position: 3 | ||
version: 0.133 | ||
--- | ||
|
||
There are a couple of points that you should be aware of when using `@nuxtjs/composition-api`. | ||
|
||
## **'Keyed' functions** | ||
|
||
A number of helper functions use a key to pass JSON-encoded information from server to client (currently `shallowSsrRef`, `ssrPromise`, `ssrRef` and `useAsync`). | ||
|
||
In order to make that possible, under the hood this library adds a key based on line number within your code. | ||
|
||
If you want to use these functions within global composables, make sure to set a unique key based on each calling of the function - for example, you might key it to the route path. (Each function takes an optional second parameter that is this key.) | ||
|
||
If you don't provide a unique key, this is the consequence: | ||
```ts | ||
function useMyFeature() { | ||
// Only one unique key is generated, no matter | ||
// how many times this function is called. | ||
const feature = ssrRef('') | ||
|
||
return feature | ||
} | ||
|
||
const a = useMyFeature() | ||
const b = useMyFeature() | ||
|
||
b.value = 'changed' | ||
// On client-side, a's value will also be initialised to 'changed' | ||
``` | ||
|
||
Here is how you might implement it by setting a key. | ||
```ts | ||
function useMyFeature(path: string) { | ||
const content = useAsync( | ||
() => fetch(`https://api.com/slug/${path}`).then(r => r.json()), | ||
path, | ||
) | ||
|
||
return { | ||
content | ||
} | ||
} | ||
|
||
export default useMyFeature | ||
``` | ||
|
||
## **Shared server state** | ||
|
||
If you are declaring refs in the global state of your application - such as within plugins or in state/store files (for example, as a replacement for Vuex) - you should be aware that these refs are persisted across requests when your site is in production mode. | ||
|
||
You should take especial care with declaring refs in this way. | ||
|
||
<alert type="info"> | ||
|
||
If you do need to reinitialise a ref on each request, you can use [the `reqRef` helper](/helpers/reqRef). | ||
|
||
</alert> | ||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: reqRef, reqSsrRef | ||
description: '@nuxtjs/composition-api provides a way to use the Vue 3 Composition API with Nuxt-specific features.' | ||
category: Helpers | ||
fullscreen: True | ||
version: 0.133 | ||
position: 12 | ||
--- | ||
|
||
`reqRef` declares a normal `ref` with one key difference. It resets the value of this ref on each request. You can find out [more information here](/gotchas#shared-server-state). | ||
|
||
<alert>You should take especial care because of the danger of shared state when using refs in this way.</alert> | ||
|
||
|
||
## Example | ||
|
||
```ts[~/state/sampleModule.js] | ||
import { reqRef } from '@nuxtjs/composition-api' | ||
export const user = reqRef(null) | ||
export const fetchUser = async () => { | ||
const r = await fetch('https://api.com/users) | ||
user.value = await r.json() | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ref, Ref } from '@vue/composition-api' | ||
|
||
import { sanitise, ssrRef } from './ssr-ref' | ||
|
||
export const reqRefs = new Set<() => void>() | ||
|
||
export const reqRef = <T>(initialValue: T): Ref<T> => { | ||
const _ref = ref(initialValue) | ||
|
||
if (process.server) reqRefs.add(() => (_ref.value = initialValue as any)) | ||
|
||
return _ref as Ref<T> | ||
} | ||
|
||
export const reqSsrRef = <T>(initialValue: T, key?: string) => { | ||
const _ref = ssrRef(initialValue, key) | ||
|
||
if (process.server) | ||
reqRefs.add(() => { | ||
_ref.value = | ||
initialValue instanceof Function | ||
? sanitise(initialValue()) | ||
: initialValue | ||
}) | ||
|
||
return _ref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b6f327c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: