fix(nuxt)!: remove initialCache option (#8885)

This commit is contained in:
pooya parsa 2022-11-10 14:27:59 +01:00 committed by GitHub
parent 83b5c09090
commit 52c2bffa76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 15 deletions

View File

@ -25,7 +25,6 @@ type AsyncDataOptions<DataT> = {
transform?: (input: DataT) => DataT transform?: (input: DataT) => DataT
pick?: string[] pick?: string[]
watch?: WatchSource[] watch?: WatchSource[]
initialCache?: boolean
immediate?: boolean immediate?: boolean
} }
@ -55,7 +54,6 @@ type AsyncData<DataT, ErrorT> = {
* _transform_: a function that can be used to alter `handler` function result after resolving * _transform_: a function that can be used to alter `handler` function result after resolving
* _pick_: only pick specified keys in this array from the `handler` function result * _pick_: only pick specified keys in this array from the `handler` function result
* _watch_: watch reactive sources to auto-refresh * _watch_: watch reactive sources to auto-refresh
* _initialCache_: When set to `false`, will skip payload cache for initial fetch. (defaults to `true`)
* _immediate_: When set to `false`, will prevent the request from firing immediately. (defaults to `true`) * _immediate_: When set to `false`, will prevent the request from firing immediately. (defaults to `true`)
Under the hood, `lazy: false` uses `<Suspense>` to block the loading of the route before the data has been fetched. Consider using `lazy: true` and implementing a loading state instead for a snappier user experience. Under the hood, `lazy: false` uses `<Suspense>` to block the loading of the route before the data has been fetched. Consider using `lazy: true` and implementing a loading state instead for a snappier user experience.

View File

@ -27,7 +27,6 @@ type UseFetchOptions = {
transform?: (input: DataT) => DataT transform?: (input: DataT) => DataT
pick?: string[] pick?: string[]
watch?: WatchSource[] watch?: WatchSource[]
initialCache?: boolean
} }
type AsyncData<DataT> = { type AsyncData<DataT> = {
@ -60,7 +59,6 @@ All fetch options can be given a `computed` or `ref` value. These will be watche
* `default`: A factory function to set the default value of the data, before the async function resolves - particularly useful with the `lazy: true` option. * `default`: A factory function to set the default value of the data, before the async function resolves - particularly useful with the `lazy: true` option.
* `pick`: Only pick specified keys in this array from the `handler` function result. * `pick`: Only pick specified keys in this array from the `handler` function result.
* `watch`: watch reactive sources to auto-refresh. * `watch`: watch reactive sources to auto-refresh.
* `initialCache`: When set to `false`, will skip payload cache for initial fetch (defaults to `true`).
* `transform`: A function that can be used to alter `handler` function result after resolving. * `transform`: A function that can be used to alter `handler` function result after resolving.
* `immediate`: When set to `false`, will prevent the request from firing immediately. (defaults to `true`) * `immediate`: When set to `false`, will prevent the request from firing immediately. (defaults to `true`)

View File

@ -29,7 +29,6 @@ export interface AsyncDataOptions<
transform?: Transform transform?: Transform
pick?: PickKeys pick?: PickKeys
watch?: MultiWatchSources watch?: MultiWatchSources
initialCache?: boolean
immediate?: boolean immediate?: boolean
} }
@ -102,19 +101,19 @@ export function useAsyncData<
console.warn('[useAsyncData] `defer` has been renamed to `lazy`. Support for `defer` will be removed in RC.') console.warn('[useAsyncData] `defer` has been renamed to `lazy`. Support for `defer` will be removed in RC.')
} }
options.lazy = options.lazy ?? (options as any).defer ?? false options.lazy = options.lazy ?? (options as any).defer ?? false
options.initialCache = options.initialCache ?? true
options.immediate = options.immediate ?? true options.immediate = options.immediate ?? true
// Setup nuxt instance payload // Setup nuxt instance payload
const nuxt = useNuxtApp() const nuxt = useNuxtApp()
const useInitialCache = () => (nuxt.isHydrating || options.initialCache) && nuxt.payload.data[key] !== undefined const getCachedData = () => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]
const hasCachedData = () => getCachedData() !== undefined
// Create or use a shared asyncData entity // Create or use a shared asyncData entity
if (!nuxt._asyncData[key]) { if (!nuxt._asyncData[key]) {
nuxt._asyncData[key] = { nuxt._asyncData[key] = {
data: ref(useInitialCache() ? nuxt.payload.data[key] : options.default?.() ?? null), data: ref(getCachedData() ?? options.default?.() ?? null),
pending: ref(!useInitialCache()), pending: ref(!hasCachedData()),
error: ref(nuxt.payload._errors[key] ? createError(nuxt.payload._errors[key]) : null) error: ref(nuxt.payload._errors[key] ? createError(nuxt.payload._errors[key]) : null)
} }
} }
@ -130,8 +129,8 @@ export function useAsyncData<
(nuxt._asyncDataPromises[key] as any).cancelled = true (nuxt._asyncDataPromises[key] as any).cancelled = true
} }
// Avoid fetching same key that is already fetched // Avoid fetching same key that is already fetched
if (opts._initial && useInitialCache()) { if (opts._initial && hasCachedData()) {
return nuxt.payload.data[key] return getCachedData()
} }
asyncData.pending.value = true asyncData.pending.value = true
// TODO: Cancel previous promise // TODO: Cancel previous promise
@ -204,7 +203,7 @@ export function useAsyncData<
} }
} }
if (fetchOnServer && nuxt.isHydrating && key in nuxt.payload.data) { if (fetchOnServer && nuxt.isHydrating && hasCachedData()) {
// 1. Hydration (server: true): no fetch // 1. Hydration (server: true): no fetch
asyncData.pending.value = false asyncData.pending.value = false
} else if (instance && ((nuxt.payload.serverRendered && nuxt.isHydrating) || options.lazy) && options.immediate) { } else if (instance && ((nuxt.payload.serverRendered && nuxt.isHydrating) || options.lazy) && options.immediate) {

View File

@ -68,7 +68,6 @@ export function useFetch<
transform, transform,
pick, pick,
watch, watch,
initialCache,
immediate, immediate,
...fetchOptions ...fetchOptions
} = opts } = opts
@ -84,7 +83,6 @@ export function useFetch<
default: defaultFn, default: defaultFn,
transform, transform,
pick, pick,
initialCache,
immediate, immediate,
watch: [ watch: [
_fetchOptions, _fetchOptions,

View File

@ -89,6 +89,9 @@ interface _NuxtApp {
} | null } | null
[key: string]: any [key: string]: any
} }
static: {
data: Record<string, any>
}
provide: (name: string, value: any) => void provide: (name: string, value: any) => void
} }
@ -118,6 +121,9 @@ export function createNuxtApp (options: CreateOptions) {
_errors: {}, _errors: {},
...(process.client ? window.__NUXT__ : { serverRendered: true }) ...(process.client ? window.__NUXT__ : { serverRendered: true })
}), }),
static: {
data: {}
},
isHydrating: process.client, isHydrating: process.client,
deferHydration () { deferHydration () {
if (!nuxtApp.isHydrating) { return () => {} } if (!nuxtApp.isHydrating) { return () => {} }

View File

@ -20,6 +20,6 @@ export default defineNuxtPlugin((nuxtApp) => {
if (to.path === from.path) { return } if (to.path === from.path) { return }
const payload = await loadPayload(to.path) const payload = await loadPayload(to.path)
if (!payload) { return } if (!payload) { return }
Object.assign(nuxtApp.payload.data, payload.data) Object.assign(nuxtApp.static.data, payload.data)
}) })
}) })