mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
refactor(nuxt): add strategy
and deprecate lazy
option
This commit is contained in:
parent
54887aa05b
commit
a3d96714d4
@ -38,7 +38,9 @@ export interface AsyncDataOptions<
|
||||
DefaultT = null,
|
||||
> {
|
||||
server?: boolean
|
||||
/** @deprecated Use strategy: 'lazy' */
|
||||
lazy?: boolean
|
||||
strategy?: 'lazy' | 'parallel' | 'blocking'
|
||||
default?: () => DefaultT | Ref<DefaultT>
|
||||
transform?: _Transform<ResT, DataT>
|
||||
pick?: PickKeys
|
||||
@ -135,7 +137,7 @@ export function useAsyncData<
|
||||
options.server = options.server ?? true
|
||||
options.default = options.default ?? (getDefault as () => DefaultT)
|
||||
|
||||
options.lazy = options.lazy ?? false
|
||||
options.strategy = options.strategy || (options.lazy ? 'lazy' : 'blocking')
|
||||
options.immediate = options.immediate ?? true
|
||||
|
||||
// Setup nuxt instance payload
|
||||
@ -251,7 +253,7 @@ export function useAsyncData<
|
||||
// 1. Hydration (server: true): no fetch
|
||||
asyncData.pending.value = false
|
||||
asyncData.status.value = asyncData.error.value ? 'error' : 'success'
|
||||
} else if (instance && ((nuxt.payload.serverRendered && nuxt.isHydrating) || options.lazy) && options.immediate) {
|
||||
} else if (instance && ((nuxt.payload.serverRendered && nuxt.isHydrating) || options.strategy === 'lazy') && options.immediate) {
|
||||
// 2. Initial load (server: false): fetch on mounted
|
||||
// 3. Initial load or navigation (lazy: true): fetch on mounted
|
||||
instance._nuxtOnBeforeMountCbs.push(initialFetch)
|
||||
@ -332,7 +334,7 @@ export function useLazyAsyncData<
|
||||
if (typeof args[0] !== 'string') { args.unshift(autoKey) }
|
||||
const [key, handler, options] = args as [string, (ctx?: NuxtApp) => Promise<ResT>, AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>]
|
||||
// @ts-expect-error we pass an extra argument to prevent a key being injected
|
||||
return useAsyncData(key, handler, { ...options, lazy: true }, null)
|
||||
return useAsyncData(key, handler, { ...options, strategy: 'lazy' }, null)
|
||||
}
|
||||
|
||||
export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | null> } {
|
||||
|
@ -99,6 +99,7 @@ export function useFetch<
|
||||
pick,
|
||||
watch,
|
||||
immediate,
|
||||
strategy,
|
||||
...fetchOptions
|
||||
} = opts
|
||||
|
||||
@ -110,6 +111,7 @@ export function useFetch<
|
||||
const _asyncDataOptions: AsyncDataOptions<_ResT, DataT, PickKeys, DefaultT> = {
|
||||
server,
|
||||
lazy,
|
||||
strategy,
|
||||
default: defaultFn,
|
||||
transform,
|
||||
pick,
|
||||
@ -180,7 +182,7 @@ export function useLazyFetch<
|
||||
|
||||
return useFetch<ResT, ErrorT, ReqT, Method, _ResT, DataT, PickKeys, DefaultT>(request, {
|
||||
...opts,
|
||||
lazy: true
|
||||
strategy: 'lazy'
|
||||
},
|
||||
// @ts-expect-error we pass an extra argument with the resolved auto-key to prevent another from being injected
|
||||
autoKey)
|
||||
|
Loading…
Reference in New Issue
Block a user