1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Added WebSocket arg to allow manually setting port (#5963)

Saw a reply on the original pull request that the WebSocket using a random port broke their set up so I added a `--websocket` or `-w` argument similar to the `-p` argument to allow manually setting this port also.
This commit is contained in:
JJ Kasper 2018-12-31 18:07:10 -06:00 committed by Tim Neutkens
parent 662dfd4271
commit ba8cb31a40
7 changed files with 44 additions and 10 deletions

View file

@ -14,7 +14,12 @@ const defaultConfig = {
generateBuildId: () => null, generateBuildId: () => null,
generateEtags: true, generateEtags: true,
pageExtensions: ['jsx', 'js'], pageExtensions: ['jsx', 'js'],
target: 'server' target: 'server',
onDemandEntries: {
maxInactiveAge: 60 * 1000,
pagesBufferLength: 2,
websocketPort: 0
}
} }
function normalizeConfig (phase, config) { function normalizeConfig (phase, config) {
@ -40,6 +45,12 @@ export default function loadConfig (phase, dir, customConfig) {
if (userConfig.target && !targets.includes(userConfig.target)) { if (userConfig.target && !targets.includes(userConfig.target)) {
throw new Error(`Specified target is invalid. Provided: "${userConfig.target}" should be one of ${targets.join(', ')}`) throw new Error(`Specified target is invalid. Provided: "${userConfig.target}" should be one of ${targets.join(', ')}`)
} }
if (userConfig.onDemandEntries) {
userConfig.onDemandEntries = {
...defaultConfig.onDemandEntries,
...userConfig.onDemandEntries
}
}
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfig} return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfig}
} }

View file

@ -1292,7 +1292,9 @@ module.exports = {
maxInactiveAge: 25 * 1000, maxInactiveAge: 25 * 1000,
// number of pages that should be kept simultaneously without being disposed // number of pages that should be kept simultaneously without being disposed
pagesBufferLength: 2, pagesBufferLength: 2,
} // optionally configure a port for the onDemandEntries WebSocket, not needed by default
websocketPort: 3001,
},
} }
``` ```

View file

@ -173,8 +173,9 @@ export default class HotReloader {
await this.clean() await this.clean()
this.wsPort = await new Promise((resolve, reject) => { this.wsPort = await new Promise((resolve, reject) => {
// create dynamic entries WebSocket const { websocketPort } = this.config.onDemandEntries
this.wss = new WebSocket.Server({ port: 0 }, function (err) { // create on-demand-entries WebSocket
this.wss = new WebSocket.Server({ port: websocketPort }, function (err) {
if (err) { if (err) {
return reject(err) return reject(err)
} }

View file

@ -33,8 +33,8 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
dev, dev,
reload, reload,
pageExtensions, pageExtensions,
maxInactiveAge = 1000 * 60, maxInactiveAge,
pagesBufferLength = 2, pagesBufferLength,
wsPort wsPort
}) { }) {
const {compilers} = multiCompiler const {compilers} = multiCompiler

View file

@ -5,7 +5,8 @@ const path = require('path')
module.exports = withCSS(withSass({ module.exports = withCSS(withSass({
onDemandEntries: { onDemandEntries: {
// Make sure entries are not getting disposed. // Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60 maxInactiveAge: 1000 * 60 * 60,
websocketPort: 3001
}, },
cssModules: true, cssModules: true,
serverRuntimeConfig: { serverRuntimeConfig: {

View file

@ -1,7 +1,7 @@
/* eslint-env jest */ /* eslint-env jest */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { waitFor } from 'next-test-utils' /* check, File */ import { waitFor, fetchViaHTTP } from 'next-test-utils' /* check, File */
import { readFileSync, writeFileSync } from 'fs' import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path' import { join } from 'path'
@ -20,6 +20,12 @@ export default (context, render) => {
browser.close() browser.close()
}) })
it('should use websocketPort for on-demand-entries WebSocket', async () => {
const res = await fetchViaHTTP(context.appPort, '/_next/on-demand-entries-ping')
const wsPort = res.headers.get('port')
expect(wsPort).toBe(context.devWebSocketPort + '')
})
it('should update css styles using hmr', async () => { it('should update css styles using hmr', async () => {
let browser let browser
try { try {

View file

@ -6,7 +6,8 @@ import {
fetchViaHTTP, fetchViaHTTP,
findPort, findPort,
launchApp, launchApp,
killApp killApp,
File
} from 'next-test-utils' } from 'next-test-utils'
// test suits // test suits
@ -19,6 +20,15 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Configuration', () => { describe('Configuration', () => {
beforeAll(async () => { beforeAll(async () => {
context.appPort = await findPort() context.appPort = await findPort()
context.devWebSocketPort = await findPort()
// update next.config with found devWebSocketPort (must come before launchApp)
context.nextConfig = new File(join(__dirname, '../next.config.js'))
context.nextConfig.replace(
'websocketPort: 3001',
`websocketPort: ${context.devWebSocketPort}`
)
context.server = await launchApp(join(__dirname, '../'), context.appPort) context.server = await launchApp(join(__dirname, '../'), context.appPort)
// pre-build all pages at the start // pre-build all pages at the start
@ -28,7 +38,10 @@ describe('Configuration', () => {
renderViaHTTP(context.appPort, '/webpack-css') renderViaHTTP(context.appPort, '/webpack-css')
]) ])
}) })
afterAll(() => killApp(context.server)) afterAll(() => {
killApp(context.server)
context.nextConfig.restore()
})
rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q), (p, q) => fetchViaHTTP(context.appPort, p, q)) rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q), (p, q) => fetchViaHTTP(context.appPort, p, q))
client(context, (p, q) => renderViaHTTP(context.appPort, p, q)) client(context, (p, q) => renderViaHTTP(context.appPort, p, q))