Skip to content

Commit

Permalink
remove ssr link injection and css flight
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jul 11, 2022
1 parent 94bd4e8 commit e48e739
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/next/build/webpack/plugins/flight-manifest-plugin.ts
Expand Up @@ -187,15 +187,15 @@ export class FlightManifestPlugin {
.filter((name) => name !== null)

// Get all CSS files imported from the module's dependencies.
const visitedCss = new Set()
const visitedModule = new Set()
const cssChunks: Set<string> = new Set()

function collectClientImportedCss(module: any) {
if (!module) return

const modRequest = module.userRequest
if (visitedCss.has(modRequest)) return
visitedCss.add(modRequest)
if (visitedModule.has(modRequest)) return
visitedModule.add(modRequest)

if (/\.css$/.test(modRequest)) {
// collect relative imported css chunks
Expand Down
4 changes: 3 additions & 1 deletion packages/next/client/app-index.tsx
Expand Up @@ -186,7 +186,9 @@ function useInitialServerResponse(cacheKey: string) {
buffer = buffer.slice(index + 1)
loadCssFromStreamData(line)
}
controller.enqueue(chunk)
if (!data.startsWith('CSS:')) {
controller.enqueue(chunk)
}
},
flush() {
loadCssFromStreamData(buffer)
Expand Down
36 changes: 3 additions & 33 deletions packages/next/server/app-render.tsx
Expand Up @@ -125,7 +125,6 @@ function useFlightResponse(
rscCache.set(id, entry)

let bootstrapped = false
let remainingFlightResponse = ''
const forwardReader = forwardStream.getReader()
const writer = writable.getWriter()
function process() {
Expand All @@ -145,40 +144,11 @@ function useFlightResponse(
writer.close()
} else {
const responsePartial = decodeText(value)
const css = responsePartial
.split('\n')
.map((partialLine) => {
const line = remainingFlightResponse + partialLine
remainingFlightResponse = ''

try {
const match = line.match(/^M\d+:(.+)/)
if (match) {
return JSON.parse(match[1])
.chunks.filter((chunkId: string) =>
chunkId.endsWith('.css')
)
.map(
(file: string) =>
`<link rel="stylesheet" href="/_next/${file}">`
)
.join('')
}
return ''
} catch (err) {
// The JSON is partial
remainingFlightResponse = line
return ''
}
})
.join('')

writer.write(
encodeText(
css +
`<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString(
JSON.stringify([1, id, responsePartial])
)})</script>`
`<script>(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString(
JSON.stringify([1, id, responsePartial])
)})</script>`
)
)
process()
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/app/app/dashboard/page.server.js
@@ -1,7 +1,7 @@
export default function DashboardPage(props) {
return (
<>
<p>hello from app/dashboard</p>
<p className="p">hello from app/dashboard</p>
<p className="green">this is green</p>
</>
)
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -393,6 +393,24 @@ describe('app dir', () => {
})

describe('css support', () => {
it('should support global css inside server component layouts', async () => {
const browser = await webdriver(next.url, '/dashboard')

// Should body text in red
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('.p')).color`
)
).toBe('rgb(255, 0, 0)')

// Should inject global css for .green selectors
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('.green')).color`
)
).toBe('rgb(0, 128, 0)')
})

it('should support css modules inside client layouts', async () => {
const browser = await webdriver(next.url, '/client-nested')

Expand Down

0 comments on commit e48e739

Please sign in to comment.