Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jul 12, 2022
1 parent abec463 commit 0741f9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/next/client/app-index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ function RSCComponent({ onFlightCssLoaded }: { onFlightCssLoaded: any }) {
)
}

export async function hydrate(opts: {
export async function hydrate(opts?: {
onFlightCssLoaded?: () => Promise<void>
}) {
renderReactElement(appElement!, () => (
<React.StrictMode>
<Root>
<RSCComponent onFlightCssLoaded={opts.onFlightCssLoaded} />
<RSCComponent onFlightCssLoaded={opts?.onFlightCssLoaded} />
</Root>
</React.StrictMode>
))
Expand Down
16 changes: 10 additions & 6 deletions packages/next/server/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,19 @@ function getSegmentParam(segment: string): {
return null
}

function getCssFlightData(ComponentMod: any, serverComponentManifest: any) {
function getCssFlightData(
ComponentMod: any,
serverComponentManifest: any,
dev?: boolean
) {
const importedServerCSSFiles: string[] =
ComponentMod.__client__?.__next_rsc_css__ || []

const cssFiles = importedServerCSSFiles.map(
(css) => serverComponentManifest[css].default
)
if (process.env.NODE_ENV === 'development') {

if (dev) {
return cssFiles.map((css) => `CSS:${JSON.stringify(css)}\n`).join('')
}

Expand All @@ -344,9 +349,7 @@ function getCssFlightData(ComponentMod: any, serverComponentManifest: any) {
return res
}, new Set())

return cssSet.size
? `CSS:${JSON.stringify({ chunks: [Array.from(cssSet)] })}\n`
: ''
return cssSet.size ? `CSS:${JSON.stringify({ chunks: [...cssSet] })}\n` : ''
}

export async function renderToHTML(
Expand Down Expand Up @@ -765,7 +768,8 @@ export async function renderToHTML(

const cssFlightData = getCssFlightData(
ComponentMod,
serverComponentManifest
serverComponentManifest,
renderOpts.dev
)
const flightData: FlightData = [
// TODO: change walk to output without ''
Expand Down
15 changes: 8 additions & 7 deletions packages/next/server/node-web-streams-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,19 @@ export function createDevScriptTransformStream(): TransformStream<
Uint8Array,
Uint8Array
> {
const headClosedTag = '</head>'
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)

if (content.includes('</head>')) {
if (content.includes(headClosedTag)) {
const injectedContent = content.replaceAll(
'</head>',
`<style data-next-hide-fouc>body{display:none}</style>
<noscript data-next-hide-fouc>
<style>body{display:block}</style>
</noscript>
</head>`
headClosedTag,
foucTags + headClosedTag
)
controller.enqueue(encodeText(injectedContent))
} else {
Expand Down

0 comments on commit 0741f9c

Please sign in to comment.