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

Remove the hide-fouc tags after hydration #38592

Merged
merged 3 commits into from Jul 13, 2022
Merged
Changes from 1 commit
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
30 changes: 11 additions & 19 deletions packages/next/client/app-index.tsx
Expand Up @@ -165,15 +165,11 @@ async function loadCss(cssChunkInfoJson: string) {
return Promise.resolve()
}

function createLoadFlightCssStream(callback?: () => Promise<void>) {
const cssLoadingPromises: Promise<any>[] = []
function createLoadFlightCssStream() {
const loadCssFromStreamData = (data: string) => {
if (data.startsWith('CSS')) {
const cssJson = data.slice(4).trim()
if (!loadedCss.has(cssJson)) {
loadedCss.add(cssJson)
cssLoadingPromises.push(loadCss(cssJson))
}
loadCss(cssJson)
}
}

Expand Down Expand Up @@ -204,19 +200,10 @@ function createLoadFlightCssStream(callback?: () => Promise<void>) {
},
})

if (process.env.NODE_ENV === 'development') {
Promise.all(cssLoadingPromises).then(() => {
// TODO: find better timing for css injection
setTimeout(() => {
callback?.()
})
})
}

return loadCssFromFlight
}

function useInitialServerResponse(cacheKey: string, onFlightCssLoaded: any) {
function useInitialServerResponse(cacheKey: string) {
const response = rscCache.get(cacheKey)
if (response) return response

Expand All @@ -227,7 +214,7 @@ function useInitialServerResponse(cacheKey: string, onFlightCssLoaded: any) {
})

const newResponse = createFromReadableStream(
readable.pipeThrough(createLoadFlightCssStream(onFlightCssLoaded))
readable.pipeThrough(createLoadFlightCssStream())
)

rscCache.set(cacheKey, newResponse)
Expand All @@ -239,12 +226,17 @@ function ServerRoot({
onFlightCssLoaded,
}: {
cacheKey: string
onFlightCssLoaded: any
onFlightCssLoaded: () => Promise<void>
}) {
React.useEffect(() => {
rscCache.delete(cacheKey)
})
const response = useInitialServerResponse(cacheKey, onFlightCssLoaded)
React.useEffect(() => {
if (process.env.NODE_ENV === 'development') {
onFlightCssLoaded?.()
}
}, [])
const response = useInitialServerResponse(cacheKey)
const root = response.readRoot()
return root
}
Expand Down