Skip to content

Commit

Permalink
Fix app sc_client componet HMR server-side (vercel#41510)
Browse files Browse the repository at this point in the history
This ensures we properly clear the `sc_client` component cache from
`react-server-dom-webpack` in development so that HMR works properly
after a refresh.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

Fixes: [slack
thread](https://vercel.slack.com/archives/C043ANYDB24/p1666051202574509)
  • Loading branch information
ijjk authored and Kikobeats committed Oct 24, 2022
1 parent 20999e9 commit ee50d44
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
Expand Up @@ -68,15 +68,33 @@ export class NextJsRequireCacheHotReloader implements WebpackPluginInstance {
)
deleteCache(runtimeChunkPath)
})
let hasAppPath = false

// we need to make sure to clear all server entries from cache
// since they can have a stale webpack-runtime cache
// which needs to always be in-sync
const entries = [...compilation.entries.keys()].filter(
(entry) =>
entry.toString().startsWith('pages/') ||
entry.toString().startsWith('app/')
)
const entries = [...compilation.entries.keys()].filter((entry) => {
const isAppPath = entry.toString().startsWith('app/')
hasAppPath = hasAppPath || isAppPath
return entry.toString().startsWith('pages/') || isAppPath
})

if (hasAppPath) {
// ensure we reset the cache for sc_server components
// loaded via react-server-dom-webpack
const reactWebpackModId = require.resolve(
'next/dist/compiled/react-server-dom-webpack'
)
const reactWebpackMod = require.cache[reactWebpackModId]

if (reactWebpackMod) {
for (const child of reactWebpackMod.children) {
child.parent = null
delete require.cache[child.id]
}
}
delete require.cache[reactWebpackModId]
}

entries.forEach((page) => {
const outputPath = path.join(
Expand Down
4 changes: 3 additions & 1 deletion packages/next/server/app-render.tsx
Expand Up @@ -8,7 +8,6 @@ import type { FontLoaderManifest } from '../build/webpack/plugins/font-loader-ma
import React, { experimental_use as use } from 'react'

import { ParsedUrlQuery } from 'querystring'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { NextParsedUrlQuery } from './request-meta'
import RenderResult from './render-result'
import {
Expand Down Expand Up @@ -271,6 +270,9 @@ function useFlightResponse(
if (flightResponseRef.current !== null) {
return flightResponseRef.current
}
const {
createFromReadableStream,
} = require('next/dist/compiled/react-server-dom-webpack')

const [renderStream, forwardStream] = readableStreamTee(req)
const res = createFromReadableStream(renderStream, {
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -1197,6 +1197,50 @@ describe('app dir', () => {
})

if (isDev) {
it('should HMR correctly for client component', async () => {
const filePath = 'app/client-component-route/page.js'
const origContent = await next.readFile(filePath)

try {
const browser = await webdriver(next.url, '/client-component-route')

const ssrInitial = await renderViaHTTP(
next.url,
'/client-component-route'
)

expect(ssrInitial).toContain(
'hello from app/client-component-route'
)

expect(await browser.elementByCss('p').text()).toContain(
'hello from app/client-component-route'
)

await next.patchFile(
filePath,
origContent.replace('hello from', 'swapped from')
)

await check(() => browser.elementByCss('p').text(), /swapped from/)

const ssrUpdated = await renderViaHTTP(
next.url,
'/client-component-route'
)
expect(ssrUpdated).toContain('swapped from')

await next.patchFile(filePath, origContent)

await check(() => browser.elementByCss('p').text(), /hello from/)
expect(
await renderViaHTTP(next.url, '/client-component-route')
).toContain('hello from')
} finally {
await next.patchFile(filePath, origContent)
}
})

it('should throw an error when getServerSideProps is used', async () => {
const pageFile =
'app/client-with-errors/get-server-side-props/page.js'
Expand Down

0 comments on commit ee50d44

Please sign in to comment.