mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 09:33:54 +00:00
fix(nuxt): key distinct pages differently for legacy asyncData (#21263)
This commit is contained in:
parent
8453feedcb
commit
ec72066f91
@ -13,8 +13,9 @@ async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<str
|
|||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const vm = getCurrentInstance()!
|
const vm = getCurrentInstance()!
|
||||||
const { fetchKey } = vm.proxy!.$options
|
const { fetchKey, _fetchKeyBase } = vm.proxy!.$options
|
||||||
const key = typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey || route.fullPath
|
const key = (typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey) ||
|
||||||
|
([_fetchKeyBase, route.fullPath, route.matched.findIndex(r => Object.values(r.components || {}).includes(vm.type))].join(':'))
|
||||||
const { data, error } = await useAsyncData(`options:asyncdata:${key}`, () => nuxtApp.runWithContext(() => fn(nuxtApp)))
|
const { data, error } = await useAsyncData(`options:asyncdata:${key}`, () => nuxtApp.runWithContext(() => fn(nuxtApp)))
|
||||||
if (error.value) {
|
if (error.value) {
|
||||||
throw createError(error.value)
|
throw createError(error.value)
|
||||||
@ -27,7 +28,8 @@ async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<str
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const defineNuxtComponent: typeof defineComponent =
|
export const defineNuxtComponent: typeof defineComponent =
|
||||||
function defineNuxtComponent (options: any): any {
|
function defineNuxtComponent (...args: any[]): any {
|
||||||
|
const [options, key] = args
|
||||||
const { setup } = options
|
const { setup } = options
|
||||||
|
|
||||||
// Avoid wrapping if no options api is used
|
// Avoid wrapping if no options api is used
|
||||||
@ -40,6 +42,7 @@ export const defineNuxtComponent: typeof defineComponent =
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
[NuxtComponentIndicator]: true,
|
[NuxtComponentIndicator]: true,
|
||||||
|
_fetchKeyBase: key,
|
||||||
...options,
|
...options,
|
||||||
setup (props, ctx) {
|
setup (props, ctx) {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
|
@ -146,6 +146,7 @@ export default defineUntypedSchema({
|
|||||||
*/
|
*/
|
||||||
keyedComposables: {
|
keyedComposables: {
|
||||||
$resolve: (val) => [
|
$resolve: (val) => [
|
||||||
|
{ name: 'defineNuxtComponent', argumentLength: 2 },
|
||||||
{ name: 'useState', argumentLength: 2 },
|
{ name: 'useState', argumentLength: 2 },
|
||||||
{ name: 'useFetch', argumentLength: 3 },
|
{ name: 'useFetch', argumentLength: 3 },
|
||||||
{ name: 'useAsyncData', argumentLength: 3 },
|
{ name: 'useAsyncData', argumentLength: 3 },
|
||||||
|
@ -578,8 +578,27 @@ describe('legacy async data', () => {
|
|||||||
it('should work with defineNuxtComponent', async () => {
|
it('should work with defineNuxtComponent', async () => {
|
||||||
const html = await $fetch('/legacy/async-data')
|
const html = await $fetch('/legacy/async-data')
|
||||||
expect(html).toContain('<div>Hello API</div>')
|
expect(html).toContain('<div>Hello API</div>')
|
||||||
|
expect(html).toContain('<div>fooChild</div>')
|
||||||
|
expect(html).toContain('<div>fooParent</div>')
|
||||||
const { script } = parseData(html)
|
const { script } = parseData(html)
|
||||||
expect(script.data['options:asyncdata:/legacy/async-data'].hello).toEqual('Hello API')
|
expect(script.data['options:asyncdata:hello'].hello).toBe('Hello API')
|
||||||
|
expect(Object.values(script.data)).toMatchInlineSnapshot(`
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"baz": "qux",
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"hello": "Hello API",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fooParent": "fooParent",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fooChild": "fooChild",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
{{ hello }}
|
<div>{{ hello }}</div>
|
||||||
|
<NuxtPage />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
export default defineNuxtComponent({
|
export default defineNuxtComponent({
|
||||||
|
fetchKey: () => 'hello',
|
||||||
async setup () {
|
async setup () {
|
||||||
await nextTick()
|
await nextTick()
|
||||||
useRuntimeConfig()
|
useRuntimeConfig()
|
||||||
|
16
test/fixtures/basic/pages/legacy/async-data/index.vue
vendored
Normal file
16
test/fixtures/basic/pages/legacy/async-data/index.vue
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div>{{ fooParent }}</div>
|
||||||
|
<NuxtPage />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default defineNuxtComponent({
|
||||||
|
asyncData () {
|
||||||
|
return {
|
||||||
|
fooParent: 'fooParent'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
13
test/fixtures/basic/pages/legacy/async-data/index/index.vue
vendored
Normal file
13
test/fixtures/basic/pages/legacy/async-data/index/index.vue
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<template>
|
||||||
|
<div>{{ fooChild }}</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default defineNuxtComponent({
|
||||||
|
asyncData () {
|
||||||
|
return {
|
||||||
|
fooChild: 'fooChild'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
Loading…
Reference in New Issue
Block a user