chore(nuxt): rename nuxt -> nuxtApp internally for consistency

This commit is contained in:
Daniel Roe 2024-02-05 12:02:41 +00:00
parent 9dfc0c2c31
commit c5d5932f57
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
3 changed files with 41 additions and 41 deletions

View File

@ -209,21 +209,21 @@ export function useAsyncData<
}
// Setup nuxt instance payload
const nuxt = useNuxtApp()
const nuxtApp = useNuxtApp()
// When prerendering, share payload data automatically between requests
const handler = import.meta.client || !import.meta.prerender || !nuxt.ssrContext?._sharedPrerenderCache ? _handler : async () => {
const value = await nuxt.ssrContext!._sharedPrerenderCache!.get(key)
const handler = import.meta.client || !import.meta.prerender || !nuxtApp.ssrContext?._sharedPrerenderCache ? _handler : async () => {
const value = await nuxtApp.ssrContext!._sharedPrerenderCache!.get(key)
if (value) { return value as ResT }
const promise = nuxt.runWithContext(_handler)
nuxt.ssrContext!._sharedPrerenderCache!.set(key, promise)
const promise = nuxtApp.runWithContext(_handler)
nuxtApp.ssrContext!._sharedPrerenderCache!.set(key, promise)
return promise
}
// Used to get default values
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
options.server = options.server ?? true
@ -242,32 +242,32 @@ export function useAsyncData<
const hasCachedData = () => ![null, undefined].includes(options.getCachedData!(key) as any)
// Create or use a shared asyncData entity
if (!nuxt._asyncData[key] || !options.immediate) {
nuxt.payload._errors[key] ??= null
if (!nuxtApp._asyncData[key] || !options.immediate) {
nuxtApp.payload._errors[key] ??= null
const _ref = options.deep ? ref : shallowRef
nuxt._asyncData[key] = {
nuxtApp._asyncData[key] = {
data: _ref(options.getCachedData!(key) ?? options.default!()),
pending: ref(!hasCachedData()),
error: toRef(nuxt.payload._errors, key),
error: toRef(nuxtApp.payload._errors, key),
status: ref('idle')
}
}
// 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 = {}) => {
if (nuxt._asyncDataPromises[key]) {
if (nuxtApp._asyncDataPromises[key]) {
if (isDefer(opts.dedupe ?? options.dedupe)) {
// 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
if ((opts._initial || (nuxt.isHydrating && opts._initial !== false)) && hasCachedData()) {
if ((opts._initial || (nuxtApp.isHydrating && opts._initial !== false)) && hasCachedData()) {
return Promise.resolve(options.getCachedData!(key))
}
asyncData.pending.value = true
@ -276,14 +276,14 @@ export function useAsyncData<
const promise = new Promise<ResT>(
(resolve, reject) => {
try {
resolve(handler(nuxt))
resolve(handler(nuxtApp))
} catch (err) {
reject(err)
}
})
.then((_result) => {
// 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
if (options.transform) {
@ -293,7 +293,7 @@ export function useAsyncData<
result = pick(result as any, options.pick) as DataT
}
nuxt.payload.data[key] = result
nuxtApp.payload.data[key] = result
asyncData.data.value = result
asyncData.error.value = null
@ -301,7 +301,7 @@ export function useAsyncData<
})
.catch((error: any) => {
// 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.data.value = unref(options.default!())
@ -312,15 +312,15 @@ export function useAsyncData<
asyncData.pending.value = false
delete nuxt._asyncDataPromises[key]
delete nuxtApp._asyncDataPromises[key]
})
nuxt._asyncDataPromises[key] = promise
return nuxt._asyncDataPromises[key]!
nuxtApp._asyncDataPromises[key] = promise
return nuxtApp._asyncDataPromises[key]!
}
const initialFetch = () => asyncData.refresh({ _initial: true })
const fetchOnServer = options.server !== false && nuxt.payload.serverRendered
const fetchOnServer = options.server !== false && nuxtApp.payload.serverRendered
// Server side
if (import.meta.server && fetchOnServer && options.immediate) {
@ -328,7 +328,7 @@ export function useAsyncData<
if (getCurrentInstance()) {
onServerPrefetch(() => promise)
} 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) {
// Setup hook callbacks once per instance
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
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
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 && ((nuxtApp.payload.serverRendered && nuxtApp.isHydrating) || options.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)
@ -367,7 +367,7 @@ export function useAsyncData<
if (options.watch) {
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)) {
await asyncData.refresh()
}
@ -378,7 +378,7 @@ export function useAsyncData<
}
// 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)
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 */
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
if (!(key in nuxt.payload.data)) {
nuxt.payload.data[key] = null
if (!(key in nuxtApp.payload.data)) {
nuxtApp.payload.data[key] = null
}
return {
data: toRef(nuxt.payload.data, key)
data: toRef(nuxtApp.payload.data, key)
}
}

View File

@ -9,17 +9,17 @@ import type { NuxtPayload } from '../nuxt'
* @since 3.0.0
*/
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) {
nuxt.hooks.hook('app:rendered', () => {
nuxt.payload[key] = get()
nuxtApp.hooks.hook('app:rendered', () => {
nuxtApp.payload[key] = get()
})
}
if (import.meta.client) {
nuxt.hooks.hook('app:created', () => {
set(nuxt.payload[key] as T)
nuxtApp.hooks.hook('app:created', () => {
set(nuxtApp.payload[key] as T)
})
}
}

View File

@ -24,13 +24,13 @@ export function useState <T> (...args: any): Ref<T> {
}
const key = useStateKeyPrefix + _key
const nuxt = useNuxtApp()
const state = toRef(nuxt.payload.state, key)
const nuxtApp = useNuxtApp()
const state = toRef(nuxtApp.payload.state, key)
if (state.value === undefined && init) {
const initialValue = init()
if (isRef(initialValue)) {
// vue will unwrap the ref for us
nuxt.payload.state[key] = initialValue
nuxtApp.payload.state[key] = initialValue
return initialValue as Ref<T>
}
state.value = initialValue