Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix app sc_client componet HMR server-side #41510

Merged
merged 1 commit into from Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -261,6 +260,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