Skip to content

Commit 81065a2

Browse files
committed
Additional config options for hot reloader web socket connection
1 parent f960091 commit 81065a2

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
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: null,
23+
websocketProxyPort: null
2224
}
2325
}
2426

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
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'
8+
const wsPort = process.env.NEXT_WS_PROXY_PORT || process.env.NEXT_WS_PORT
9+
const wsPath = process.env.NEXT_WS_PROXY_PATH || null
710
const retryTime = 5000
811
let ws = null
912
let lastHref = null
@@ -19,7 +22,7 @@ export default async ({ assetPrefix }) => {
1922
}
2023

2124
return new Promise(resolve => {
22-
ws = new WebSocket(`ws://${hostname}:${process.env.NEXT_WS_PORT}`)
25+
ws = new WebSocket(`${wsProtocol}://${hostname}:${wsPort}${wsPath ? `/${wsPath}` : ''}`)
2326
ws.onopen = () => resolve()
2427
ws.onclose = () => {
2528
setTimeout(async () => {

packages/next/server/hot-reloader.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,18 @@ 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({
167+
addWsConfig (configs) {
168+
const opts = {
169169
'process.env.NEXT_WS_PORT': this.wsPort
170-
}))
170+
}
171+
const { websocketProxyPath, websocketProxyPort } = this.config.onDemandEntries
172+
if (websocketProxyPath) {
173+
opts['process.env.NEXT_WS_PROXY_PATH'] = websocketProxyPath
174+
}
175+
if (websocketProxyPath) {
176+
opts['process.env.NEXT_WS_PROXY_PORT'] = websocketProxyPort
177+
}
178+
configs[0].plugins.push(new webpack.DefinePlugin(opts))
171179
}
172180

173181
async getWebpackConfig () {
@@ -200,7 +208,7 @@ export default class HotReloader {
200208
})
201209

202210
const configs = await this.getWebpackConfig()
203-
this.addWsPort(configs)
211+
this.addWsConfig(configs)
204212

205213
const multiCompiler = webpack(configs)
206214

@@ -229,7 +237,7 @@ export default class HotReloader {
229237
await this.clean()
230238

231239
const configs = await this.getWebpackConfig()
232-
this.addWsPort(configs)
240+
this.addWsConfig(configs)
233241

234242
const compiler = webpack(configs)
235243

0 commit comments

Comments
 (0)