diff --git a/packages/nuxt/src/app/entry.ts b/packages/nuxt/src/app/entry.ts index 610f1875eb..dede500a50 100644 --- a/packages/nuxt/src/app/entry.ts +++ b/packages/nuxt/src/app/entry.ts @@ -16,7 +16,7 @@ import plugins from '#build/plugins' // @ts-expect-error virtual file import RootComponent from '#build/root-component.mjs' // @ts-expect-error virtual file -import { appRootId } from '#build/nuxt.config.mjs' +import { vueAppRootContainer } from '#build/nuxt.config.mjs' if (!globalThis.$fetch) { globalThis.$fetch = $fetch.create({ @@ -75,7 +75,7 @@ if (import.meta.client) { try { await nuxt.hooks.callHook('app:created', vueApp) await nuxt.hooks.callHook('app:beforeMount', vueApp) - vueApp.mount('#' + appRootId) + vueApp.mount(vueAppRootContainer) await nuxt.hooks.callHook('app:mounted', vueApp) await nextTick() } catch (err) { diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index 5466512c46..891acb6be6 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -117,7 +117,7 @@ const getSSRRenderer = lazyCachedFunction(async () => { if (import.meta.dev && process.env.NUXT_VITE_NODE_OPTIONS) { renderer.rendererContext.updateManifest(await getClientManifest()) } - return `<${appRootTag} id="${appRootId}">${html}` + return `<${appRootTag}${appRootId ? ` id="${appRootId}"` : ''}>${html}` } return renderer @@ -132,7 +132,7 @@ const getSPARenderer = lazyCachedFunction(async () => { const options = { manifest, - renderToString: () => `<${appRootTag} id="${appRootId}">${spaTemplate}`, + renderToString: () => `<${appRootTag}${appRootId ? ` id="${appRootId}"` : ''}>${spaTemplate}`, buildAssetsURL } // Create SPA renderer and cache the result for all requests @@ -191,7 +191,7 @@ async function getIslandContext (event: H3Event): Promise { } const PAYLOAD_URL_RE = process.env.NUXT_JSON_PAYLOADS ? /\/_payload(\.[a-zA-Z0-9]+)?.json(\?.*)?$/ : /\/_payload(\.[a-zA-Z0-9]+)?.js(\?.*)?$/ -const ROOT_NODE_REGEX = new RegExp(`^<${appRootTag} id="${appRootId}">([\\s\\S]*)$`) +const ROOT_NODE_REGEX = new RegExp(`^<${appRootTag}${appRootId ? ` id="${appRootId}"` : ''}>([\\s\\S]*)$`) const PRERENDER_NO_SSR_ROUTES = new Set(['/index.html', '/200.html', '/404.html']) diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 380d7d0cbf..cd8918a7c1 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -339,7 +339,8 @@ export const nuxtConfigTemplate = { `export const componentIslands = ${!!ctx.nuxt.options.experimental.componentIslands}`, `export const remoteComponentIslands = ${ctx.nuxt.options.experimental.componentIslands === 'local+remote'}`, `export const devPagesDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.dir.pages) : 'null'}`, - `export const devRootDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.rootDir) : 'null'}` + `export const devRootDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.rootDir) : 'null'}`, + `export const vueAppRootContainer = ${ctx.nuxt.options.app.rootId ? `'#${ctx.nuxt.options.app.rootId}'` : `'body > ${ctx.nuxt.options.app.rootTag}'`}` ].join('\n\n') } } diff --git a/packages/schema/src/config/app.ts b/packages/schema/src/config/app.ts index 1332618b11..af468b54b0 100644 --- a/packages/schema/src/config/app.ts +++ b/packages/schema/src/config/app.ts @@ -173,9 +173,11 @@ export default defineUntypedSchema({ /** * Customize Nuxt root element id. + * + * @type {string | false} */ rootId: { - $resolve: val => val || '__nuxt' + $resolve: val => val === false ? false : val || '__nuxt' }, /**