Skip to content

Commit

Permalink
keep css module id in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jul 12, 2022
1 parent e3f7179 commit 7063105
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
32 changes: 18 additions & 14 deletions packages/next/client/app-index.tsx
Expand Up @@ -169,31 +169,37 @@ function createLoadFlightCssStream(callback?: () => Promise<void>) {
const loadCssFromStreamData = (data: string) => {
if (data.startsWith('CSS')) {
const cssJson = data.slice(4).trim()
if (!loadedCss.has(cssJson)) cssLoadingPromises.push(loadCss(cssJson))
if (!loadedCss.has(cssJson)) {
loadedCss.add(cssJson)
cssLoadingPromises.push(loadCss(cssJson))
}
}
}

// TODO-APP: Refine the buffering code here to make it more correct.
let buffer = ''
const loadCssFromFlight = new TransformStream({
transform(chunk, controller) {
const process = (buf: string) => {
if (buf) {
if (buf.startsWith('CSS:')) {
loadCssFromStreamData(buf)
} else {
controller.enqueue(new TextEncoder().encode(buf))
}
}
}

const data = new TextDecoder().decode(chunk)
buffer += data
let index
while ((index = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, index + 1)
buffer = buffer.slice(index + 1)
if (line.startsWith('CSS:')) {
loadCssFromStreamData(line)
} else {
controller.enqueue(new TextEncoder().encode(line))
}
}

if (buffer && !buffer.startsWith('CSS:')) {
controller.enqueue(new TextEncoder().encode(buffer))
buffer = ''
process(line)
}
process(buffer)
buffer = ''
},
})

Expand Down Expand Up @@ -262,9 +268,7 @@ function RSCComponent(props: any) {
return <ServerRoot {...props} cacheKey={cacheKey} />
}

export async function hydrate(opts?: {
onFlightCssLoaded?: () => Promise<void>
}) {
export function hydrate(opts?: { onFlightCssLoaded?: () => Promise<void> }) {
renderReactElement(appElement!, () => (
<React.StrictMode>
<Root>
Expand Down
26 changes: 13 additions & 13 deletions packages/next/client/components/app-router.client.tsx
Expand Up @@ -49,26 +49,26 @@ function fetchFlight(url: URL, flightRouterStateData: string): ReadableStream {
let buffer = ''
const loadCssFromFlight = new TransformStream({
transform(chunk, controller) {
const process = (buf: string) => {
if (buf) {
if (buf.startsWith('CSS:')) {
loadCssFromStreamData(buf)
} else {
controller.enqueue(new TextEncoder().encode(buf))
}
}
}

const data = new TextDecoder().decode(chunk)
buffer += data
let index
while ((index = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, index + 1)
buffer = buffer.slice(index + 1)

if (line.startsWith('CSS:')) {
loadCssFromStreamData(line)
} else {
controller.enqueue(new TextEncoder().encode(line))
}
}
if (buffer && !buffer.startsWith('CSS:')) {
controller.enqueue(new TextEncoder().encode(buffer))
buffer = ''
process(line)
}
},
flush() {
loadCssFromStreamData(buffer)
process(buffer)
buffer = ''
},
})

Expand Down
20 changes: 17 additions & 3 deletions packages/next/server/app-render.tsx
Expand Up @@ -191,7 +191,11 @@ function createServerComponentRenderer(
globalThis.__next_chunk_load__ = () => Promise.resolve()
}

const cssFlightData = getCssFlightData(ComponentMod, serverComponentManifest)
const cssFlightData = getCssFlightData(
ComponentMod,
serverComponentManifest,
dev
)

let RSCStream: ReadableStream<Uint8Array>
const createRSCStream = () => {
Expand Down Expand Up @@ -346,14 +350,23 @@ function getCSSInlinedLinkTags(
)
}

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 (dev) {
// Keep `id` in dev mode css flight to require the css module
return cssFiles.map((css) => `CSS:${JSON.stringify(css)}`).join('\n') + '\n'
}

// Multiple css chunks could be merged into one by mini-css-extract-plugin,
// we use a set here to dedupe the css chunks in production.
const cssSet: Set<string> = cssFiles.reduce((res, css) => {
Expand Down Expand Up @@ -781,7 +794,8 @@ export async function renderToHTML(

const cssFlightData = getCssFlightData(
ComponentMod,
serverComponentManifest
serverComponentManifest,
dev
)
const flightData: FlightData = [
// TODO-APP: change walk to output without ''
Expand Down
4 changes: 2 additions & 2 deletions packages/next/server/node-web-streams-helper.ts
Expand Up @@ -231,9 +231,9 @@ export function createPrefixStream(
let prefixFlushed = false
return new TransformStream({
transform(chunk, controller) {
if (!prefixFlushed && prefix) {
prefixFlushed = true
if (!prefixFlushed) {
controller.enqueue(encodeText(prefix))
prefixFlushed = true
}
controller.enqueue(chunk)
},
Expand Down

0 comments on commit 7063105

Please sign in to comment.