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

fix(nuxt): lazy-load entry CSS #8278

Merged
merged 4 commits into from Oct 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/nuxt/src/core/runtime/nitro/renderer.ts
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't they register as side-effects of #paths? (and if they do, do we still need it from #paths?

Copy link
Member Author

Choose a reason for hiding this comment

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

They aren't registered as globals there.

And yes, we probably could wrap these lines in if (process.client) - will try:

'globalThis.__buildAssetsURL = buildAssetsURL',
'globalThis.__publicAssetsURL = publicAssetsURL'


export interface NuxtRenderHTMLContext {
htmlAttrs: string[]
Expand Down Expand Up @@ -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 ?? [])
: ''

Expand Down Expand Up @@ -284,6 +289,7 @@ function renderHTMLDocument (html: NuxtRenderHTMLContext) {
}

async function renderInlineStyles (usedModules: Set<string> | string[]) {
const { entryCSS } = await getClientManifest()
const styleMap = await getSSRStyles()
const inlinedStyles = new Set<string>()
for (const mod of ['entry', ...usedModules]) {
Expand All @@ -293,6 +299,9 @@ async function renderInlineStyles (usedModules: Set<string> | string[]) {
}
}
}
for (const css of entryCSS?.css || []) {
inlinedStyles.add(`<link rel="stylesheet" href=${JSON.stringify(buildAssetsURL(css))} media="print" onload="this.media='all'; this.onload=null;">`)
}
return Array.from(inlinedStyles).join('')
}

Expand Down
7 changes: 5 additions & 2 deletions packages/nuxt/src/core/templates.ts
Expand Up @@ -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')
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/server.ts
Expand Up @@ -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')
}
}
})
Expand Down
3 changes: 1 addition & 2 deletions test/basic.test.ts
Expand Up @@ -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)')
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.