Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nitro): update nitro internal hook name #218

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/kit/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions packages/nitro/src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

@pi0 pi0 Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for the future: We need to once fix two way hooks and avoid manually connecting them.


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

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/presets/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ if ('serviceWorker' in navigator) {
}
},
hooks: {
'nitro:template:document' (tmpl) {
'nitro:document' (tmpl) {
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
},
async 'nitro:compiled' ({ output }: NitroContext) {
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt3/src/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ 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))

// 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
Expand Down