mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 21:58:19 +00:00
chore(nuxt): rename nuxt
-> nuxtApp
internally for consistency
This commit is contained in:
parent
9dfc0c2c31
commit
c5d5932f57
@ -209,21 +209,21 @@ export function useAsyncData<
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setup nuxt instance payload
|
// Setup nuxt instance payload
|
||||||
const nuxt = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
|
|
||||||
// When prerendering, share payload data automatically between requests
|
// When prerendering, share payload data automatically between requests
|
||||||
const handler = import.meta.client || !import.meta.prerender || !nuxt.ssrContext?._sharedPrerenderCache ? _handler : async () => {
|
const handler = import.meta.client || !import.meta.prerender || !nuxtApp.ssrContext?._sharedPrerenderCache ? _handler : async () => {
|
||||||
const value = await nuxt.ssrContext!._sharedPrerenderCache!.get(key)
|
const value = await nuxtApp.ssrContext!._sharedPrerenderCache!.get(key)
|
||||||
if (value) { return value as ResT }
|
if (value) { return value as ResT }
|
||||||
|
|
||||||
const promise = nuxt.runWithContext(_handler)
|
const promise = nuxtApp.runWithContext(_handler)
|
||||||
nuxt.ssrContext!._sharedPrerenderCache!.set(key, promise)
|
nuxtApp.ssrContext!._sharedPrerenderCache!.set(key, promise)
|
||||||
return promise
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to get default values
|
// Used to get default values
|
||||||
const getDefault = () => null
|
const getDefault = () => null
|
||||||
const getDefaultCachedData = () => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]
|
const getDefaultCachedData = () => nuxtApp.isHydrating ? nuxtApp.payload.data[key] : nuxtApp.static.data[key]
|
||||||
|
|
||||||
// Apply defaults
|
// Apply defaults
|
||||||
options.server = options.server ?? true
|
options.server = options.server ?? true
|
||||||
@ -242,32 +242,32 @@ export function useAsyncData<
|
|||||||
const hasCachedData = () => ![null, undefined].includes(options.getCachedData!(key) as any)
|
const hasCachedData = () => ![null, undefined].includes(options.getCachedData!(key) as any)
|
||||||
|
|
||||||
// Create or use a shared asyncData entity
|
// Create or use a shared asyncData entity
|
||||||
if (!nuxt._asyncData[key] || !options.immediate) {
|
if (!nuxtApp._asyncData[key] || !options.immediate) {
|
||||||
nuxt.payload._errors[key] ??= null
|
nuxtApp.payload._errors[key] ??= null
|
||||||
|
|
||||||
const _ref = options.deep ? ref : shallowRef
|
const _ref = options.deep ? ref : shallowRef
|
||||||
|
|
||||||
nuxt._asyncData[key] = {
|
nuxtApp._asyncData[key] = {
|
||||||
data: _ref(options.getCachedData!(key) ?? options.default!()),
|
data: _ref(options.getCachedData!(key) ?? options.default!()),
|
||||||
pending: ref(!hasCachedData()),
|
pending: ref(!hasCachedData()),
|
||||||
error: toRef(nuxt.payload._errors, key),
|
error: toRef(nuxtApp.payload._errors, key),
|
||||||
status: ref('idle')
|
status: ref('idle')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Else, somehow check for conflicting keys with different defaults or fetcher
|
// TODO: Else, somehow check for conflicting keys with different defaults or fetcher
|
||||||
const asyncData = { ...nuxt._asyncData[key] } as AsyncData<DataT | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
const asyncData = { ...nuxtApp._asyncData[key] } as AsyncData<DataT | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
||||||
|
|
||||||
asyncData.refresh = asyncData.execute = (opts = {}) => {
|
asyncData.refresh = asyncData.execute = (opts = {}) => {
|
||||||
if (nuxt._asyncDataPromises[key]) {
|
if (nuxtApp._asyncDataPromises[key]) {
|
||||||
if (isDefer(opts.dedupe ?? options.dedupe)) {
|
if (isDefer(opts.dedupe ?? options.dedupe)) {
|
||||||
// Avoid fetching same key more than once at a time
|
// Avoid fetching same key more than once at a time
|
||||||
return nuxt._asyncDataPromises[key]!
|
return nuxtApp._asyncDataPromises[key]!
|
||||||
}
|
}
|
||||||
(nuxt._asyncDataPromises[key] as any).cancelled = true
|
(nuxtApp._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 || (nuxt.isHydrating && opts._initial !== false)) && hasCachedData()) {
|
if ((opts._initial || (nuxtApp.isHydrating && opts._initial !== false)) && hasCachedData()) {
|
||||||
return Promise.resolve(options.getCachedData!(key))
|
return Promise.resolve(options.getCachedData!(key))
|
||||||
}
|
}
|
||||||
asyncData.pending.value = true
|
asyncData.pending.value = true
|
||||||
@ -276,14 +276,14 @@ export function useAsyncData<
|
|||||||
const promise = new Promise<ResT>(
|
const promise = new Promise<ResT>(
|
||||||
(resolve, reject) => {
|
(resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
resolve(handler(nuxt))
|
resolve(handler(nuxtApp))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(err)
|
reject(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((_result) => {
|
.then((_result) => {
|
||||||
// If this request is cancelled, resolve to the latest request.
|
// If this request is cancelled, resolve to the latest request.
|
||||||
if ((promise as any).cancelled) { return nuxt._asyncDataPromises[key] }
|
if ((promise as any).cancelled) { return nuxtApp._asyncDataPromises[key] }
|
||||||
|
|
||||||
let result = _result as unknown as DataT
|
let result = _result as unknown as DataT
|
||||||
if (options.transform) {
|
if (options.transform) {
|
||||||
@ -293,7 +293,7 @@ export function useAsyncData<
|
|||||||
result = pick(result as any, options.pick) as DataT
|
result = pick(result as any, options.pick) as DataT
|
||||||
}
|
}
|
||||||
|
|
||||||
nuxt.payload.data[key] = result
|
nuxtApp.payload.data[key] = result
|
||||||
|
|
||||||
asyncData.data.value = result
|
asyncData.data.value = result
|
||||||
asyncData.error.value = null
|
asyncData.error.value = null
|
||||||
@ -301,7 +301,7 @@ export function useAsyncData<
|
|||||||
})
|
})
|
||||||
.catch((error: any) => {
|
.catch((error: any) => {
|
||||||
// If this request is cancelled, resolve to the latest request.
|
// If this request is cancelled, resolve to the latest request.
|
||||||
if ((promise as any).cancelled) { return nuxt._asyncDataPromises[key] }
|
if ((promise as any).cancelled) { return nuxtApp._asyncDataPromises[key] }
|
||||||
|
|
||||||
asyncData.error.value = createError<NuxtErrorDataT>(error) as (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)
|
asyncData.error.value = createError<NuxtErrorDataT>(error) as (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)
|
||||||
asyncData.data.value = unref(options.default!())
|
asyncData.data.value = unref(options.default!())
|
||||||
@ -312,15 +312,15 @@ export function useAsyncData<
|
|||||||
|
|
||||||
asyncData.pending.value = false
|
asyncData.pending.value = false
|
||||||
|
|
||||||
delete nuxt._asyncDataPromises[key]
|
delete nuxtApp._asyncDataPromises[key]
|
||||||
})
|
})
|
||||||
nuxt._asyncDataPromises[key] = promise
|
nuxtApp._asyncDataPromises[key] = promise
|
||||||
return nuxt._asyncDataPromises[key]!
|
return nuxtApp._asyncDataPromises[key]!
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialFetch = () => asyncData.refresh({ _initial: true })
|
const initialFetch = () => asyncData.refresh({ _initial: true })
|
||||||
|
|
||||||
const fetchOnServer = options.server !== false && nuxt.payload.serverRendered
|
const fetchOnServer = options.server !== false && nuxtApp.payload.serverRendered
|
||||||
|
|
||||||
// Server side
|
// Server side
|
||||||
if (import.meta.server && fetchOnServer && options.immediate) {
|
if (import.meta.server && fetchOnServer && options.immediate) {
|
||||||
@ -328,7 +328,7 @@ export function useAsyncData<
|
|||||||
if (getCurrentInstance()) {
|
if (getCurrentInstance()) {
|
||||||
onServerPrefetch(() => promise)
|
onServerPrefetch(() => promise)
|
||||||
} else {
|
} else {
|
||||||
nuxt.hook('app:created', async () => { await promise })
|
nuxtApp.hook('app:created', async () => { await promise })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,7 +336,7 @@ export function useAsyncData<
|
|||||||
if (import.meta.client) {
|
if (import.meta.client) {
|
||||||
// Setup hook callbacks once per instance
|
// Setup hook callbacks once per instance
|
||||||
const instance = getCurrentInstance()
|
const instance = getCurrentInstance()
|
||||||
if (import.meta.dev && !nuxt.isHydrating && (!instance || instance?.isMounted)) {
|
if (import.meta.dev && !nuxtApp.isHydrating && (!instance || instance?.isMounted)) {
|
||||||
// @ts-expect-error private property
|
// @ts-expect-error private property
|
||||||
console.warn(`[nuxt] [${options._functionName || 'useAsyncData'}] Component is already mounted, please use $fetch instead. See https://nuxt.com/docs/getting-started/data-fetching`)
|
console.warn(`[nuxt] [${options._functionName || 'useAsyncData'}] Component is already mounted, please use $fetch instead. See https://nuxt.com/docs/getting-started/data-fetching`)
|
||||||
}
|
}
|
||||||
@ -352,11 +352,11 @@ export function useAsyncData<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fetchOnServer && nuxt.isHydrating && (asyncData.error.value || hasCachedData())) {
|
if (fetchOnServer && nuxtApp.isHydrating && (asyncData.error.value || hasCachedData())) {
|
||||||
// 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 && ((nuxtApp.payload.serverRendered && nuxtApp.isHydrating) || options.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)
|
||||||
@ -367,7 +367,7 @@ export function useAsyncData<
|
|||||||
if (options.watch) {
|
if (options.watch) {
|
||||||
watch(options.watch, () => asyncData.refresh())
|
watch(options.watch, () => asyncData.refresh())
|
||||||
}
|
}
|
||||||
const off = nuxt.hook('app:data:refresh', async (keys) => {
|
const off = nuxtApp.hook('app:data:refresh', async (keys) => {
|
||||||
if (!keys || keys.includes(key)) {
|
if (!keys || keys.includes(key)) {
|
||||||
await asyncData.refresh()
|
await asyncData.refresh()
|
||||||
}
|
}
|
||||||
@ -378,7 +378,7 @@ export function useAsyncData<
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Allow directly awaiting on asyncData
|
// Allow directly awaiting on asyncData
|
||||||
const asyncDataPromise = Promise.resolve(nuxt._asyncDataPromises[key]).then(() => asyncData) as AsyncData<ResT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
const asyncDataPromise = Promise.resolve(nuxtApp._asyncDataPromises[key]).then(() => asyncData) as AsyncData<ResT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
||||||
Object.assign(asyncDataPromise, asyncData)
|
Object.assign(asyncDataPromise, asyncData)
|
||||||
|
|
||||||
return asyncDataPromise as AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
return asyncDataPromise as AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
||||||
@ -449,15 +449,15 @@ export function useLazyAsyncData<
|
|||||||
|
|
||||||
/** @since 3.1.0 */
|
/** @since 3.1.0 */
|
||||||
export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | null> } {
|
export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | null> } {
|
||||||
const nuxt = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
|
|
||||||
// Initialize value when key is not already set
|
// Initialize value when key is not already set
|
||||||
if (!(key in nuxt.payload.data)) {
|
if (!(key in nuxtApp.payload.data)) {
|
||||||
nuxt.payload.data[key] = null
|
nuxtApp.payload.data[key] = null
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: toRef(nuxt.payload.data, key)
|
data: toRef(nuxtApp.payload.data, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,17 +9,17 @@ import type { NuxtPayload } from '../nuxt'
|
|||||||
* @since 3.0.0
|
* @since 3.0.0
|
||||||
*/
|
*/
|
||||||
export const useHydration = <K extends keyof NuxtPayload, T = NuxtPayload[K]> (key: K, get: () => T, set: (value: T) => void) => {
|
export const useHydration = <K extends keyof NuxtPayload, T = NuxtPayload[K]> (key: K, get: () => T, set: (value: T) => void) => {
|
||||||
const nuxt = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
|
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
nuxt.hooks.hook('app:rendered', () => {
|
nuxtApp.hooks.hook('app:rendered', () => {
|
||||||
nuxt.payload[key] = get()
|
nuxtApp.payload[key] = get()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.client) {
|
if (import.meta.client) {
|
||||||
nuxt.hooks.hook('app:created', () => {
|
nuxtApp.hooks.hook('app:created', () => {
|
||||||
set(nuxt.payload[key] as T)
|
set(nuxtApp.payload[key] as T)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,13 +24,13 @@ export function useState <T> (...args: any): Ref<T> {
|
|||||||
}
|
}
|
||||||
const key = useStateKeyPrefix + _key
|
const key = useStateKeyPrefix + _key
|
||||||
|
|
||||||
const nuxt = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
const state = toRef(nuxt.payload.state, key)
|
const state = toRef(nuxtApp.payload.state, key)
|
||||||
if (state.value === undefined && init) {
|
if (state.value === undefined && init) {
|
||||||
const initialValue = init()
|
const initialValue = init()
|
||||||
if (isRef(initialValue)) {
|
if (isRef(initialValue)) {
|
||||||
// vue will unwrap the ref for us
|
// vue will unwrap the ref for us
|
||||||
nuxt.payload.state[key] = initialValue
|
nuxtApp.payload.state[key] = initialValue
|
||||||
return initialValue as Ref<T>
|
return initialValue as Ref<T>
|
||||||
}
|
}
|
||||||
state.value = initialValue
|
state.value = initialValue
|
||||||
|
Loading…
Reference in New Issue
Block a user