提交 00003193 编写于 作者: J Jason Aslakson 提交者: Tim Neutkens

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: Njaslakson <jason.aslakson@americastestkitchen.com>

* updated README with websocket proxy options

* Fix test / cleanup port setting

* Always set proxy_path
上级 b8c9a1b5
...@@ -18,7 +18,9 @@ const defaultConfig = { ...@@ -18,7 +18,9 @@ const defaultConfig = {
onDemandEntries: { onDemandEntries: {
maxInactiveAge: 60 * 1000, maxInactiveAge: 60 * 1000,
pagesBufferLength: 2, pagesBufferLength: 2,
websocketPort: 0 websocketPort: 0,
websocketProxyPath: '/',
websocketProxyPort: null
} }
} }
......
...@@ -1297,6 +1297,10 @@ module.exports = { ...@@ -1297,6 +1297,10 @@ module.exports = {
pagesBufferLength: 2, pagesBufferLength: 2,
// optionally configure a port for the onDemandEntries WebSocket, not needed by default // optionally configure a port for the onDemandEntries WebSocket, not needed by default
websocketPort: 3001, websocketPort: 3001,
// optionally configure a proxy path for the onDemandEntries WebSocket, not need by default
websocketProxyPath: '/hmr',
// optionally configure a proxy port for the onDemandEntries WebSocket, not need by default
websocketProxyPort: 7002,
}, },
} }
``` ```
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
import Router from 'next/router' import Router from 'next/router'
import fetch from 'unfetch' import fetch from 'unfetch'
const { hostname } = location const { hostname, protocol } = location
const wsProtocol = protocol.includes('https') ? 'wss' : 'ws'
const retryTime = 5000 const retryTime = 5000
let ws = null let ws = null
let lastHref = null let lastHref = null
...@@ -19,7 +20,7 @@ export default async ({ assetPrefix }) => { ...@@ -19,7 +20,7 @@ export default async ({ assetPrefix }) => {
} }
return new Promise(resolve => { return new Promise(resolve => {
ws = new WebSocket(`ws://${hostname}:${process.env.NEXT_WS_PORT}`) ws = new WebSocket(`${wsProtocol}://${hostname}:${process.env.NEXT_WS_PORT}${process.env.NEXT_WS_PROXY_PATH}`)
ws.onopen = () => resolve() ws.onopen = () => resolve()
ws.onclose = () => { ws.onclose = () => {
setTimeout(async () => { setTimeout(async () => {
......
...@@ -164,10 +164,13 @@ export default class HotReloader { ...@@ -164,10 +164,13 @@ export default class HotReloader {
return del(join(this.dir, this.config.distDir), { force: true }) return del(join(this.dir, this.config.distDir), { force: true })
} }
addWsPort (configs) { addWsConfig (configs) {
configs[0].plugins.push(new webpack.DefinePlugin({ const { websocketProxyPath, websocketProxyPort } = this.config.onDemandEntries
'process.env.NEXT_WS_PORT': this.wsPort const opts = {
})) 'process.env.NEXT_WS_PORT': websocketProxyPort || this.wsPort,
'process.env.NEXT_WS_PROXY_PATH': JSON.stringify(websocketProxyPath)
}
configs[0].plugins.push(new webpack.DefinePlugin(opts))
} }
async getWebpackConfig () { async getWebpackConfig () {
...@@ -200,7 +203,7 @@ export default class HotReloader { ...@@ -200,7 +203,7 @@ export default class HotReloader {
}) })
const configs = await this.getWebpackConfig() const configs = await this.getWebpackConfig()
this.addWsPort(configs) this.addWsConfig(configs)
const multiCompiler = webpack(configs) const multiCompiler = webpack(configs)
...@@ -229,7 +232,7 @@ export default class HotReloader { ...@@ -229,7 +232,7 @@ export default class HotReloader {
await this.clean() await this.clean()
const configs = await this.getWebpackConfig() const configs = await this.getWebpackConfig()
this.addWsPort(configs) this.addWsConfig(configs)
const compiler = webpack(configs) const compiler = webpack(configs)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册