feat(app): backwards-compatible options `asyncData` support (#286)

This commit is contained in:
Daniel Roe 2021-06-30 11:53:10 +01:00 committed by GitHub
parent 79523399d2
commit 53f6756178
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 2 deletions

View File

@ -0,0 +1,13 @@
<template>
<div>
<div>
<nuxt-page />
</div>
<nuxt-link to="/">
Home
</nuxt-link>
<nuxt-link to="/options">
Options
</nuxt-link>
</div>
</template>

View File

@ -0,0 +1,31 @@
<template>
<div>
About
{{ path }}
<button @click="path = 'newpath'">
Update path
</button>
</div>
</template>
<script lang="ts">
import { defineNuxtComponent } from '@nuxt/app'
import { ref } from 'vue'
export default defineNuxtComponent({
fetchKey: 'custom',
asyncData ({ route }) {
return {
path: route.path
}
},
setup () {
// This will get overwritten with asyncData
const path = ref('original path')
return {
path
}
}
})
</script>

View File

@ -1,4 +1,9 @@
import { toRefs } from '@vue/reactivity'
import { ComponentInternalInstance, DefineComponent, defineComponent, getCurrentInstance } from 'vue'
import { useRoute } from 'vue-router'
import type { LegacyContext } from '../legacy'
import { useNuxt } from '../nuxt'
import { asyncData } from './asyncData'
export const NuxtComponentIndicator = '__nuxt_component'
export const NuxtComponentPendingPromises = '_pendingPromises'
@ -22,11 +27,21 @@ export function enqueueNuxtComponent (p: Promise<void>) {
vm[NuxtComponentPendingPromises].push(p)
}
async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<string, any>>, fn: (context: LegacyContext) => Promise<Record<string, any>>) {
const nuxt = useNuxt()
const route = useRoute()
const vm = getCurrentNuxtComponentInstance()
const { fetchKey } = vm.proxy.$options
const key = typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey || route.fullPath
const { data } = await asyncData(`options:asyncdata:${key}`, () => fn(nuxt._legacyContext))
Object.assign(await res, toRefs(data))
}
export const defineNuxtComponent: typeof defineComponent =
function defineNuxtComponent (options: any): any {
const { setup } = options
if (!setup) {
if (!setup && !options.asyncData) {
return {
[NuxtComponentIndicator]: true,
...options
@ -40,7 +55,11 @@ export const defineNuxtComponent: typeof defineComponent =
const vm = getCurrentNuxtComponentInstance()
let promises = vm[NuxtComponentPendingPromises] = vm[NuxtComponentPendingPromises] || []
const res = setup(props, ctx)
const res = setup?.(props, ctx) || {}
if (options.asyncData) {
promises.push(runLegacyAsyncData(res, options.asyncData))
}
if (!promises.length && !(res instanceof Promise)) {
return res