From 4cbde2a57e94f44761a95ce7695f6571c2c6ff30 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 18 Oct 2022 11:40:05 +0100 Subject: [PATCH 1/3] test: wrap other handlers --- .../basic/extends/node_modules/foo/server/api/foo.ts | 4 +++- .../basic/extends/node_modules/foo/server/middleware/foo.ts | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test/fixtures/basic/extends/node_modules/foo/server/api/foo.ts b/test/fixtures/basic/extends/node_modules/foo/server/api/foo.ts index f759e165008..4529f026397 100644 --- a/test/fixtures/basic/extends/node_modules/foo/server/api/foo.ts +++ b/test/fixtures/basic/extends/node_modules/foo/server/api/foo.ts @@ -1 +1,3 @@ -export default () => 'foo' +import { eventHandler } from 'h3' + +export default eventHandler(() => 'foo') diff --git a/test/fixtures/basic/extends/node_modules/foo/server/middleware/foo.ts b/test/fixtures/basic/extends/node_modules/foo/server/middleware/foo.ts index 0af570f235f..aedbcc3fbf4 100644 --- a/test/fixtures/basic/extends/node_modules/foo/server/middleware/foo.ts +++ b/test/fixtures/basic/extends/node_modules/foo/server/middleware/foo.ts @@ -1,4 +1,6 @@ // TODO: add back TypeScript and auto-importing once Nitro supports it -export default (event) => { +import { eventHandler } from 'h3' + +export default eventHandler((event) => { event.res.setHeader('injected-header', 'foo') -} +}) From f0c5c6b953060190121cb7f2403ad15f61d34964 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 18 Oct 2022 11:40:19 +0100 Subject: [PATCH 2/3] fix(nuxt): lazy-load entry CSS --- packages/nuxt/src/core/runtime/nitro/renderer.ts | 13 +++++++++++-- packages/vite/src/server.ts | 4 ++-- test/basic.test.ts | 3 +-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index 24199f51e8e..ceff0fd6257 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -10,7 +10,12 @@ import { useRuntimeConfig, useNitroApp, defineRenderHandler, getRouteRules } fro import type { NuxtApp, NuxtSSRContext } from '#app' // @ts-ignore -import { buildAssetsURL } from '#paths' +import { buildAssetsURL, publicAssetsURL } from '#paths' + +// @ts-ignore +globalThis.__buildAssetsURL = buildAssetsURL +// @ts-ignore +globalThis.__publicAssetsURL = publicAssetsURL export interface NuxtRenderHTMLContext { htmlAttrs: string[] @@ -198,7 +203,7 @@ export default defineRenderHandler(async (event) => { const renderedMeta = await ssrContext.renderMeta?.() ?? {} // Render inline styles - const inlinedStyles = process.env.NUXT_INLINE_STYLES && !(process.env.NUXT_NO_SSR || ssrContext.noSSR) + const inlinedStyles = process.env.NUXT_INLINE_STYLES ? await renderInlineStyles(ssrContext.modules ?? ssrContext._registeredComponents ?? []) : '' @@ -284,6 +289,7 @@ function renderHTMLDocument (html: NuxtRenderHTMLContext) { } async function renderInlineStyles (usedModules: Set | string[]) { + const { entryCSS } = await getClientManifest() const styleMap = await getSSRStyles() const inlinedStyles = new Set() for (const mod of ['entry', ...usedModules]) { @@ -293,6 +299,9 @@ async function renderInlineStyles (usedModules: Set | string[]) { } } } + for (const css of entryCSS?.css || []) { + inlinedStyles.add(``) + } return Array.from(inlinedStyles).join('') } diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index fe99919aa2e..66b0b9b47db 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -129,13 +129,13 @@ export async function buildServer (ctx: ViteBuildContext) { } // Add entry CSS as prefetch (non-blocking) if (entry.isEntry) { - manifest[key + '-css'] = { + manifest.entryCSS = { file: '', css: entry.css } entry.css = [] entry.dynamicImports = entry.dynamicImports || [] - entry.dynamicImports.push(key + '-css') + entry.dynamicImports.push('entryCSS') } } }) diff --git a/test/basic.test.ts b/test/basic.test.ts index 2c22c0cab86..7d95f6712c1 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -616,8 +616,7 @@ describe.skipIf(process.env.NUXT_TEST_DEV || process.env.TEST_WITH_WEBPACK)('inl `) }) - // TODO: fix this in style inlining implementation: https://github.com/nuxt/framework/pull/8265#issuecomment-1282148407 - it.skip('still downloads client-only styles', async () => { + it('still downloads client-only styles', async () => { const page = await createPage('/styles') await page.waitForLoadState('networkidle') expect(await page.$eval('.client-only-css', e => getComputedStyle(e).color)).toBe('rgb(50, 50, 50)') From 271f83463f9883f8e968c917d324eced3a6c455f Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 18 Oct 2022 12:38:13 +0100 Subject: [PATCH 3/3] fix: avoid registering global path helpers twice --- packages/nuxt/src/core/templates.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 4aa110f9124..76d914672d6 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -240,8 +240,11 @@ export const publicPathTemplate: NuxtTemplate = { ' return path.length ? joinURL(publicBase, ...path) : publicBase', '}', - 'globalThis.__buildAssetsURL = buildAssetsURL', - 'globalThis.__publicAssetsURL = publicAssetsURL' + // On server these are registered directly in packages/nuxt/src/core/runtime/nitro/renderer.ts + 'if (process.client) {', + ' globalThis.__buildAssetsURL = buildAssetsURL', + ' globalThis.__publicAssetsURL = publicAssetsURL', + '}' ].filter(Boolean).join('\n') } }