fix(nuxt): use payload url for isPrerendered, not current route (#24031)

This commit is contained in:
Daniel Roe 2023-10-30 21:56:34 +01:00 committed by GitHub
parent e297368ee3
commit 62b7917d2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 51 deletions

View File

@ -23,7 +23,7 @@ export function loadPayload (url: string, opts: LoadPayloadOptions = {}): Record
if (payloadURL in cache) { if (payloadURL in cache) {
return cache[payloadURL] return cache[payloadURL]
} }
cache[payloadURL] = isPrerendered().then((prerendered) => { cache[payloadURL] = isPrerendered(url).then((prerendered) => {
if (!prerendered) { if (!prerendered) {
cache[payloadURL] = null cache[payloadURL] = null
return null return null
@ -78,11 +78,7 @@ async function _importPayload (payloadURL: string) {
export async function isPrerendered (url = useRoute().path) { export async function isPrerendered (url = useRoute().path) {
// Note: Alternative for server is checking x-nitro-prerender header // Note: Alternative for server is checking x-nitro-prerender header
const nuxtApp = useNuxtApp() if (!appManifest) { return !!useNuxtApp().payload.prerenderedAt }
if (nuxtApp.payload.prerenderedAt) {
return true
}
if (!appManifest) { return false }
const manifest = await getAppManifest() const manifest = await getAppManifest()
if (manifest.prerendered.includes(url)) { if (manifest.prerendered.includes(url)) {
return true return true

View File

@ -244,7 +244,8 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
} }
) )
nuxt.hook('nitro:build:before', async (nitro) => { nuxt.hook('nitro:init', (nitro) => {
nitro.hooks.hook('rollup:before', async (nitro) => {
const routeRules = {} as Record<string, any> const routeRules = {} as Record<string, any>
const _routeRules = nitro.options.routeRules const _routeRules = nitro.options.routeRules
for (const key in _routeRules) { for (const key in _routeRules) {
@ -292,6 +293,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
})) }))
await fsp.writeFile(join(tempDir, `meta/${buildId}.json`), JSON.stringify(manifest)) await fsp.writeFile(join(tempDir, `meta/${buildId}.json`), JSON.stringify(manifest))
}) })
})
} }
// Add fallback server for `ssr: false` // Add fallback server for `ssr: false`
@ -457,6 +459,14 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
}) })
} }
// Copy public assets after prerender so app manifest can be present
if (!nuxt.options.dev) {
nitro.hooks.hook('rollup:before', async (nitro) => {
await copyPublicAssets(nitro)
await nuxt.callHook('nitro:build:public-assets', nitro)
})
}
// nuxt build/dev // nuxt build/dev
nuxt.hook('build:done', async () => { nuxt.hook('build:done', async () => {
await nuxt.callHook('nitro:build:before', nitro) await nuxt.callHook('nitro:build:before', nitro)
@ -464,8 +474,6 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
await build(nitro) await build(nitro)
} else { } else {
await prepare(nitro) await prepare(nitro)
await copyPublicAssets(nitro)
await nuxt.callHook('nitro:build:public-assets', nitro)
await prerender(nitro) await prerender(nitro)
logger.restoreAll() logger.restoreAll()