Skip to content

Commit 0000319

Browse files
jaslaksontimneutkens
authored andcommitted
Additional config options for hot reloader web socket connection (#6083)
* Additional config options for hot reloader web socket connection * Apply suggestions from code review Accepting suggested code changes Co-Authored-By: jaslakson <jason.aslakson@americastestkitchen.com> * updated README with websocket proxy options * Fix test / cleanup port setting * Always set proxy_path
1 parent b8c9a1b commit 0000319

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

packages/next-server/server/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const defaultConfig = {
1818
onDemandEntries: {
1919
maxInactiveAge: 60 * 1000,
2020
pagesBufferLength: 2,
21-
websocketPort: 0
21+
websocketPort: 0,
22+
websocketProxyPath: '/',
23+
websocketProxyPort: null
2224
}
2325
}
2426

packages/next/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,10 @@ module.exports = {
12971297
pagesBufferLength: 2,
12981298
// optionally configure a port for the onDemandEntries WebSocket, not needed by default
12991299
websocketPort: 3001,
1300+
// optionally configure a proxy path for the onDemandEntries WebSocket, not need by default
1301+
websocketProxyPath: '/hmr',
1302+
// optionally configure a proxy port for the onDemandEntries WebSocket, not need by default
1303+
websocketProxyPort: 7002,
13001304
},
13011305
}
13021306
```

packages/next/client/on-demand-entries-client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import Router from 'next/router'
44
import fetch from 'unfetch'
55

6-
const { hostname } = location
6+
const { hostname, protocol } = location
7+
const wsProtocol = protocol.includes('https') ? 'wss' : 'ws'
78
const retryTime = 5000
89
let ws = null
910
let lastHref = null
@@ -19,7 +20,7 @@ export default async ({ assetPrefix }) => {
1920
}
2021

2122
return new Promise(resolve => {
22-
ws = new WebSocket(`ws://${hostname}:${process.env.NEXT_WS_PORT}`)
23+
ws = new WebSocket(`${wsProtocol}://${hostname}:${process.env.NEXT_WS_PORT}${process.env.NEXT_WS_PROXY_PATH}`)
2324
ws.onopen = () => resolve()
2425
ws.onclose = () => {
2526
setTimeout(async () => {

packages/next/server/hot-reloader.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,13 @@ export default class HotReloader {
164164
return del(join(this.dir, this.config.distDir), { force: true })
165165
}
166166

167-
addWsPort (configs) {
168-
configs[0].plugins.push(new webpack.DefinePlugin({
169-
'process.env.NEXT_WS_PORT': this.wsPort
170-
}))
167+
addWsConfig (configs) {
168+
const { websocketProxyPath, websocketProxyPort } = this.config.onDemandEntries
169+
const opts = {
170+
'process.env.NEXT_WS_PORT': websocketProxyPort || this.wsPort,
171+
'process.env.NEXT_WS_PROXY_PATH': JSON.stringify(websocketProxyPath)
172+
}
173+
configs[0].plugins.push(new webpack.DefinePlugin(opts))
171174
}
172175

173176
async getWebpackConfig () {
@@ -200,7 +203,7 @@ export default class HotReloader {
200203
})
201204

202205
const configs = await this.getWebpackConfig()
203-
this.addWsPort(configs)
206+
this.addWsConfig(configs)
204207

205208
const multiCompiler = webpack(configs)
206209

@@ -229,7 +232,7 @@ export default class HotReloader {
229232
await this.clean()
230233

231234
const configs = await this.getWebpackConfig()
232-
this.addWsPort(configs)
235+
this.addWsConfig(configs)
233236

234237
const compiler = webpack(configs)
235238

0 commit comments

Comments
 (0)