From 1bbd754f29ee525b45454a2427682a7fa801610b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 3 Jun 2021 14:12:05 +0100 Subject: [PATCH] refactor(nitro): app template 2-3 normalization (#177) --- packages/app/src/_templates/views/app.template.html | 4 ++-- packages/kit/src/types/hooks.ts | 3 +++ packages/nitro/src/build.ts | 2 +- packages/nitro/src/compat.ts | 4 ++-- packages/nitro/src/runtime/app/render.ts | 7 ++++--- packages/nitro/src/runtime/app/vue3.ts | 6 +++++- packages/nuxt3/src/nitro.ts | 2 ++ 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/app/src/_templates/views/app.template.html b/packages/app/src/_templates/views/app.template.html index ccf7df0307..276240846d 100644 --- a/packages/app/src/_templates/views/app.template.html +++ b/packages/app/src/_templates/views/app.template.html @@ -5,9 +5,9 @@ {{ BODY_SCRIPTS_PREPEND }} -
{{ APP }}
+ {{ APP }} + {{ BODY_SCRIPTS }} <% if (nuxt.options.vite && nuxt.options.dev) { %> <% } %> - {{ BODY_SCRIPTS }} diff --git a/packages/kit/src/types/hooks.ts b/packages/kit/src/types/hooks.ts index bd041c4a83..777a42bab4 100644 --- a/packages/kit/src/types/hooks.ts +++ b/packages/kit/src/types/hooks.ts @@ -57,6 +57,9 @@ export interface NuxtHooks { // 'watch:fileChanged': (builder: Builder, fileName: string) => HookResult 'builder:watch': (event: WatchEvent, path: string) => HookResult + // @nuxt/nitro + 'nitro:template': (template: { src: string, contents: string }) => HookResult + // @nuxt/cli 'cli:buildError': (error: unknown) => HookResult 'generate:cache:ignore': (ignore: string[]) => HookResult diff --git a/packages/nitro/src/build.ts b/packages/nitro/src/build.ts index a94bfe4784..31db3d5e73 100644 --- a/packages/nitro/src/build.ts +++ b/packages/nitro/src/build.ts @@ -49,8 +49,8 @@ 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') - htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents) await nitroContext._internal.hooks.callHook('nitro:template:document', htmlTemplate) + htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents) await writeFile(htmlTemplate.dst, htmlTemplate.compiled) nitroContext.rollupConfig = getRollupConfig(nitroContext) diff --git a/packages/nitro/src/compat.ts b/packages/nitro/src/compat.ts index 3ac9b2f5b0..2aa9dccb53 100644 --- a/packages/nitro/src/compat.ts +++ b/packages/nitro/src/compat.ts @@ -6,7 +6,6 @@ import { getNitroContext, NitroContext } from './context' import { createDevServer } from './server/dev' import { wpfs } from './utils/wpfs' import { resolveMiddleware } from './server/middleware' -import { serializeTemplate } from './utils' export default function nuxt2CompatModule () { const { nuxt } = this @@ -34,6 +33,8 @@ export default function nuxt2CompatModule () { 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 @@ -57,7 +58,6 @@ export default function nuxt2CompatModule () { if (!htmlTemplate.contents.includes('BODY_SCRIPTS_PREPEND')) { const fullTemplate = ['{{ BODY_SCRIPTS_PREPEND }}', '{{ APP }}', '{{ BODY_SCRIPTS }}'].join('\n ') htmlTemplate.contents = htmlTemplate.contents.replace('{{ APP }}', fullTemplate) - htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents) } }) diff --git a/packages/nitro/src/runtime/app/render.ts b/packages/nitro/src/runtime/app/render.ts index a453673f68..1731940d92 100644 --- a/packages/nitro/src/runtime/app/render.ts +++ b/packages/nitro/src/runtime/app/render.ts @@ -103,9 +103,10 @@ function renderHTML (payload, rendered, ssrContext) { meta.htmlAttrs += _meta.htmlAttrs.text() meta.headAttrs += _meta.headAttrs.text() meta.headTags += - _meta.title.text() + _meta.meta.text() + - _meta.link.text() + _meta.style.text() + - _meta.script.text() + _meta.noscript.text() + _meta.title.text() + _meta.base.text() + + _meta.meta.text() + _meta.link.text() + + _meta.style.text() + _meta.script.text() + + _meta.noscript.text() meta.bodyAttrs += _meta.bodyAttrs.text() meta.bodyScriptsPrepend = _meta.meta.text({ pbody: true }) + _meta.link.text({ pbody: true }) + diff --git a/packages/nitro/src/runtime/app/vue3.ts b/packages/nitro/src/runtime/app/vue3.ts index f3fd7daa59..e76354bf15 100644 --- a/packages/nitro/src/runtime/app/vue3.ts +++ b/packages/nitro/src/runtime/app/vue3.ts @@ -1,2 +1,6 @@ // @ts-ignore -export { renderToString } from '@vue/server-renderer' +import { renderToString as render } from '@vue/server-renderer' + +export const renderToString: typeof render = (...args) => { + return render(...args).then(result => `
${result}
`) +} diff --git a/packages/nuxt3/src/nitro.ts b/packages/nuxt3/src/nitro.ts index 702476602c..c88c872f82 100644 --- a/packages/nuxt3/src/nitro.ts +++ b/packages/nuxt3/src/nitro.ts @@ -21,6 +21,8 @@ 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