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