Unverified Commit 75748caf authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Migrate server-sent events HMR connection to WebSocket (#29903)

This replaces the server-sent events HMR connection with a WebSocket connection to prevent hitting browser connection limits, allow sending events back from the browser, and overall better performance. 

This approach sets up the the `upgrade` event listener on the server immediately when created via `next dev` and on the first request using `req.socket.server` when created via a custom server. In a follow-up PR we can push the files changed via the WebSocket as well. 

x-ref: https://github.com/vercel/next.js/issues/10061
x-ref: https://github.com/vercel/next.js/issues/8064
x-ref: https://github.com/vercel/next.js/issues/4495

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
parent ea0cdc5a
/* globals __webpack_hash__ */ /* globals __webpack_hash__ */
import EventSourcePolyfill from './event-source-polyfill'
import { addMessageListener } from './error-overlay/eventsource'
import { setupPing } from './on-demand-entries-utils'
import { displayContent } from './fouc' import { displayContent } from './fouc'
import initOnDemandEntries from './on-demand-entries-client'
if (!window.EventSource) { import { addMessageListener, connectHMR } from './error-overlay/websocket'
window.EventSource = EventSourcePolyfill
}
const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent) const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent)
let { assetPrefix, page } = data let { assetPrefix, page } = data
...@@ -94,5 +89,6 @@ addMessageListener((event) => { ...@@ -94,5 +89,6 @@ addMessageListener((event) => {
} }
}) })
setupPing(assetPrefix, () => page) connectHMR({ path: `${assetPrefix}/_next/webpack-hmr` })
displayContent() displayContent()
initOnDemandEntries()
import { addMessageListener } from './error-overlay/eventsource' import { addMessageListener } from './error-overlay/websocket'
export default function initializeBuildWatcher(toggleCallback) { export default function initializeBuildWatcher(toggleCallback) {
const shadowHost = document.createElement('div') const shadowHost = document.createElement('div')
......
...@@ -33,7 +33,7 @@ import { ...@@ -33,7 +33,7 @@ import {
onRefresh, onRefresh,
} from '@next/react-dev-overlay/lib/client' } from '@next/react-dev-overlay/lib/client'
import stripAnsi from 'next/dist/compiled/strip-ansi' import stripAnsi from 'next/dist/compiled/strip-ansi'
import { addMessageListener } from './eventsource' import { addMessageListener } from './websocket'
import formatWebpackMessages from './format-webpack-messages' import formatWebpackMessages from './format-webpack-messages'
// This alternative WebpackDevServer combines the functionality of: // This alternative WebpackDevServer combines the functionality of:
...@@ -51,10 +51,8 @@ export default function connect() { ...@@ -51,10 +51,8 @@ export default function connect() {
register() register()
addMessageListener((event) => { addMessageListener((event) => {
// This is the heartbeat event if (event.data.indexOf('action') === -1) return
if (event.data === '\uD83D\uDC93') {
return
}
try { try {
processMessage(event) processMessage(event)
} catch (ex) { } catch (ex) {
......
const eventCallbacks = [] let source: WebSocket
const eventCallbacks: ((event: any) => void)[] = []
let lastActivity = Date.now()
function EventSourceWrapper(options) { export function addMessageListener(cb: (event: any) => void) {
var source eventCallbacks.push(cb)
var lastActivity = new Date() }
var listeners = []
export function sendMessage(data: any) {
if (!source || source.readyState !== source.OPEN) return
return source.send(data)
}
export function connectHMR(options: {
path: string
timeout: number
log?: boolean
}) {
if (!options.timeout) { if (!options.timeout) {
options.timeout = 20 * 1000 options.timeout = 5 * 1000
} }
init() init()
var timer = setInterval(function () {
if (new Date() - lastActivity > options.timeout) { let timer = setInterval(function () {
if (Date.now() - lastActivity > options.timeout) {
handleDisconnect() handleDisconnect()
} }
}, options.timeout / 2) }, options.timeout / 2)
function init() { function init() {
source = new window.EventSource(options.path) if (source) source.close()
const { protocol, hostname, port } = location
const url = `${protocol === 'http:' ? 'ws' : 'wss'}://${hostname}:${port}`
source = new window.WebSocket(`${url}${options.path}`)
source.onopen = handleOnline source.onopen = handleOnline
source.onerror = handleDisconnect source.onerror = handleDisconnect
source.onmessage = handleMessage source.onmessage = handleMessage
...@@ -25,17 +40,13 @@ function EventSourceWrapper(options) { ...@@ -25,17 +40,13 @@ function EventSourceWrapper(options) {
function handleOnline() { function handleOnline() {
if (options.log) console.log('[HMR] connected') if (options.log) console.log('[HMR] connected')
lastActivity = new Date() lastActivity = Date.now()
} }
function handleMessage(event) { function handleMessage(event: any) {
lastActivity = new Date() lastActivity = Date.now()
for (var i = 0; i < listeners.length; i++) {
listeners[i](event)
}
eventCallbacks.forEach((cb) => { eventCallbacks.forEach((cb) => {
if (!cb.unfiltered && event.data.indexOf('action') === -1) return
cb(event) cb(event)
}) })
} }
...@@ -45,22 +56,4 @@ function EventSourceWrapper(options) { ...@@ -45,22 +56,4 @@ function EventSourceWrapper(options) {
source.close() source.close()
setTimeout(init, options.timeout) setTimeout(init, options.timeout)
} }
return {
close: () => {
clearInterval(timer)
source.close()
},
addMessageListener: function (fn) {
listeners.push(fn)
},
}
}
export function addMessageListener(cb) {
eventCallbacks.push(cb)
}
export function getEventSourceWrapper(options) {
return EventSourceWrapper(options)
} }
This diff is collapsed.
import Router from 'next/router' import Router from 'next/router'
import { setupPing, currentPage, closePing } from './on-demand-entries-utils' import { addMessageListener, sendMessage } from './error-overlay/websocket'
export default async ({ assetPrefix }) => { export default async () => {
Router.ready(() => { setInterval(() => {
Router.events.on( sendMessage(JSON.stringify({ event: 'ping', page: Router.pathname }))
'routeChangeComplete', }, 2500)
setupPing.bind(this, assetPrefix, () => Router.pathname)
)
})
setupPing(
assetPrefix,
() => Router.query.__NEXT_PAGE || Router.pathname,
currentPage
)
// prevent HMR connection from being closed when running tests addMessageListener((event) => {
if (!process.env.__NEXT_TEST_MODE) { if (event.data.indexOf('{') === -1) return
document.addEventListener('visibilitychange', (_event) => { try {
const state = document.visibilityState const payload = JSON.parse(event.data)
if (state === 'visible') { // don't attempt fetching the page if we're already showing
setupPing(assetPrefix, () => Router.pathname, true) // the dev overlay as this can cause the error to be triggered
} else { // repeatedly
closePing() if (
payload.event === 'pong' &&
payload.invalid &&
!self.__NEXT_DATA__.err
) {
// Payload can be invalid even if the page does exist.
// So, we check if it can be created.
fetch(location.href, {
credentials: 'same-origin',
}).then((pageRes) => {
if (pageRes.status === 200) {
// Page exists now, reload
location.reload()
} else {
// Page doesn't exist
if (
self.__NEXT_DATA__.page === Router.pathname &&
Router.pathname !== '/_error'
) {
// We are still on the page,
// reload to show 404 error page
location.reload()
}
}
})
} }
}) } catch (err) {
console.error('on-demand-entries failed to parse response', err)
window.addEventListener('beforeunload', () => { }
closePing() })
})
}
} }
/* global location */
import { getEventSourceWrapper } from './error-overlay/eventsource'
let evtSource
export let currentPage
export function closePing() {
if (evtSource) evtSource.close()
evtSource = null
}
export function setupPing(assetPrefix, pathnameFn, retry) {
const pathname = pathnameFn()
// Make sure to only create new EventSource request if page has changed
if (pathname === currentPage && !retry) return
currentPage = pathname
// close current EventSource connection
closePing()
evtSource = getEventSourceWrapper({
path: `${assetPrefix}/_next/webpack-hmr?page=${encodeURIComponent(
currentPage
)}`,
timeout: 5000,
})
evtSource.addMessageListener((event) => {
if (event.data.indexOf('{') === -1) return
try {
const payload = JSON.parse(event.data)
// don't attempt fetching the page if we're already showing
// the dev overlay as this can cause the error to be triggered
// repeatedly
if (payload.invalid && !self.__NEXT_DATA__.err) {
// Payload can be invalid even if the page does exist.
// So, we check if it can be created.
fetch(location.href, {
credentials: 'same-origin',
}).then((pageRes) => {
if (pageRes.status === 200) {
// Page exists now, reload
location.reload()
} else {
// Page doesn't exist
if (
self.__NEXT_DATA__.page === currentPage &&
currentPage !== '/_error'
) {
// We are still on the page,
// reload to show 404 error page
location.reload()
}
}
})
}
} catch (err) {
console.error('on-demand-entries failed to parse response', err)
}
})
}
/* globals __REPLACE_NOOP_IMPORT__ */ /* globals __REPLACE_NOOP_IMPORT__ */
import { initNext, version, router, emitter, render, renderError } from './' import { initNext, version, router, emitter, render, renderError } from './'
import EventSourcePolyfill from './dev/event-source-polyfill'
import initOnDemandEntries from './dev/on-demand-entries-client' import initOnDemandEntries from './dev/on-demand-entries-client'
import initWebpackHMR from './dev/webpack-hot-middleware-client' import initWebpackHMR from './dev/webpack-hot-middleware-client'
import initializeBuildWatcher from './dev/dev-build-watcher' import initializeBuildWatcher from './dev/dev-build-watcher'
import { displayContent } from './dev/fouc' import { displayContent } from './dev/fouc'
import { addMessageListener } from './dev/error-overlay/eventsource' import { connectHMR, addMessageListener } from './dev/error-overlay/websocket'
import { import {
assign, assign,
urlQueryToSearchParams, urlQueryToSearchParams,
...@@ -18,11 +17,6 @@ import { ...@@ -18,11 +17,6 @@ import {
// eslint-disable-next-line no-unused-expressions // eslint-disable-next-line no-unused-expressions
__REPLACE_NOOP_IMPORT__ __REPLACE_NOOP_IMPORT__
// Support EventSource on Internet Explorer 11
if (!window.EventSource) {
window.EventSource = EventSourcePolyfill
}
const { const {
__NEXT_DATA__: { assetPrefix }, __NEXT_DATA__: { assetPrefix },
} = window } = window
...@@ -30,6 +24,8 @@ const { ...@@ -30,6 +24,8 @@ const {
const prefix = assetPrefix || '' const prefix = assetPrefix || ''
const webpackHMR = initWebpackHMR() const webpackHMR = initWebpackHMR()
connectHMR({ path: `${prefix}/_next/webpack-hmr` })
window.next = { window.next = {
version, version,
// router is initialized later so it has to be live-binded // router is initialized later so it has to be live-binded
...@@ -42,7 +38,7 @@ window.next = { ...@@ -42,7 +38,7 @@ window.next = {
} }
initNext({ webpackHMR }) initNext({ webpackHMR })
.then(({ renderCtx }) => { .then(({ renderCtx }) => {
initOnDemandEntries({ assetPrefix: prefix }) initOnDemandEntries()
let buildIndicatorHandler = () => {} let buildIndicatorHandler = () => {}
...@@ -88,7 +84,6 @@ initNext({ webpackHMR }) ...@@ -88,7 +84,6 @@ initNext({ webpackHMR })
} }
} }
} }
devPagesManifestListener.unfiltered = true
addMessageListener(devPagesManifestListener) addMessageListener(devPagesManifestListener)
if (process.env.__NEXT_BUILD_INDICATOR) { if (process.env.__NEXT_BUILD_INDICATOR) {
......
Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This diff is collapsed.
{"name":"ws","main":"index.js","author":"Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)","license":"MIT"}
...@@ -183,6 +183,7 @@ ...@@ -183,6 +183,7 @@
"@types/styled-jsx": "2.2.8", "@types/styled-jsx": "2.2.8",
"@types/text-table": "0.2.1", "@types/text-table": "0.2.1",
"@types/webpack-sources1": "npm:@types/webpack-sources@0.1.5", "@types/webpack-sources1": "npm:@types/webpack-sources@0.1.5",
"@types/ws": "8.2.0",
"@vercel/ncc": "0.27.0", "@vercel/ncc": "0.27.0",
"@vercel/nft": "0.16.1", "@vercel/nft": "0.16.1",
"amphtml-validator": "1.0.33", "amphtml-validator": "1.0.33",
...@@ -249,7 +250,8 @@ ...@@ -249,7 +250,8 @@
"webpack-sources1": "npm:webpack-sources@1.4.3", "webpack-sources1": "npm:webpack-sources@1.4.3",
"webpack-sources3": "npm:webpack-sources@3.2.0", "webpack-sources3": "npm:webpack-sources@3.2.0",
"webpack4": "npm:webpack@4.44.1", "webpack4": "npm:webpack@4.44.1",
"webpack5": "npm:webpack@5.58.2" "webpack5": "npm:webpack@5.58.2",
"ws": "8.2.3"
}, },
"resolutions": { "resolutions": {
"browserslist": "4.16.6", "browserslist": "4.16.6",
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
import http from 'http' import type ws from 'ws'
export class WebpackHotMiddleware { export class WebpackHotMiddleware {
eventStream: EventStream eventStream: EventStream
...@@ -86,14 +86,10 @@ export class WebpackHotMiddleware { ...@@ -86,14 +86,10 @@ export class WebpackHotMiddleware {
this.latestStats = statsResult this.latestStats = statsResult
this.publishStats('built', this.latestStats) this.publishStats('built', this.latestStats)
} }
middleware = (
req: http.IncomingMessage, onHMR = (client: ws) => {
res: http.ServerResponse, if (this.closed) return
next: () => void this.eventStream.handler(client)
) => {
if (this.closed) return next()
if (!req.url?.startsWith('/_next/webpack-hmr')) return next()
this.eventStream.handler(req, res)
if (this.latestStats) { if (this.latestStats) {
// Explicitly not passing in `log` fn as we don't want to log again on // Explicitly not passing in `log` fn as we don't want to log again on
// the server // the server
...@@ -131,64 +127,34 @@ export class WebpackHotMiddleware { ...@@ -131,64 +127,34 @@ export class WebpackHotMiddleware {
} }
class EventStream { class EventStream {
clients: Set<http.ServerResponse> clients: Set<ws>
interval: NodeJS.Timeout
constructor() { constructor() {
this.clients = new Set() this.clients = new Set()
this.interval = setInterval(this.heartbeatTick, 2500).unref()
} }
heartbeatTick = () => { everyClient(fn: (client: ws) => void) {
this.everyClient((client) => {
client.write('data: \uD83D\uDC93\n\n')
})
}
everyClient(fn: (client: http.ServerResponse) => void) {
for (const client of this.clients) { for (const client of this.clients) {
fn(client) fn(client)
} }
} }
close() { close() {
clearInterval(this.interval)
this.everyClient((client) => { this.everyClient((client) => {
if (!client.finished) client.end() client.close()
}) })
this.clients.clear() this.clients.clear()
} }
handler(req: http.IncomingMessage, res: http.ServerResponse) { handler(client: ws) {
const headers = { this.clients.add(client)
'Access-Control-Allow-Origin': '*', client.addEventListener('close', () => {
'Content-Type': 'text/event-stream;charset=utf-8', this.clients.delete(client)
'Cache-Control': 'no-cache, no-transform',
// While behind nginx, event stream should not be buffered:
// http://nginx.org/docs/http/ngx_http_proxy_module.html#proxy_buffering
'X-Accel-Buffering': 'no',
}
const isHttp1 = !(parseInt(req.httpVersion) >= 2)
if (isHttp1) {
req.socket.setKeepAlive(true)
Object.assign(headers, {
Connection: 'keep-alive',
})
}
res.writeHead(200, headers)
res.write('\n')
this.clients.add(res)
req.on('close', () => {
if (!res.finished) res.end()
this.clients.delete(res)
}) })
} }
publish(payload: any) { publish(payload: any) {
this.everyClient((client) => { this.everyClient((client) => {
client.write('data: ' + JSON.stringify(payload) + '\n\n') client.send(JSON.stringify(payload))
}) })
} }
} }
import { getOverlayMiddleware } from '@next/react-dev-overlay/lib/middleware' import { getOverlayMiddleware } from '@next/react-dev-overlay/lib/middleware'
import { NextHandleFunction } from 'connect'
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
import { WebpackHotMiddleware } from './hot-middleware' import { WebpackHotMiddleware } from './hot-middleware'
import { join, relative, isAbsolute } from 'path' import { join, relative, isAbsolute } from 'path'
...@@ -33,6 +32,9 @@ import { CustomRoutes } from '../../lib/load-custom-routes' ...@@ -33,6 +32,9 @@ import { CustomRoutes } from '../../lib/load-custom-routes'
import { DecodeError } from '../../shared/lib/utils' import { DecodeError } from '../../shared/lib/utils'
import { Span, trace } from '../../trace' import { Span, trace } from '../../trace'
import isError from '../../lib/is-error' import isError from '../../lib/is-error'
import ws from 'next/dist/compiled/ws'
const wsServer = new ws.Server({ noServer: true })
export async function renderScriptError( export async function renderScriptError(
res: ServerResponse, res: ServerResponse,
...@@ -136,7 +138,7 @@ export default class HotReloader { ...@@ -136,7 +138,7 @@ export default class HotReloader {
private buildId: string private buildId: string
private middlewares: any[] private middlewares: any[]
private pagesDir: string private pagesDir: string
private webpackHotMiddleware: (NextHandleFunction & any) | null private webpackHotMiddleware?: WebpackHotMiddleware
private config: NextConfigComplete private config: NextConfigComplete
private stats: webpack.Stats | null private stats: webpack.Stats | null
public serverStats: webpack.Stats | null public serverStats: webpack.Stats | null
...@@ -144,7 +146,7 @@ export default class HotReloader { ...@@ -144,7 +146,7 @@ export default class HotReloader {
private serverError: Error | null = null private serverError: Error | null = null
private serverPrevDocumentHash: string | null private serverPrevDocumentHash: string | null
private prevChunkNames?: Set<any> private prevChunkNames?: Set<any>
private onDemandEntries: any private onDemandEntries?: ReturnType<typeof onDemandEntryHandler>
private previewProps: __ApiPreviewProps private previewProps: __ApiPreviewProps
private watcher: any private watcher: any
private rewrites: CustomRoutes['rewrites'] private rewrites: CustomRoutes['rewrites']
...@@ -171,7 +173,6 @@ export default class HotReloader { ...@@ -171,7 +173,6 @@ export default class HotReloader {
this.dir = dir this.dir = dir
this.middlewares = [] this.middlewares = []
this.pagesDir = pagesDir this.pagesDir = pagesDir
this.webpackHotMiddleware = null
this.stats = null this.stats = null
this.serverStats = null this.serverStats = null
this.serverPrevDocumentHash = null this.serverPrevDocumentHash = null
...@@ -261,6 +262,13 @@ export default class HotReloader { ...@@ -261,6 +262,13 @@ export default class HotReloader {
return { finished } return { finished }
} }
public onHMR(req: IncomingMessage, _res: ServerResponse, head: Buffer) {
wsServer.handleUpgrade(req, req.socket, head, (client) => {
this.webpackHotMiddleware?.onHMR(client)
this.onDemandEntries?.onHMR(client)
})
}
private async clean(span: Span): Promise<void> { private async clean(span: Span): Promise<void> {
return span return span
.traceChild('clean') .traceChild('clean')
...@@ -623,9 +631,6 @@ export default class HotReloader { ...@@ -623,9 +631,6 @@ export default class HotReloader {
}) })
this.middlewares = [ this.middlewares = [
// must come before hotMiddleware
this.onDemandEntries.middleware,
this.webpackHotMiddleware.middleware,
getOverlayMiddleware({ getOverlayMiddleware({
rootDirectory: this.dir, rootDirectory: this.dir,
stats: () => this.stats, stats: () => this.stats,
...@@ -703,7 +708,7 @@ export default class HotReloader { ...@@ -703,7 +708,7 @@ export default class HotReloader {
if (error) { if (error) {
return Promise.reject(error) return Promise.reject(error)
} }
return this.onDemandEntries.ensurePage(page, clientOnly) return this.onDemandEntries?.ensurePage(page, clientOnly) as any
} }
} }
......
import crypto from 'crypto' import crypto from 'crypto'
import fs from 'fs' import fs from 'fs'
import chalk from 'chalk' import chalk from 'chalk'
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse, Server as HTTPServer } from 'http'
import { Worker } from 'jest-worker' import { Worker } from 'jest-worker'
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator' import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import findUp from 'next/dist/compiled/find-up' import findUp from 'next/dist/compiled/find-up'
...@@ -73,6 +73,7 @@ export default class DevServer extends Server { ...@@ -73,6 +73,7 @@ export default class DevServer extends Server {
private hotReloader?: HotReloader private hotReloader?: HotReloader
private isCustomServer: boolean private isCustomServer: boolean
protected sortedRoutes?: string[] protected sortedRoutes?: string[]
private addedUpgradeListener = false
protected staticPathsWorker: import('jest-worker').Worker & { protected staticPathsWorker: import('jest-worker').Worker & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
...@@ -82,6 +83,7 @@ export default class DevServer extends Server { ...@@ -82,6 +83,7 @@ export default class DevServer extends Server {
options: ServerConstructor & { options: ServerConstructor & {
conf: NextConfig conf: NextConfig
isNextDevCommand?: boolean isNextDevCommand?: boolean
httpServer?: HTTPServer
} }
) { ) {
super({ ...options, dev: true }) super({ ...options, dev: true })
...@@ -116,6 +118,13 @@ export default class DevServer extends Server { ...@@ -116,6 +118,13 @@ export default class DevServer extends Server {
`The static directory has been deprecated in favor of the public directory. https://nextjs.org/docs/messages/static-dir-deprecated` `The static directory has been deprecated in favor of the public directory. https://nextjs.org/docs/messages/static-dir-deprecated`
) )
} }
// setup upgrade listener eagerly when we can otherwise
// it will be done on the first request via req.socket.server
if (options.httpServer) {
this.setupWebSocketHandler(options.httpServer)
}
this.isCustomServer = !options.isNextDevCommand this.isCustomServer = !options.isNextDevCommand
this.pagesDir = findPagesDir(this.dir) this.pagesDir = findPagesDir(this.dir)
this.staticPathsWorker = new Worker( this.staticPathsWorker = new Worker(
...@@ -410,12 +419,38 @@ export default class DevServer extends Server { ...@@ -410,12 +419,38 @@ export default class DevServer extends Server {
return false return false
} }
private setupWebSocketHandler(server?: HTTPServer, _req?: IncomingMessage) {
if (!this.addedUpgradeListener) {
this.addedUpgradeListener = true
server = server || (_req?.socket as any)?.server
if (!server) {
// this is very unlikely to happen but show an error in case
// it does somehow
Log.error(
`Invalid IncomingMessage received, make sure http.createServer is being used to handle requests.`
)
} else {
server.on('upgrade', (req, socket, head) => {
if (
req.url?.startsWith(
`${this.nextConfig.basePath || ''}/_next/webpack-hmr`
)
) {
this.hotReloader?.onHMR(req, socket, head)
}
})
}
}
}
async run( async run(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
parsedUrl: UrlWithParsedQuery parsedUrl: UrlWithParsedQuery
): Promise<void> { ): Promise<void> {
await this.devReady await this.devReady
this.setupWebSocketHandler(undefined, req)
const { basePath } = this.nextConfig const { basePath } = this.nextConfig
let originalPathname: string | null = null let originalPathname: string | null = null
......
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { IncomingMessage, ServerResponse } from 'http'
import { join, posix } from 'path' import { join, posix } from 'path'
import { parse } from 'url'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
import { normalizePagePath, normalizePathSep } from '../normalize-page-path' import { normalizePagePath, normalizePathSep } from '../normalize-page-path'
import { pageNotFoundError } from '../require' import { pageNotFoundError } from '../require'
...@@ -9,6 +7,7 @@ import { findPageFile } from '../lib/find-page-file' ...@@ -9,6 +7,7 @@ import { findPageFile } from '../lib/find-page-file'
import getRouteFromEntrypoint from '../get-route-from-entrypoint' import getRouteFromEntrypoint from '../get-route-from-entrypoint'
import { API_ROUTE } from '../../lib/constants' import { API_ROUTE } from '../../lib/constants'
import { reportTrigger } from '../../build/output' import { reportTrigger } from '../../build/output'
import type ws from 'ws'
export const ADDED = Symbol('added') export const ADDED = Symbol('added')
export const BUILDING = Symbol('building') export const BUILDING = Symbol('building')
...@@ -247,27 +246,23 @@ export default function onDemandEntryHandler( ...@@ -247,27 +246,23 @@ export default function onDemandEntryHandler(
return promise return promise
}, },
middleware(req: IncomingMessage, res: ServerResponse, next: Function) { onHMR(client: ws) {
if (!req.url?.startsWith('/_next/webpack-hmr')) return next() client.addEventListener('message', ({ data }) => {
data = typeof data !== 'string' ? data.toString() : data
const { query } = parse(req.url!, true) try {
const page = query.page const parsedData = JSON.parse(data)
if (!page) return next()
if (parsedData.event === 'ping') {
const runPing = () => { const result = handlePing(parsedData.page)
const data = handlePing(query.page as string) client.send(
if (!data) return JSON.stringify({
res.write('data: ' + JSON.stringify(data) + '\n\n') ...result,
} event: 'pong',
const pingInterval = setInterval(() => runPing(), pingIntervalTime) })
)
// Run a ping now to make sure page is instantly flagged as active }
setTimeout(() => runPing(), 0) } catch (_) {}
req.on('close', () => {
clearInterval(pingInterval)
}) })
next()
}, },
} }
} }
......
...@@ -6,11 +6,17 @@ export default async function start( ...@@ -6,11 +6,17 @@ export default async function start(
port?: number, port?: number,
hostname?: string hostname?: string
) { ) {
let requestHandler: ReturnType<typeof app.getRequestHandler>
const srv = http.createServer((req, res) => {
return requestHandler(req, res)
})
const app = next({ const app = next({
...serverOptions, ...serverOptions,
customServer: false, customServer: false,
httpServer: srv,
}) })
const srv = http.createServer(app.getRequestHandler()) requestHandler = app.getRequestHandler()
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use // This code catches EADDRINUSE error if the port is already in use
srv.on('error', reject) srv.on('error', reject)
......
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
const findUp = require('find-up')
// eslint-disable-next-line import/no-extraneous-dependencies
const ncc = require('@vercel/ncc') const ncc = require('@vercel/ncc')
const { existsSync, readFileSync } = require('fs') const { existsSync, readFileSync } = require('fs')
const { basename, dirname, extname, join, resolve } = require('path') const { basename, dirname, extname, join, resolve } = require('path')
...@@ -56,7 +58,17 @@ module.exports = function (task) { ...@@ -56,7 +58,17 @@ module.exports = function (task) {
// It defines `name`, `main`, `author`, and `license`. It also defines `types`. // It defines `name`, `main`, `author`, and `license`. It also defines `types`.
// n.b. types intended for development usage only. // n.b. types intended for development usage only.
function writePackageManifest(packageName, main, bundleName, precompiled) { function writePackageManifest(packageName, main, bundleName, precompiled) {
const packagePath = bundleRequire.resolve(packageName + '/package.json') // some newer packages fail to include package.json in the exports
// so we can't reliably use require.resolve here
let packagePath
try {
packagePath = bundleRequire.resolve(packageName + '/package.json')
} catch (_) {
packagePath = findUp.sync('package.json', {
cwd: dirname(bundleRequire.resolve(packageName)),
})
}
let { name, author, license } = require(packagePath) let { name, author, license } = require(packagePath)
const compiledPackagePath = join( const compiledPackagePath = join(
......
...@@ -782,6 +782,15 @@ export async function ncc_webpack_bundle_packages(task, opts) { ...@@ -782,6 +782,15 @@ export async function ncc_webpack_bundle_packages(task, opts) {
.target('compiled/webpack/') .target('compiled/webpack/')
} }
// eslint-disable-next-line camelcase
externals['ws'] = 'next/dist/compiled/ws'
export async function ncc_ws(task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('ws')))
.ncc({ packageName: 'ws', externals })
.target('compiled/ws')
}
externals['path-to-regexp'] = 'next/dist/compiled/path-to-regexp' externals['path-to-regexp'] = 'next/dist/compiled/path-to-regexp'
export async function path_to_regexp(task, opts) { export async function path_to_regexp(task, opts) {
await task await task
...@@ -871,6 +880,7 @@ export async function ncc(task, opts) { ...@@ -871,6 +880,7 @@ export async function ncc(task, opts) {
'ncc_webpack_bundle_packages', 'ncc_webpack_bundle_packages',
'ncc_webpack_sources1', 'ncc_webpack_sources1',
'ncc_webpack_sources3', 'ncc_webpack_sources3',
'ncc_ws',
'ncc_mini_css_extract_plugin', 'ncc_mini_css_extract_plugin',
], ],
opts opts
......
...@@ -218,6 +218,10 @@ declare module 'next/dist/compiled/web-vitals' { ...@@ -218,6 +218,10 @@ declare module 'next/dist/compiled/web-vitals' {
import m from 'web-vitals' import m from 'web-vitals'
export = m export = m
} }
declare module 'next/dist/compiled/ws' {
import m from 'ws'
export = m
}
declare module 'next/dist/compiled/comment-json' { declare module 'next/dist/compiled/comment-json' {
import m from 'comment-json' import m from 'comment-json'
......
export default () => <div>API - conflict</div> export default function Page() {
return <div>API - conflict</div>
}
export default () => <div>API - support</div> export default function Page() {
return <div>API - support</div>
}
export default () => <div>User</div> export default function Page() {
return <div>User</div>
}
...@@ -14,6 +14,7 @@ import { ...@@ -14,6 +14,7 @@ import {
nextExport, nextExport,
getPageFileFromBuildManifest, getPageFileFromBuildManifest,
getPageFileFromPagesManifest, getPageFileFromPagesManifest,
check,
} from 'next-test-utils' } from 'next-test-utils'
import json from '../big.json' import json from '../big.json'
...@@ -479,8 +480,10 @@ function runTests(dev = false) { ...@@ -479,8 +480,10 @@ function runTests(dev = false) {
await fetchViaHTTP(appPort, '/api/test-no-end', undefined, { await fetchViaHTTP(appPort, '/api/test-no-end', undefined, {
signal: controller.signal, signal: controller.signal,
}).catch(() => {}) }).catch(() => {})
expect(stderr).toContain(
`API resolved without sending a response for /api/test-no-end, this may result in stalled requests.` await check(
() => stderr,
/API resolved without sending a response for \/api\/test-no-end, this may result in stalled requests/
) )
}) })
......
/* eslint-env jest */ /* eslint-env jest */
import WebSocket from 'ws'
import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
import AbortController from 'abort-controller' import webdriver from 'next-webdriver'
import { import {
renderViaHTTP, renderViaHTTP,
fetchViaHTTP,
findPort, findPort,
launchApp, launchApp,
killApp, killApp,
...@@ -20,22 +18,9 @@ const appDir = join(__dirname, '../') ...@@ -20,22 +18,9 @@ const appDir = join(__dirname, '../')
const context = {} const context = {}
const doPing = (page) => { const doPing = (page) => {
const controller = new AbortController() return new Promise((resolve) => {
const signal = controller.signal context.ws.onmessage = () => resolve()
return fetchViaHTTP( context.ws.send(JSON.stringify({ event: 'ping', page }))
context.appPort,
'/_next/webpack-hmr',
{ page },
{ signal }
).then((res) => {
res.body.on('data', (chunk) => {
try {
const payload = JSON.parse(chunk.toString().split('data:')[1])
if (payload.success || payload.invalid) {
controller.abort()
}
} catch (_) {}
})
}) })
} }
...@@ -44,8 +29,17 @@ describe('On Demand Entries', () => { ...@@ -44,8 +29,17 @@ describe('On Demand Entries', () => {
beforeAll(async () => { beforeAll(async () => {
context.appPort = await findPort() context.appPort = await findPort()
context.server = await launchApp(appDir, context.appPort) context.server = await launchApp(appDir, context.appPort)
await new Promise((resolve) => {
context.ws = new WebSocket(
`ws://localhost:${context.appPort}/_next/webpack-hmr`
)
context.ws.on('open', () => resolve())
context.ws.on('error', console.error)
})
}) })
afterAll(() => { afterAll(() => {
context.ws.close()
killApp(context.server) killApp(context.server)
}) })
......
...@@ -4867,6 +4867,13 @@ ...@@ -4867,6 +4867,13 @@
"@types/source-list-map" "*" "@types/source-list-map" "*"
source-map "^0.6.1" source-map "^0.6.1"
"@types/ws@8.2.0":
version "8.2.0"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.2.0.tgz#75faefbe2328f3b833cb8dc640658328990d04f3"
integrity sha512-cyeefcUCgJlEk+hk2h3N+MqKKsPViQgF5boi9TTHSK+PoR9KWBb/C5ccPcDyAqgsbAYHTwulch725DV84+pSpg==
dependencies:
"@types/node" "*"
"@types/yargs-parser@*": "@types/yargs-parser@*":
version "13.1.0" version "13.1.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-13.1.0.tgz#c563aa192f39350a1d18da36c5a8da382bbd8228" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-13.1.0.tgz#c563aa192f39350a1d18da36c5a8da382bbd8228"
...@@ -17277,6 +17284,7 @@ scheduler@^0.20.2: ...@@ -17277,6 +17284,7 @@ scheduler@^0.20.2:
ajv-keywords "^3.5.2" ajv-keywords "^3.5.2"
"schema-utils3@npm:schema-utils@3.0.0", schema-utils@^3.0.0: "schema-utils3@npm:schema-utils@3.0.0", schema-utils@^3.0.0:
name schema-utils3
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-3.0.0.tgz#67502f6aa2b66a2d4032b4279a2944978a0913ef" resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-3.0.0.tgz#67502f6aa2b66a2d4032b4279a2944978a0913ef"
integrity sha512-6D82/xSzO094ajanoOSbe4YvXWMfn2A//8Y1+MUqFAJul5Bs+yn36xbK9OtNDcRVSBJ9jjeoXftM6CfztsjOAA== integrity sha512-6D82/xSzO094ajanoOSbe4YvXWMfn2A//8Y1+MUqFAJul5Bs+yn36xbK9OtNDcRVSBJ9jjeoXftM6CfztsjOAA==
...@@ -19989,6 +19997,11 @@ write-pkg@^4.0.0: ...@@ -19989,6 +19997,11 @@ write-pkg@^4.0.0:
type-fest "^0.4.1" type-fest "^0.4.1"
write-json-file "^3.2.0" write-json-file "^3.2.0"
ws@8.2.3:
version "8.2.3"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.2.3.tgz#63a56456db1b04367d0b721a0b80cae6d8becbba"
integrity sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==
ws@>=7.4.6: ws@>=7.4.6:
version "8.2.0" version "8.2.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.2.0.tgz#0b738cd484bfc9303421914b11bb4011e07615bb" resolved "https://registry.yarnpkg.com/ws/-/ws-8.2.0.tgz#0b738cd484bfc9303421914b11bb4011e07615bb"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment