@@ -6,7 +6,6 @@ import React from 'react'
66import { ParsedUrlQuery , stringify as stringifyQuery } from 'querystring'
77import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
88import { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
9- import { StyleRegistry , createStyleRegistry } from 'styled-jsx'
109import { NextParsedUrlQuery } from './request-meta'
1110import RenderResult from './render-result'
1211import {
@@ -23,6 +22,7 @@ import { htmlEscapeJsonString } from './htmlescape'
2322import { shouldUseReactRoot , stripInternalQueries } from './utils'
2423import { NextApiRequestCookies } from './api-utils'
2524import { matchSegment } from '../client/components/match-segments'
25+ import { FlushEffectsContext } from '../client/components/hooks-client'
2626
2727// this needs to be required lazily so that `next-server` can set
2828// the env before we require
@@ -985,18 +985,26 @@ export async function renderToHTMLOrFlight(
985985 }
986986 )
987987
988- /**
989- * Style registry for styled-jsx
990- */
991- const jsxStyleRegistry = createStyleRegistry ( )
988+ let flushEffectsHandler : ( ( ) => React . ReactNode ) | null = null
989+ function FlushEffects ( { children } : { children : JSX . Element } ) {
990+ // Reset flushEffectsHandler on each render
991+ flushEffectsHandler = null
992+ const setFlushEffectsHandler = React . useCallback (
993+ ( handler : ( ) => React . ReactNode ) => {
994+ if ( flushEffectsHandler )
995+ throw new Error (
996+ 'The `useFlushEffects` hook cannot be used more than once.'
997+ )
998+ flushEffectsHandler = handler
999+ } ,
1000+ [ ]
1001+ )
9921002
993- /**
994- * styled-jsx styles as React Component
995- */
996- const styledJsxFlushEffect = ( ) : React . ReactNode => {
997- const styles = jsxStyleRegistry . styles ( )
998- jsxStyleRegistry . flush ( )
999- return < > { styles } </ >
1003+ return (
1004+ < FlushEffectsContext . Provider value = { setFlushEffectsHandler } >
1005+ { children }
1006+ </ FlushEffectsContext . Provider >
1007+ )
10001008 }
10011009
10021010 /**
@@ -1015,11 +1023,18 @@ export async function renderToHTMLOrFlight(
10151023 const generateStaticHTML = supportsDynamicHTML !== true
10161024 const bodyResult = async ( ) => {
10171025 const content = (
1018- < StyleRegistry registry = { jsxStyleRegistry } >
1026+ < FlushEffects >
10191027 < ServerComponentsRenderer />
1020- </ StyleRegistry >
1028+ </ FlushEffects >
10211029 )
10221030
1031+ const flushEffectHandler = ( ) : string => {
1032+ const flushed = ReactDOMServer . renderToString (
1033+ < > { flushEffectsHandler && flushEffectsHandler ( ) } </ >
1034+ )
1035+ return flushed
1036+ }
1037+
10231038 const renderStream = await renderToInitialStream ( {
10241039 ReactDOMServer,
10251040 element : content ,
@@ -1031,17 +1046,13 @@ export async function renderToHTMLOrFlight(
10311046 } ,
10321047 } )
10331048
1034- const flushEffectHandler = ( ) : string => {
1035- const flushed = ReactDOMServer . renderToString ( styledJsxFlushEffect ( ) )
1036- return flushed
1037- }
1038-
10391049 const hasConcurrentFeatures = ! ! runtime
10401050
10411051 return await continueFromInitialStream ( renderStream , {
10421052 dataStream : serverComponentsInlinedTransformStream ?. readable ,
10431053 generateStaticHTML : generateStaticHTML || ! hasConcurrentFeatures ,
10441054 flushEffectHandler,
1055+ flushEffectsToHead : true ,
10451056 initialStylesheets,
10461057 } )
10471058 }
0 commit comments