Skip to content

Commit

Permalink
fix: HMR not works when assetPrefix set to URL (#68622)
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi authored Aug 8, 2024
1 parent 604e68a commit 01a3f98
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,16 @@ export default async function loadConfig(
}
}

if (
phase === PHASE_DEVELOPMENT_SERVER &&
URL.canParse(userConfig.assetPrefix ?? '')
) {
curLog.warn(
`Absolute URL assetPrefix "${userConfig.assetPrefix}" may disrupt development HMR.\n` +
'See more info here https://nextjs.org/docs/app/api-reference/next-config-js/assetPrefix'
)
}

if (userConfig.target && userConfig.target !== 'server') {
throw new Error(
`The "target" property is no longer supported in ${configFileName}.\n` +
Expand Down
8 changes: 8 additions & 0 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,15 @@ export async function initialize(opts: {
// assetPrefix overrides basePath for HMR path
if (assetPrefix) {
hmrPrefix = normalizedAssetPrefix(assetPrefix)

if (URL.canParse(hmrPrefix)) {
// remove trailing slash from pathname
// return empty string if pathname is '/'
// to avoid conflicts with '/_next' below
hmrPrefix = new URL(hmrPrefix).pathname.replace(/\/$/, '')
}
}

const isHMRRequest = req.url.startsWith(
ensureLeadingSlash(`${hmrPrefix}/_next/webpack-hmr`)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>before edit</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createNext } from 'e2e-utils'
import { findPort, retry } from 'next-test-utils'

describe('app-dir assetPrefix full URL', () => {
let next, forcedPort
beforeAll(async () => {
forcedPort = ((await findPort()) ?? '54321').toString()

next = await createNext({
files: __dirname,
forcedPort,
nextConfig: {
assetPrefix: `http://localhost:${forcedPort}`,
},
})
})
afterAll(() => next.destroy())

it('should not break HMR when asset prefix set to full URL', async () => {
const browser = await next.browser('/')
const text = await browser.elementByCss('p').text()
expect(text).toBe('before edit')

await next.patchFile('app/page.tsx', (content) => {
return content.replace('before', 'after')
})

await retry(async () => {
expect(await browser.elementByCss('p').text()).toContain('after edit')
})
})
})

0 comments on commit 01a3f98

Please sign in to comment.