Unverified Commit 7bfd5829 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure WebSocket is polyfilled for node runtime as well (#48924)

Follow-up to https://github.com/vercel/next.js/pull/48870 this exposes
WebSocket for the Next.js runtime so we don't have divergence in APIs as
discussed with @cramforce
parent d922e3d4
...@@ -59,10 +59,7 @@ import { StaticGenerationAsyncStorageWrapper } from '../server/async-storage/sta ...@@ -59,10 +59,7 @@ import { StaticGenerationAsyncStorageWrapper } from '../server/async-storage/sta
import { IncrementalCache } from '../server/lib/incremental-cache' import { IncrementalCache } from '../server/lib/incremental-cache'
import { patchFetch } from '../server/lib/patch-fetch' import { patchFetch } from '../server/lib/patch-fetch'
import { nodeFs } from '../server/lib/node-fs-methods' import { nodeFs } from '../server/lib/node-fs-methods'
import '../server/node-environment'
// expose AsyncLocalStorage on global for react usage
const { AsyncLocalStorage } = require('async_hooks')
;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage
export type ROUTER_TYPE = 'pages' | 'app' export type ROUTER_TYPE = 'pages' | 'app'
......
...@@ -12,6 +12,7 @@ import type { OutgoingHttpHeaders } from 'http' ...@@ -12,6 +12,7 @@ import type { OutgoingHttpHeaders } from 'http'
// Polyfill fetch for the export worker. // Polyfill fetch for the export worker.
import '../server/node-polyfill-fetch' import '../server/node-polyfill-fetch'
import '../server/node-environment'
import { extname, join, dirname, sep, posix } from 'path' import { extname, join, dirname, sep, posix } from 'path'
import fs, { promises } from 'fs' import fs, { promises } from 'fs'
...@@ -119,10 +120,6 @@ interface RenderOpts { ...@@ -119,10 +120,6 @@ interface RenderOpts {
strictNextHead?: boolean strictNextHead?: boolean
} }
// expose AsyncLocalStorage on globalThis for react usage
const { AsyncLocalStorage } = require('async_hooks')
;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage
export default async function exportPage({ export default async function exportPage({
parentSpanId, parentSpanId,
path, path,
......
import type { NextConfigComplete } from '../config-shared' import type { NextConfigComplete } from '../config-shared'
import type { AppRouteUserlandModule } from '../future/route-modules/app-route/module' import type { AppRouteUserlandModule } from '../future/route-modules/app-route/module'
import '../../server/require-hook' import '../require-hook'
import '../node-polyfill-fetch' import '../node-polyfill-fetch'
import '../node-environment'
import { import {
buildAppStaticPaths, buildAppStaticPaths,
buildStaticPaths, buildStaticPaths,
...@@ -17,10 +18,6 @@ import { staticGenerationAsyncStorage } from '../../client/components/static-gen ...@@ -17,10 +18,6 @@ import { staticGenerationAsyncStorage } from '../../client/components/static-gen
type RuntimeConfig = any type RuntimeConfig = any
// expose AsyncLocalStorage on globalThis for react usage
const { AsyncLocalStorage } = require('async_hooks')
;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage
// we call getStaticPaths in a separate process to ensure // we call getStaticPaths in a separate process to ensure
// side-effects aren't relied on in dev that will break // side-effects aren't relied on in dev that will break
// during a production build // during a production build
......
...@@ -6,3 +6,16 @@ if (typeof (globalThis as any).AsyncLocalStorage !== 'function') { ...@@ -6,3 +6,16 @@ if (typeof (globalThis as any).AsyncLocalStorage !== 'function') {
const { AsyncLocalStorage } = require('async_hooks') const { AsyncLocalStorage } = require('async_hooks')
;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage ;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage
} }
if (typeof (globalThis as any).WebSocket !== 'function') {
let WebSocket
// undici's WebSocket handling is only available in Node.js >= 18
// so fallback to using ws for v16
if (Number(process.version.split('.')[0].substring(1)) < 18) {
WebSocket = require('next/dist/compiled/ws').WebSocket
} else {
WebSocket = require('next/dist/compiled/undici').WebSocket
}
;(globalThis as any).WebSocket = WebSocket
}
...@@ -235,7 +235,17 @@ async function createModuleContext(options: ModuleContextOptions) { ...@@ -235,7 +235,17 @@ async function createModuleContext(options: ModuleContextOptions) {
? { strings: true, wasm: true } ? { strings: true, wasm: true }
: undefined, : undefined,
extend: (context) => { extend: (context) => {
context.WebSocket = require('next/dist/compiled/undici').WebSocket let WebSocket
// undici's WebSocket handling is only available in Node.js >= 18
// so fallback to using ws for v16
if (Number(process.version.split('.')[0].substring(1)) < 18) {
WebSocket = require('next/dist/compiled/ws').WebSocket
} else {
WebSocket = require('next/dist/compiled/undici').WebSocket
}
context.WebSocket = WebSocket
context.process = createProcessPolyfill(options) context.process = createProcessPolyfill(options)
Object.defineProperty(context, 'require', { Object.defineProperty(context, 'require', {
......
...@@ -7,6 +7,10 @@ const helloHandler = async ( ...@@ -7,6 +7,10 @@ const helloHandler = async (
): Promise<Response> => { ): Promise<Response> => {
const { pathname } = request.nextUrl const { pathname } = request.nextUrl
if (typeof WebSocket === 'undefined') {
throw new Error('missing WebSocket constructor!!')
}
return new Response('hello, world', { return new Response('hello, world', {
headers: withRequestMeta({ headers: withRequestMeta({
method: request.method, method: request.method,
......
export function generateStaticParams() { export function generateStaticParams() {
if (typeof WebSocket === 'undefined') {
throw new Error('missing WebSocket constructor!!')
}
return [{ lang: 'en' }, { lang: 'fr' }] return [{ lang: 'en' }, { lang: 'fr' }]
} }
......
export default function DeploymentsInfoPage(props) { export default function DeploymentsInfoPage(props) {
if (typeof WebSocket === 'undefined') {
throw new Error('missing WebSocket constructor!!')
}
return ( return (
<> <>
<p>hello from app/dashboard/deployments/info</p> <p>hello from app/dashboard/deployments/info</p>
......
import ClientComp from './client-comp-client' import ClientComp from './client-comp-client'
export default function DashboardPage(props) { export default function DashboardPage(props) {
if (typeof WebSocket === 'undefined') {
throw new Error('missing WebSocket constructor!!')
}
return ( return (
<> <>
<p id="from-dashboard" className="p"> <p id="from-dashboard" className="p">
......
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