fix(nuxt): reload nitro when spa loading template updates (#24036)

This commit is contained in:
Daniel Roe 2023-10-31 19:05:06 +01:00 committed by GitHub
parent c4bb8f31d4
commit 948b30d9c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
import { existsSync, promises as fsp, readFileSync } from 'node:fs'
import { cpus } from 'node:os'
import { join, relative, resolve } from 'pathe'
import { join, normalize, relative, resolve } from 'pathe'
import { createRouter as createRadixRouter, exportMatcher, toRouteMatcher } from 'radix3'
import { randomUUID } from 'uncrypto'
import { joinURL, withTrailingSlash } from 'ufo'
@ -366,6 +366,13 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// Init nitro
const nitro = await createNitro(nitroConfig)
// Trigger Nitro reload when SPA loading template changes
nuxt.hook('builder:watch', async (_event, path) => {
if (normalize(path) === spaLoadingTemplatePath(nuxt)) {
await nitro.hooks.callHook('rollup:reload')
}
})
// Set prerender-only options
nitro.options._config.storage ||= {}
nitro.options._config.storage['internal:nuxt:prerender'] = { driver: 'memory' }
@ -507,12 +514,16 @@ function relativeWithDot (from: string, to: string) {
return relative(from, to).replace(/^([^.])/, './$1') || '.'
}
function spaLoadingTemplatePath (nuxt: Nuxt) {
return typeof nuxt.options.spaLoadingTemplate === 'string'
? resolve(nuxt.options.srcDir, nuxt.options.spaLoadingTemplate)
: resolve(nuxt.options.srcDir, 'app/spa-loading-template.html')
}
function spaLoadingTemplate (nuxt: Nuxt) {
if (nuxt.options.spaLoadingTemplate === false) { return '' }
const spaLoadingTemplate = typeof nuxt.options.spaLoadingTemplate === 'string'
? nuxt.options.spaLoadingTemplate
: resolve(nuxt.options.srcDir, 'app/spa-loading-template.html')
const spaLoadingTemplate = spaLoadingTemplatePath(nuxt)
try {
if (existsSync(spaLoadingTemplate)) {