Unverified Commit 8a2bb49d authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix failed to hydrate error with global CSS (#38626)

Temporarily we need to ensure that `<link>` tags are injected to `<head>` in the stream.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 2473280f
...@@ -235,7 +235,7 @@ export default function OuterLayoutRouter({ ...@@ -235,7 +235,7 @@ export default function OuterLayoutRouter({
childProp: ChildProp childProp: ChildProp
loading: React.ReactNode | undefined loading: React.ReactNode | undefined
}) { }) {
const { childNodes, tree, url, stylesheets } = useContext(AppTreeContext) const { childNodes, tree, url } = useContext(AppTreeContext)
let childNodesForParallelRouter = childNodes.get(parallelRouterKey) let childNodesForParallelRouter = childNodes.get(parallelRouterKey)
if (!childNodesForParallelRouter) { if (!childNodesForParallelRouter) {
...@@ -256,11 +256,11 @@ export default function OuterLayoutRouter({ ...@@ -256,11 +256,11 @@ export default function OuterLayoutRouter({
return ( return (
<> <>
{stylesheets {/* {stylesheets
? stylesheets.map((href) => ( ? stylesheets.map((href) => (
<link rel="stylesheet" href={`/_next/${href}`} key={href} /> <link rel="stylesheet" href={`/_next/${href}`} key={href} />
)) ))
: null} : null} */}
{preservedSegments.map((preservedSegment) => { {preservedSegments.map((preservedSegment) => {
return ( return (
<LoadingBoundary loading={loading} key={preservedSegment}> <LoadingBoundary loading={loading} key={preservedSegment}>
......
...@@ -946,6 +946,7 @@ export async function renderToHTML( ...@@ -946,6 +946,7 @@ export async function renderToHTML(
dataStream: serverComponentsInlinedTransformStream?.readable, dataStream: serverComponentsInlinedTransformStream?.readable,
generateStaticHTML: generateStaticHTML || !hasConcurrentFeatures, generateStaticHTML: generateStaticHTML || !hasConcurrentFeatures,
flushEffectHandler, flushEffectHandler,
initialStylesheets,
}) })
} }
......
...@@ -136,25 +136,18 @@ export function createFlushEffectStream( ...@@ -136,25 +136,18 @@ export function createFlushEffectStream(
}) })
} }
export function createDevScriptTransformStream(): TransformStream< export function createHeadInjectionTransformStream(
Uint8Array, inject: string
Uint8Array ): TransformStream<Uint8Array, Uint8Array> {
> {
let injected = false let injected = false
const foucTags = `<style data-next-hide-fouc>body{display:none}</style>
<noscript data-next-hide-fouc>
<style>body{display:block}</style>
</noscript>`
return new TransformStream({ return new TransformStream({
transform(chunk, controller) { transform(chunk, controller) {
const content = decodeText(chunk) const content = decodeText(chunk)
let index let index
if (!injected && (index = content.indexOf('</head')) !== -1) { if (!injected && (index = content.indexOf('</head')) !== -1) {
injected = true injected = true
// head content + fouc tags + </head
const injectedContent = const injectedContent =
content.slice(0, index) + foucTags + content.slice(index) content.slice(0, index) + inject + content.slice(index)
controller.enqueue(encodeText(injectedContent)) controller.enqueue(encodeText(injectedContent))
} else { } else {
controller.enqueue(chunk) controller.enqueue(chunk)
...@@ -183,12 +176,14 @@ export async function continueFromInitialStream( ...@@ -183,12 +176,14 @@ export async function continueFromInitialStream(
dataStream, dataStream,
generateStaticHTML, generateStaticHTML,
flushEffectHandler, flushEffectHandler,
initialStylesheets,
}: { }: {
dev?: boolean dev?: boolean
suffix?: string suffix?: string
dataStream?: ReadableStream<Uint8Array> dataStream?: ReadableStream<Uint8Array>
generateStaticHTML: boolean generateStaticHTML: boolean
flushEffectHandler?: () => string flushEffectHandler?: () => string
initialStylesheets?: string[]
} }
): Promise<ReadableStream<Uint8Array>> { ): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>' const closeTag = '</body></html>'
...@@ -204,7 +199,18 @@ export async function continueFromInitialStream( ...@@ -204,7 +199,18 @@ export async function continueFromInitialStream(
suffixUnclosed != null ? createDeferredSuffixStream(suffixUnclosed) : null, suffixUnclosed != null ? createDeferredSuffixStream(suffixUnclosed) : null,
dataStream ? createInlineDataStream(dataStream) : null, dataStream ? createInlineDataStream(dataStream) : null,
suffixUnclosed != null ? createSuffixStream(closeTag) : null, suffixUnclosed != null ? createSuffixStream(closeTag) : null,
dev ? createDevScriptTransformStream() : null, createHeadInjectionTransformStream(
dev
? `<style data-next-hide-fouc>body{display:none}</style>
<noscript data-next-hide-fouc>
<style>body{display:block}</style>
</noscript>`
: initialStylesheets
? initialStylesheets
.map((href) => `<link rel="stylesheet" href="/_next/${href}">`)
.join('')
: ''
),
].filter(nonNullable) ].filter(nonNullable)
return transforms.reduce( return transforms.reduce(
......
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