Commit 228f69ba authored by Jimmy Lai's avatar Jimmy Lai

refactor: simplify inlineCss conditional logic and remove dead code

- Restructure render-css-resource.tsx to use clearer early returns
- Extract isRSCRequest to local variable for readability
- Remove unreachable fallback <style> render in 'shared' mode
- Initialize rootLayoutCSSPaths at creation instead of lazily
- Make rootLayoutCSSPaths required in CollectedInlineCss type
- Improve JSDoc to explain CSS duplication with inlineCss: true
parent d39f0bc2
...@@ -2535,6 +2535,7 @@ async function renderToStream( ...@@ -2535,6 +2535,7 @@ async function renderToStream(
// and RSC payload (serialized in <script> tags). // and RSC payload (serialized in <script> tags).
const collectedInlineCss: CollectedInlineCss = { const collectedInlineCss: CollectedInlineCss = {
styles: [], styles: [],
rootLayoutCSSPaths: new Set(),
inlineCssMode: experimental.inlineCss, inlineCssMode: experimental.inlineCss,
} }
...@@ -4152,6 +4153,7 @@ async function prerenderToStream( ...@@ -4152,6 +4153,7 @@ async function prerenderToStream(
// and RSC payload (serialized in <script> tags). // and RSC payload (serialized in <script> tags).
const collectedInlineCss: CollectedInlineCss = { const collectedInlineCss: CollectedInlineCss = {
styles: [], styles: [],
rootLayoutCSSPaths: new Set(),
inlineCssMode: experimental.inlineCss, inlineCssMode: experimental.inlineCss,
} }
......
...@@ -40,9 +40,6 @@ export function getLayerAssets({ ...@@ -40,9 +40,6 @@ export function getLayerAssets({
// Track root layout CSS paths for inlineCss: 'shared' mode // Track root layout CSS paths for inlineCss: 'shared' mode
// Root layout CSS is guaranteed to be shared across all pages // Root layout CSS is guaranteed to be shared across all pages
if (isRootLayout && collectedInlineCss && styleTags.length > 0) { if (isRootLayout && collectedInlineCss && styleTags.length > 0) {
if (!collectedInlineCss.rootLayoutCSSPaths) {
collectedInlineCss.rootLayoutCSSPaths = new Set()
}
for (const css of styleTags) { for (const css of styleTags) {
collectedInlineCss.rootLayoutCSSPaths.add(css.path) collectedInlineCss.rootLayoutCSSPaths.add(css.path)
} }
......
...@@ -11,7 +11,9 @@ import type { PreloadCallbacks, CollectedInlineCss } from './types' ...@@ -11,7 +11,9 @@ import type { PreloadCallbacks, CollectedInlineCss } from './types'
* *
* The inlineCssMode determines the inlining behavior: * The inlineCssMode determines the inlining behavior:
* - `false` or `undefined`: No CSS inlining (render as <link> tags) * - `false` or `undefined`: No CSS inlining (render as <link> tags)
* - `true`: Inline ALL CSS (note: CSS will be duplicated in HTML and RSC payload) * - `true`: Inline ALL CSS. Note: This causes CSS duplication because styles
* appear both in initial HTML <style> tags AND in the RSC payload's serialized
* <script> content. The client then re-injects them, causing double styles.
* - `'shared'`: Only inline root layout CSS (safer for client navigations). * - `'shared'`: Only inline root layout CSS (safer for client navigations).
* In this mode, collectedInlineCss is used to inject CSS via ServerInsertedHTML * In this mode, collectedInlineCss is used to inject CSS via ServerInsertedHTML
* to avoid duplicating CSS in both HTML and RSC payload. * to avoid duplicating CSS in both HTML and RSC payload.
...@@ -54,47 +56,28 @@ export function renderCssResource( ...@@ -54,47 +56,28 @@ export function renderCssResource(
// Check if this CSS is from the root layout (shared across all pages) // Check if this CSS is from the root layout (shared across all pages)
const isRootLayoutCSS = rootLayoutCSSPaths?.has(entryCssFile.path) const isRootLayoutCSS = rootLayoutCSSPaths?.has(entryCssFile.path)
const isRSCRequest = ctx.parsedRequestHeaders.isRSCRequest
// In 'shared' mode, handle root layout CSS specially: // Handle inlineCss: 'shared' mode
// - For RSC requests: skip root layout CSS entirely (client already has it from initial HTML) if (inlineCssMode === 'shared' && isRootLayoutCSS) {
// - For non-RSC requests: inline only root layout CSS // RSC requests: skip root layout CSS (client already has it from initial HTML)
if (inlineCssMode === 'shared') { if (isRSCRequest) {
if (ctx.parsedRequestHeaders.isRSCRequest) { return null
// For RSC/navigation requests, skip root layout CSS
// (client already has it from the initial HTML load)
if (isRootLayoutCSS) {
return null
}
// Page-specific CSS should still be included in RSC payload
// Fall through to render as <link> tag
} else if (isRootLayoutCSS && entryCssFile.inlined) {
// For initial HTML requests, inline only root layout CSS
if (collectedInlineCss) {
collectedInlineCss.styles.push({
href: fullHref,
content: entryCssFile.content!,
precedence: precedence,
nonce: ctx.nonce,
})
return null
}
return createElement(
'style',
{
key: index,
nonce: ctx.nonce,
precedence: precedence,
href: fullHref,
},
entryCssFile.content
)
} }
// For page-specific CSS in 'shared' mode, fall through to render as <link> tag // Initial HTML: collect for ServerInsertedHTML injection
} else if ( if (entryCssFile.inlined) {
entryCssFile.inlined && collectedInlineCss!.styles.push({
!ctx.parsedRequestHeaders.isRSCRequest href: fullHref,
) { content: entryCssFile.content!,
// inlineCss: true mode - inline ALL CSS for non-RSC requests precedence: precedence,
nonce: ctx.nonce,
})
return null
}
}
// Handle inlineCss: true mode - inline ALL CSS for non-RSC requests
if (inlineCssMode === true && entryCssFile.inlined && !isRSCRequest) {
return createElement( return createElement(
'style', 'style',
{ {
...@@ -107,6 +90,8 @@ export function renderCssResource( ...@@ -107,6 +90,8 @@ export function renderCssResource(
) )
} }
// Default: render as <link> tag
preloadCallbacks?.push(() => { preloadCallbacks?.push(() => {
ctx.componentMod.preloadStyle( ctx.componentMod.preloadStyle(
fullHref, fullHref,
......
...@@ -238,7 +238,7 @@ export interface CollectedInlineCss { ...@@ -238,7 +238,7 @@ export interface CollectedInlineCss {
* Used when `inlineCss: 'shared'` to only inline root layout CSS * Used when `inlineCss: 'shared'` to only inline root layout CSS
* (which is guaranteed to be shared across all pages). * (which is guaranteed to be shared across all pages).
*/ */
rootLayoutCSSPaths?: Set<string> rootLayoutCSSPaths: Set<string>
/** /**
* The inlineCss mode from config. * The inlineCss mode from config.
* - `false` or `undefined`: No CSS inlining * - `false` or `undefined`: No CSS inlining
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment