Unverified Commit df0e61b0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix app sc_client componet HMR server-side (#41510)

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)
parent fcac2e2b
...@@ -68,15 +68,33 @@ export class NextJsRequireCacheHotReloader implements WebpackPluginInstance { ...@@ -68,15 +68,33 @@ export class NextJsRequireCacheHotReloader implements WebpackPluginInstance {
) )
deleteCache(runtimeChunkPath) deleteCache(runtimeChunkPath)
}) })
let hasAppPath = false
// we need to make sure to clear all server entries from cache // we need to make sure to clear all server entries from cache
// since they can have a stale webpack-runtime cache // since they can have a stale webpack-runtime cache
// which needs to always be in-sync // which needs to always be in-sync
const entries = [...compilation.entries.keys()].filter( const entries = [...compilation.entries.keys()].filter((entry) => {
(entry) => const isAppPath = entry.toString().startsWith('app/')
entry.toString().startsWith('pages/') || hasAppPath = hasAppPath || isAppPath
entry.toString().startsWith('app/') 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) => { entries.forEach((page) => {
const outputPath = path.join( const outputPath = path.join(
......
...@@ -8,7 +8,6 @@ import type { FontLoaderManifest } from '../build/webpack/plugins/font-loader-ma ...@@ -8,7 +8,6 @@ import type { FontLoaderManifest } from '../build/webpack/plugins/font-loader-ma
import React, { experimental_use as use } from 'react' import React, { experimental_use as use } from 'react'
import { ParsedUrlQuery } from 'querystring' import { ParsedUrlQuery } from 'querystring'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { NextParsedUrlQuery } from './request-meta' import { NextParsedUrlQuery } from './request-meta'
import RenderResult from './render-result' import RenderResult from './render-result'
import { import {
...@@ -271,6 +270,9 @@ function useFlightResponse( ...@@ -271,6 +270,9 @@ function useFlightResponse(
if (flightResponseRef.current !== null) { if (flightResponseRef.current !== null) {
return flightResponseRef.current return flightResponseRef.current
} }
const {
createFromReadableStream,
} = require('next/dist/compiled/react-server-dom-webpack')
const [renderStream, forwardStream] = readableStreamTee(req) const [renderStream, forwardStream] = readableStreamTee(req)
const res = createFromReadableStream(renderStream, { const res = createFromReadableStream(renderStream, {
......
...@@ -1197,6 +1197,50 @@ describe('app dir', () => { ...@@ -1197,6 +1197,50 @@ describe('app dir', () => {
}) })
if (isDev) { 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 () => { it('should throw an error when getServerSideProps is used', async () => {
const pageFile = const pageFile =
'app/client-with-errors/get-server-side-props/page.js' 'app/client-with-errors/get-server-side-props/page.js'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment