diff --git a/packages/kit/src/types/hooks.ts b/packages/kit/src/types/hooks.ts index 777a42bab4..42bb644075 100644 --- a/packages/kit/src/types/hooks.ts +++ b/packages/kit/src/types/hooks.ts @@ -58,7 +58,7 @@ export interface NuxtHooks { 'builder:watch': (event: WatchEvent, path: string) => HookResult // @nuxt/nitro - 'nitro:template': (template: { src: string, contents: string }) => HookResult + 'nitro:document': (template: { src: string, contents: string }) => HookResult // @nuxt/cli 'cli:buildError': (error: unknown) => HookResult diff --git a/packages/nitro/src/build.ts b/packages/nitro/src/build.ts index 31db3d5e73..c307eb43a5 100644 --- a/packages/nitro/src/build.ts +++ b/packages/nitro/src/build.ts @@ -49,7 +49,7 @@ export async function build (nitroContext: NitroContext) { const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' } htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.js').replace('app.', 'document.') htmlTemplate.contents = await readFile(htmlTemplate.src, 'utf-8') - await nitroContext._internal.hooks.callHook('nitro:template:document', htmlTemplate) + await nitroContext._internal.hooks.callHook('nitro:document', htmlTemplate) htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents) await writeFile(htmlTemplate.dst, htmlTemplate.compiled) diff --git a/packages/nitro/src/compat.ts b/packages/nitro/src/compat.ts index 2aa9dccb53..c9892ab80d 100644 --- a/packages/nitro/src/compat.ts +++ b/packages/nitro/src/compat.ts @@ -27,14 +27,14 @@ export default function nuxt2CompatModule () { // Connect hooks nuxt.addHooks(nitroContext.nuxtHooks) nuxt.hook('close', () => nitroContext._internal.hooks.callHook('close')) + nitroContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template)) nuxt.addHooks(nitroDevContext.nuxtHooks) nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close')) + nitroDevContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template)) nitroDevContext._internal.hooks.hook('renderLoading', (req, res) => nuxt.callHook('server:nuxt:renderLoading', req, res)) - nitroContext._internal.hooks.hook('nitro:template', template => nuxt.callHook('nitro:template', template)) - // Expose process.env.NITRO_PRESET nuxt.options.env.NITRO_PRESET = nitroContext.preset @@ -54,7 +54,7 @@ export default function nuxt2CompatModule () { }) // Add missing template variables (which normally renderer would create) - nitroContext._internal.hooks.hook('nitro:template:document', (htmlTemplate) => { + nitroContext._internal.hooks.hook('nitro:document', (htmlTemplate) => { if (!htmlTemplate.contents.includes('BODY_SCRIPTS_PREPEND')) { const fullTemplate = ['{{ BODY_SCRIPTS_PREPEND }}', '{{ APP }}', '{{ BODY_SCRIPTS }}'].join('\n ') htmlTemplate.contents = htmlTemplate.contents.replace('{{ APP }}', fullTemplate) diff --git a/packages/nitro/src/presets/browser.ts b/packages/nitro/src/presets/browser.ts index 1c77ef032f..499ca9dbf0 100644 --- a/packages/nitro/src/presets/browser.ts +++ b/packages/nitro/src/presets/browser.ts @@ -61,7 +61,7 @@ if ('serviceWorker' in navigator) { } }, hooks: { - 'nitro:template:document' (tmpl) { + 'nitro:document' (tmpl) { tmpl.compiled = tmpl.compiled.replace('', script + '') }, async 'nitro:compiled' ({ output }: NitroContext) { diff --git a/packages/nuxt3/src/nitro.ts b/packages/nuxt3/src/nitro.ts index fbd5b22be6..1d91fb5ac4 100644 --- a/packages/nuxt3/src/nitro.ts +++ b/packages/nuxt3/src/nitro.ts @@ -13,10 +13,12 @@ export function initNitro (nuxt: Nuxt) { // @ts-ignore nuxt.hooks.addHooks(nitroContext.nuxtHooks) nuxt.hook('close', () => nitroContext._internal.hooks.callHook('close')) + nitroContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template)) // @ts-ignore nuxt.hooks.addHooks(nitroDevContext.nuxtHooks) nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close')) + nitroDevContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template)) // Add nitro client plugin (to inject $fetch helper) nuxt.hook('app:resolve', (app) => { @@ -26,8 +28,6 @@ export function initNitro (nuxt: Nuxt) { // Expose process.env.NITRO_PRESET nuxt.options.env.NITRO_PRESET = nitroContext.preset - nitroContext._internal.hooks.hook('nitro:template', template => nuxt.callHook('nitro:template', template)) - // Wait for all modules to be ready nuxt.hook('modules:done', async () => { // Extend nitro with modules