Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failed to hydrate error with global CSS #38626

Merged
merged 3 commits into from Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions packages/next/client/components/layout-router.client.tsx
Expand Up @@ -235,7 +235,7 @@ export default function OuterLayoutRouter({
childProp: ChildProp
loading: React.ReactNode | undefined
}) {
const { childNodes, tree, url, stylesheets } = useContext(AppTreeContext)
const { childNodes, tree, url } = useContext(AppTreeContext)

let childNodesForParallelRouter = childNodes.get(parallelRouterKey)
if (!childNodesForParallelRouter) {
Expand All @@ -256,11 +256,11 @@ export default function OuterLayoutRouter({

return (
<>
{stylesheets
{/* {stylesheets
? stylesheets.map((href) => (
<link rel="stylesheet" href={`/_next/${href}`} key={href} />
))
: null}
: null} */}
{preservedSegments.map((preservedSegment) => {
return (
<LoadingBoundary loading={loading} key={preservedSegment}>
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/app-render.tsx
Expand Up @@ -945,6 +945,7 @@ export async function renderToHTML(
dataStream: serverComponentsInlinedTransformStream?.readable,
generateStaticHTML: generateStaticHTML || !hasConcurrentFeatures,
flushEffectHandler,
initialStylesheets,
})
}

Expand Down
30 changes: 18 additions & 12 deletions packages/next/server/node-web-streams-helper.ts
Expand Up @@ -136,25 +136,18 @@ export function createFlushEffectStream(
})
}

export function createDevScriptTransformStream(): TransformStream<
Uint8Array,
Uint8Array
> {
export function createHeadInjectionTransformStream(
inject: string
): TransformStream<Uint8Array, Uint8Array> {
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({
transform(chunk, controller) {
const content = decodeText(chunk)
let index
if (!injected && (index = content.indexOf('</head')) !== -1) {
injected = true
// head content + fouc tags + </head
const injectedContent =
content.slice(0, index) + foucTags + content.slice(index)

content.slice(0, index) + inject + content.slice(index)
controller.enqueue(encodeText(injectedContent))
} else {
controller.enqueue(chunk)
Expand Down Expand Up @@ -183,12 +176,14 @@ export async function continueFromInitialStream(
dataStream,
generateStaticHTML,
flushEffectHandler,
initialStylesheets,
}: {
dev?: boolean
suffix?: string
dataStream?: ReadableStream<Uint8Array>
generateStaticHTML: boolean
flushEffectHandler?: () => string
initialStylesheets?: string[]
}
): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>'
Expand All @@ -204,7 +199,18 @@ export async function continueFromInitialStream(
suffixUnclosed != null ? createDeferredSuffixStream(suffixUnclosed) : null,
dataStream ? createInlineDataStream(dataStream) : 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)

return transforms.reduce(
Expand Down