Skip to content

Commit 03eb446

Browse files
committed
Simplify the test case
1 parent 9dc91e2 commit 03eb446

File tree

5 files changed

+39
-45
lines changed

5 files changed

+39
-45
lines changed

test/development/app-dir/hmr-iframe/app/page1/page.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { subscribeToHMR } from './subscribeToHMR'
22
import { Component } from './Component'
33

4-
const RootPage = async ({ Component }: any) => {
4+
// The (unused) client component prop is crucial to reproduce the issue. It will
5+
// be serialized as a client reference in the props of this component, which
6+
// acts as the owner of the I/O inside subscribeToHMR, which is also serialized
7+
// as part of the async I/O sequence in page 2.
8+
const RootPage = async ({ Component }: { Component: React.ComponentType }) => {
59
await subscribeToHMR()
610

711
return (
@@ -13,6 +17,6 @@ const RootPage = async ({ Component }: any) => {
1317
)
1418
}
1519

16-
export default function Page() {
20+
export default function Page1() {
1721
return <RootPage Component={Component} />
1822
}

test/development/app-dir/hmr-iframe/app/page1/subscribeToHMR.ts

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
1-
if (!global._payload) {
2-
global._payload = {
3-
payload: null,
1+
declare global {
2+
var _testState: {
3+
initialized: boolean
4+
reload: boolean | Promise<void>
5+
}
6+
}
7+
8+
if (!globalThis._testState) {
9+
globalThis._testState = {
10+
initialized: false,
411
reload: false,
5-
ws: null,
612
}
713
}
814

915
export const subscribeToHMR = async () => {
10-
let cached = global._payload
11-
12-
if (cached.payload) {
13-
if (cached.reload === true) {
14-
let resolve: any
15-
16-
cached.reload = new Promise((res) => (resolve = res))
16+
const state = globalThis._testState
1717

18+
if (state.initialized) {
19+
if (state.reload === true) {
20+
let resolve: () => void
21+
state.reload = new Promise<void>((res) => (resolve = res))
1822
await new Promise((resolve) => setTimeout(resolve, 200))
19-
2023
resolve()
2124
}
22-
if (cached.reload instanceof Promise) {
23-
await cached.reload
25+
26+
if (state.reload instanceof Promise) {
27+
await state.reload
2428
}
2529

26-
return cached.payload
30+
return
2731
}
2832

29-
cached.payload = {}
30-
31-
const port = process.env.PORT || '3000'
32-
const hasHTTPS =
33-
process.env.USE_HTTPS === 'true' ||
34-
process.argv.includes('--experimental-https')
35-
const protocol = hasHTTPS ? 'wss' : 'ws'
36-
const path = '/_next/webpack-hmr'
37-
// The __NEXT_ASSET_PREFIX env variable is set for both assetPrefix and basePath (tested in Next.js 15.1.6)
38-
const prefix = process.env.__NEXT_ASSET_PREFIX ?? ''
39-
cached.ws = new WebSocket(
40-
process.env.PAYLOAD_HMR_URL_OVERRIDE ??
41-
`${protocol}://localhost:${port}${prefix}${path}`
33+
state.initialized = true
34+
35+
const ws = new WebSocket(
36+
`ws://localhost:${process.env.PORT}/_next/webpack-hmr`
4237
)
43-
cached.ws.onmessage = (event: any) => {
38+
39+
ws.onmessage = (event: any) => {
4440
if (typeof event.data === 'string') {
4541
const data = JSON.parse(event.data)
46-
if (
47-
// On Next.js 15, we need to check for data.action. On Next.js 16, we need to check for data.type.
48-
data.type === 'serverComponentChanges' ||
49-
data.action === 'serverComponentChanges'
50-
) {
51-
cached.reload = true
42+
if (data.type === 'serverComponentChanges') {
43+
state.reload = true
5244
}
5345
}
5446
}

test/development/app-dir/hmr-iframe/app/page2/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { subscribeToHMR } from '../page1/subscribeToHMR'
22

3-
export default async function Page() {
3+
export default async function Page2() {
44
await subscribeToHMR()
55

66
return (

test/development/app-dir/hmr-iframe/hmr-iframe.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ describe('hmr-iframe', () => {
1515
.innerText()
1616
).toEqual('content')
1717

18+
let cliOutputLength = next.cliOutput.length
19+
1820
await next.patchFile('app/page2/page.tsx', (content) =>
1921
content.replace('content', 'content-new')
2022
)
@@ -26,8 +28,9 @@ describe('hmr-iframe', () => {
2628
.innerText()
2729
).toEqual('content-new')
2830

29-
expect(next.cliOutput).not.toContain('Error')
30-
expect(next.cliOutput).not.toContain('Could not find the module')
31+
const cliOutput = next.cliOutput.slice(cliOutputLength)
32+
expect(cliOutput).not.toContain('Error')
33+
expect(cliOutput).not.toContain('Could not find the module')
3134

3235
await next.stop()
3336
await next.clean()

test/development/app-dir/hmr-iframe/next.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)