fix(nuxt): load spaLoadingTemplate if file exists (#23048)

This commit is contained in:
David Gonzalez 2023-09-11 13:02:28 +02:00 committed by GitHub
parent 40601ec15f
commit 28af761c74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 20 deletions

View File

@ -30,11 +30,6 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
? [new RegExp(`node_modules\\/(?!${excludePaths.join('|')})`)] ? [new RegExp(`node_modules\\/(?!${excludePaths.join('|')})`)]
: [/node_modules/] : [/node_modules/]
const spaLoadingTemplate = nuxt.options.spaLoadingTemplate ?? resolve(nuxt.options.srcDir, 'app/spa-loading-template.html')
if (spaLoadingTemplate && nuxt.options.spaLoadingTemplate && typeof spaLoadingTemplate !== 'boolean' && !existsSync(spaLoadingTemplate)) {
console.warn(`[nuxt] Could not load custom \`spaLoadingTemplate\` path as it does not exist: \`${spaLoadingTemplate}\`.`)
}
const nitroConfig: NitroConfig = defu(_nitroConfig, { const nitroConfig: NitroConfig = defu(_nitroConfig, {
debug: nuxt.options.debug, debug: nuxt.options.debug,
rootDir: nuxt.options.rootDir, rootDir: nuxt.options.rootDir,
@ -86,15 +81,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
baseURL: nuxt.options.app.baseURL, baseURL: nuxt.options.app.baseURL,
virtual: { virtual: {
'#internal/nuxt.config.mjs': () => nuxt.vfs['#build/nuxt.config'], '#internal/nuxt.config.mjs': () => nuxt.vfs['#build/nuxt.config'],
'#spa-template': () => { '#spa-template': () => `export const template = ${JSON.stringify(spaLoadingTemplate(nuxt))}`
if (!spaLoadingTemplate) { return 'export const template = ""' }
try {
if (spaLoadingTemplate !== true) {
return `export const template = ${JSON.stringify(readFileSync(spaLoadingTemplate, 'utf-8'))}`
}
} catch {}
return `export const template = ${JSON.stringify(defaultSpaLoadingTemplate({}))}`
}
}, },
routeRules: { routeRules: {
'/__nuxt_error': { cache: false } '/__nuxt_error': { cache: false }
@ -415,3 +402,24 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
function relativeWithDot (from: string, to: string) { function relativeWithDot (from: string, to: string) {
return relative(from, to).replace(/^([^.])/, './$1') || '.' return relative(from, to).replace(/^([^.])/, './$1') || '.'
} }
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')
try {
if (existsSync(spaLoadingTemplate)) {
return readFileSync(spaLoadingTemplate, 'utf-8')
}
} catch {}
if (nuxt.options.spaLoadingTemplate === true) {
return defaultSpaLoadingTemplate({})
}
console.warn(`[nuxt] Could not load custom \`spaLoadingTemplate\` path as it does not exist: \`${nuxt.options.spaLoadingTemplate}\`.`)
return ''
}

View File

@ -189,12 +189,11 @@ export default defineUntypedSchema({
}, },
/** /**
* A path to an HTML file, the contents of which will be inserted into any HTML page * Boolean or a path to an HTML file with the contents of which will be inserted into any HTML page
* rendered with `ssr: false`. * rendered with `ssr: false`.
* * - If it is unset, it will use `~/app/spa-loading-template.html` if it exists.
* By default Nuxt will look in `~/app/spa-loading-template.html` for this file. * - If it is false, no SPA loading indicator will be loaded.
* * - If true, Nuxt will look for `~/app/spa-loading-template.html` file or a default Nuxt image will be used.
* You can set this to `false` to disable any loading indicator.
* *
* Some good sources for spinners are [SpinKit](https://github.com/tobiasahlin/SpinKit) or [SVG Spinners](https://icones.js.org/collection/svg-spinners). * Some good sources for spinners are [SpinKit](https://github.com/tobiasahlin/SpinKit) or [SVG Spinners](https://icones.js.org/collection/svg-spinners).
* *
@ -244,7 +243,7 @@ export default defineUntypedSchema({
* @type {string | boolean} * @type {string | boolean}
*/ */
spaLoadingTemplate: { spaLoadingTemplate: {
$resolve: async (val, get) => typeof val === 'string' ? resolve(await get('srcDir'), val) : (val ?? false) $resolve: async (val, get) => typeof val === 'string' ? resolve(await get('srcDir'), val) : val
}, },
/** /**