diff --git a/packages/next/client/app-index.tsx b/packages/next/client/app-index.tsx index 95525aced666..52254f9663e9 100644 --- a/packages/next/client/app-index.tsx +++ b/packages/next/client/app-index.tsx @@ -76,7 +76,6 @@ const getCacheKey = () => { } const encoder = new TextEncoder() -const loadedCss: Set = new Set() let initialServerDataBuffer: string[] | undefined = undefined let initialServerDataWriter: ReadableStreamDefaultController | undefined = @@ -165,15 +164,11 @@ async function loadCss(cssChunkInfoJson: string) { return Promise.resolve() } -function createLoadFlightCssStream(callback?: () => Promise) { - const cssLoadingPromises: Promise[] = [] +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) } } @@ -204,19 +199,10 @@ function createLoadFlightCssStream(callback?: () => Promise) { }, }) - 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 @@ -227,7 +213,7 @@ function useInitialServerResponse(cacheKey: string, onFlightCssLoaded: any) { }) const newResponse = createFromReadableStream( - readable.pipeThrough(createLoadFlightCssStream(onFlightCssLoaded)) + readable.pipeThrough(createLoadFlightCssStream()) ) rscCache.set(cacheKey, newResponse) @@ -239,12 +225,17 @@ function ServerRoot({ onFlightCssLoaded, }: { cacheKey: string - onFlightCssLoaded: any + onFlightCssLoaded: () => Promise }) { React.useEffect(() => { rscCache.delete(cacheKey) }) - const response = useInitialServerResponse(cacheKey, onFlightCssLoaded) + React.useEffect(() => { + if (process.env.NODE_ENV === 'development') { + onFlightCssLoaded?.() + } + }, [onFlightCssLoaded]) + const response = useInitialServerResponse(cacheKey) const root = response.readRoot() return root }