From 28af761c74949e3c712ad05ef6bc73f0f50f7ab1 Mon Sep 17 00:00:00 2001 From: David Gonzalez Date: Mon, 11 Sep 2023 13:02:28 +0200 Subject: [PATCH] fix(nuxt): load `spaLoadingTemplate` if file exists (#23048) --- packages/nuxt/src/core/nitro.ts | 36 +++++++++++++++++++------------ packages/schema/src/config/app.ts | 11 +++++----- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 73a7a83377..a903a78e9b 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -30,11 +30,6 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { ? [new RegExp(`node_modules\\/(?!${excludePaths.join('|')})`)] : [/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, { debug: nuxt.options.debug, rootDir: nuxt.options.rootDir, @@ -86,15 +81,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { baseURL: nuxt.options.app.baseURL, virtual: { '#internal/nuxt.config.mjs': () => nuxt.vfs['#build/nuxt.config'], - '#spa-template': () => { - 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({}))}` - } + '#spa-template': () => `export const template = ${JSON.stringify(spaLoadingTemplate(nuxt))}` }, routeRules: { '/__nuxt_error': { cache: false } @@ -415,3 +402,24 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { function relativeWithDot (from: string, to: string) { 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 '' +} diff --git a/packages/schema/src/config/app.ts b/packages/schema/src/config/app.ts index d7b75d1523..7ef5c4d79a 100644 --- a/packages/schema/src/config/app.ts +++ b/packages/schema/src/config/app.ts @@ -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`. - * - * By default Nuxt will look in `~/app/spa-loading-template.html` for this file. - * - * You can set this to `false` to disable any loading indicator. + * - If it is unset, it will use `~/app/spa-loading-template.html` if it exists. + * - 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. * * 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} */ 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 }, /**