Several helper functions in @nuxtjs/composition-api—specifically shallowSsrRef, ssrPromise, ssrRef, and useAsync—use a key to pass JSON-encoded information from the server to the client. By default, the library generates a key based on the line number where the function is called.
If you use these functions inside a global composable, every call to that composable will share the same line number, causing them to share the same key. This results in state leakage where updating one instance affects all others. To prevent this, you must provide a unique key as the second optional parameter (e.g., using a route path) for every unique call.
// INCORRECT: Shared key due to same line number in global composable
function useMyFeature() {
const feature = ssrRef('')
return feature
}
const a = useMyFeature()
const b = useMyFeature()
b.value = 'changed'
// On client-side, a's value will also be initialized to 'changed'
// CORRECT: Providing a unique key (e.g., a path)
function useMyFeature(path: string) {
const content = useAsync(
() => fetch(`https://api.com/slug/${path}`).then(r => r.json()),
path // 'path' acts as the unique key
)
return {
content,
}
}