From 4a3f2894ce235435858682d0680249211331e49a Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Fri, 13 Oct 2023 19:26:52 -0600 Subject: [PATCH] fix: fixed import issue for edge --- packages/next/src/lib/download-swc.ts | 5 +- .../src/server/lib/incremental-cache/index.ts | 3 +- .../src/server/node-polyfill-web-streams.ts | 60 ++++++++++--------- packages/next/src/server/pipe-readable.ts | 2 +- packages/next/src/server/render.tsx | 3 +- .../stream-utils/node-web-streams-helper.ts | 2 +- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/packages/next/src/lib/download-swc.ts b/packages/next/src/lib/download-swc.ts index c92dad16b8f63..7c34a4ed11a4a 100644 --- a/packages/next/src/lib/download-swc.ts +++ b/packages/next/src/lib/download-swc.ts @@ -5,8 +5,9 @@ import tar from 'next/dist/compiled/tar' const { fetch } = require('next/dist/compiled/undici') as { fetch: typeof global.fetch } -import { WritableStream } from 'next/dist/compiled/@edge-runtime/ponyfill' - +const { WritableStream } = require('node:stream/web') as { + WritableStream: typeof global.WritableStream +} import { getRegistry } from './helpers/get-registry' import { getCacheDirectory } from './helpers/get-cache-directory' diff --git a/packages/next/src/server/lib/incremental-cache/index.ts b/packages/next/src/server/lib/incremental-cache/index.ts index 0d8927d4bc002..5a437a256b9d7 100644 --- a/packages/next/src/server/lib/incremental-cache/index.ts +++ b/packages/next/src/server/lib/incremental-cache/index.ts @@ -9,7 +9,8 @@ import FileSystemCache from './file-system-cache' import path from '../../../shared/lib/isomorphic/path' import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' -import { WritableStream } from 'next/dist/compiled/@edge-runtime/ponyfill' +import '../../node-polyfill-web-streams' + import { CACHE_ONE_YEAR, NEXT_CACHE_REVALIDATED_TAGS_HEADER, diff --git a/packages/next/src/server/node-polyfill-web-streams.ts b/packages/next/src/server/node-polyfill-web-streams.ts index ad34e5ab8fba8..0b08827d8cc80 100644 --- a/packages/next/src/server/node-polyfill-web-streams.ts +++ b/packages/next/src/server/node-polyfill-web-streams.ts @@ -1,33 +1,35 @@ -// Polyfill Web Streams for the Node.js runtime. -if (!global.ReadableStream) { - // In Node v16, ReadableStream is available natively but under the `stream` namespace. - // In Node v18+, it's available under global. - if (require('stream/web').ReadableStream) { - global.ReadableStream = require('stream/web').ReadableStream - } else { - const { ReadableStream } = - require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') - global.ReadableStream = ReadableStream +if (process.env.NEXT_RUNTIME !== 'edge') { + // Polyfill Web Streams for the Node.js runtime. + if (!global.ReadableStream) { + // In Node v16, ReadableStream is available natively but under the `stream` namespace. + // In Node v18+, it's available under global. + if (require('stream/web').ReadableStream) { + global.ReadableStream = require('stream/web').ReadableStream + } else { + const { ReadableStream } = + require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') + global.ReadableStream = ReadableStream + } } -} -if (!global.WritableStream) { - // In Node v16, WritableStream is available natively but under the `stream` namespace. - // In Node v18+, it's available under global. - if (require('stream/web').WritableStream) { - global.WritableStream = require('stream/web').WritableStream - } else { - const { WritableStream } = - require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') - global.WritableStream = WritableStream + if (!global.WritableStream) { + // In Node v16, WritableStream is available natively but under the `stream` namespace. + // In Node v18+, it's available under global. + if (require('stream/web').WritableStream) { + global.WritableStream = require('stream/web').WritableStream + } else { + const { WritableStream } = + require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') + global.WritableStream = WritableStream + } } -} -if (!global.TransformStream) { - // Same as ReadableStream above. - if (require('stream/web').TransformStream) { - global.TransformStream = require('stream/web').TransformStream - } else { - const { TransformStream } = - require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') - global.TransformStream = TransformStream + if (!global.TransformStream) { + // Same as ReadableStream above. + if (require('stream/web').TransformStream) { + global.TransformStream = require('stream/web').TransformStream + } else { + const { TransformStream } = + require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') + global.TransformStream = TransformStream + } } } diff --git a/packages/next/src/server/pipe-readable.ts b/packages/next/src/server/pipe-readable.ts index e7c5e326d64e1..9efb9c4966dea 100644 --- a/packages/next/src/server/pipe-readable.ts +++ b/packages/next/src/server/pipe-readable.ts @@ -1,6 +1,6 @@ import type { ServerResponse } from 'node:http' -import { WritableStream } from 'next/dist/compiled/@edge-runtime/ponyfill' +import './node-polyfill-web-streams' export function isAbortError(e: any): e is Error & { name: 'AbortError' } { return e?.name === 'AbortError' diff --git a/packages/next/src/server/render.tsx b/packages/next/src/server/render.tsx index fb17408808c98..0f70abe36a8a9 100644 --- a/packages/next/src/server/render.tsx +++ b/packages/next/src/server/render.tsx @@ -113,8 +113,9 @@ let postProcessHTML: typeof import('./post-process').postProcessHTML const DOCTYPE = '' +import './node-polyfill-web-streams' + if (process.env.NEXT_RUNTIME !== 'edge') { - require('./node-polyfill-web-streams') tryGetPreviewData = require('./api-utils/node/try-get-preview-data').tryGetPreviewData warn = require('../build/output/log').warn diff --git a/packages/next/src/server/stream-utils/node-web-streams-helper.ts b/packages/next/src/server/stream-utils/node-web-streams-helper.ts index be19de2a2feb4..53bfc58f86f49 100644 --- a/packages/next/src/server/stream-utils/node-web-streams-helper.ts +++ b/packages/next/src/server/stream-utils/node-web-streams-helper.ts @@ -1,6 +1,6 @@ import type { FlightRouterState } from '../app-render/types' -import { WritableStream } from 'next/dist/compiled/@edge-runtime/ponyfill' +import '../node-polyfill-web-streams' import { nonNullable } from '../../lib/non-nullable' import { getTracer } from '../lib/trace/tracer'