Unverified Commit 61b825be authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix hmr in multi-zone handling (#59307)

### What?
When running a
[multi-zone](https://github.com/vercel/next.js/tree/canary/examples/with-zones)
app in dev, app pages would infinitely reload

### Why?
The HMR upgrade request would fail and get caught into a retry loop. In
the multi-zone case, they fail because the upgrade request would be sent
again for a request that had already been upgraded. This resulted in a
"server.handleUpgrade() was called more than once with the same socket"
error, causing the upgrade request to fail.

Every time a retry occurred, the page would trigger a full refresh since
certain HMR errors cause the browser to reload.

### How?
This ensures the upgrade handler only responds to requests that match
the configured basePath.

Closes NEXT-1797
Fixes #59161
Fixes #56615
Fixes #54454
parent 1f6defd4
...@@ -582,8 +582,14 @@ export async function initialize(opts: { ...@@ -582,8 +582,14 @@ export async function initialize(opts: {
// console.error(_err); // console.error(_err);
}) })
if (opts.dev && developmentBundler) { if (opts.dev && developmentBundler && req.url) {
if (req.url?.includes(`/_next/webpack-hmr`)) { const isHMRRequest = req.url.includes('/_next/webpack-hmr')
// only handle HMR requests if the basePath in the request
// matches the basePath for the handler responding to the request
const isRequestForCurrentBasepath =
!config.basePath || pathHasPrefix(req.url, config.basePath)
if (isHMRRequest && isRequestForCurrentBasepath) {
return developmentBundler.hotReloader.onHMR(req, socket, head) return developmentBundler.hotReloader.onHMR(req, socket, head)
} }
} }
......
export default function Page() { export default function Page() {
return <p>hello from first app</p> // this variable is edited by the test to verify HMR
const editedContent = ''
return (
<>
<p>hello from first app</p>
<div>{editedContent}</div>
<div id="now">{Date.now()}</div>
</>
)
} }
export default function Page() { export default function Page() {
return <p>hello from second app</p> // this variable is edited by the test to verify HMR
const editedContent = ''
return (
<>
<p>hello from second app</p>
<div>{editedContent}</div>
<div id="now">{Date.now()}</div>
</>
)
} }
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { check, waitFor } from 'next-test-utils'
import path from 'path' import path from 'path'
createNextDescribe( createNextDescribe(
...@@ -14,7 +15,7 @@ createNextDescribe( ...@@ -14,7 +15,7 @@ createNextDescribe(
}, },
}, },
}, },
({ next }) => { ({ next, isNextDev }) => {
it.each([ it.each([
{ pathname: '/first', content: ['hello from first app'] }, { pathname: '/first', content: ['hello from first app'] },
{ pathname: '/second', content: ['hello from second app'] }, { pathname: '/second', content: ['hello from second app'] },
...@@ -45,5 +46,45 @@ createNextDescribe( ...@@ -45,5 +46,45 @@ createNextDescribe(
} }
} }
) )
if (isNextDev) {
async function runHMRTest(app: string) {
const browser = await next.browser(`/${app}`)
expect(await browser.elementByCss('body').text()).toContain(
`hello from ${app} app`
)
const initialTimestamp = await browser.elementById('now').text()
expect(await browser.elementByCss('body').text()).not.toContain(
'hmr content'
)
await waitFor(1000)
// verify that the page isn't unexpectedly reloading in the background
const newTimestamp = await browser.elementById('now').text()
expect(newTimestamp).toBe(initialTimestamp)
// trigger HMR
const filePath = `apps/${app}/pages/index.tsx`
const content = await next.readFile(filePath)
const patchedContent = content.replace(
`const editedContent = ''`,
`const editedContent = 'hmr content'`
)
await next.patchFile(filePath, patchedContent)
await check(() => browser.elementByCss('body').text(), /hmr content/)
// restore original content
await next.patchFile(filePath, content)
}
it('should support HMR in both apps', async () => {
await runHMRTest('first')
await runHMRTest('second')
})
}
} }
) )
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