Skip to content

Commit 1725af5

Browse files
authored
fix(next): use correct hmr url if assetPrefix is set in next config (#10859)
The next.js assetPrefix needs to be included in the websocket URL. Previously, we were appending both assetPrefix and basePath, which is incorrect. `assetPrefix` overrides `basePath` if both are set. This PR mimics the way Next.js connects to the HMR server. Sources: - https://github.com/AlessioGr/next.js/blob/canary/packages/next/src/server/lib/router-server.ts#L688 - https://github.com/AlessioGr/next.js/blob/canary/packages/next/src/server/config.ts#L322 - https://github.com/AlessioGr/next.js/blob/canary/packages/next/src/client/components/react-dev-overlay/app/client-entry.tsx
1 parent a13d4fe commit 1725af5

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

packages/next/src/withPayload.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ export const withPayload = (nextConfig = {}) => {
147147
toReturn.env.NEXT_BASE_PATH = nextConfig.basePath
148148
}
149149

150-
if (nextConfig.assetPrefix) {
151-
toReturn.env.NEXT_ASSET_PREFIX = nextConfig.assetPrefix
152-
}
153-
154150
return toReturn
155151
}
156152

packages/payload/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,11 +922,13 @@ export const getPayload = async (
922922
) {
923923
try {
924924
const port = process.env.PORT || '3000'
925-
const basePath = process.env.NEXT_BASE_PATH || ''
926-
const assetPrefix = process.env.NEXT_ASSET_PREFIX || ''
925+
926+
const path = '/_next/webpack-hmr'
927+
// The __NEXT_ASSET_PREFIX env variable is set for both assetPrefix and basePath (tested in Next.js 15.1.6)
928+
const prefix = process.env.__NEXT_ASSET_PREFIX ?? ''
927929

928930
cached.ws = new WebSocket(
929-
`ws://localhost:${port}${basePath}${assetPrefix}/_next/webpack-hmr`,
931+
process.env.PAYLOAD_HMR_URL_OVERRIDE ?? `ws://localhost:${port}${prefix}${path}`,
930932
)
931933

932934
cached.ws.onmessage = (event) => {

0 commit comments

Comments
 (0)