Skip to content

Commit

Permalink
fix: fixed import issue for edge
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Oct 14, 2023
1 parent 2bdfb4a commit 4a3f289
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 35 deletions.
5 changes: 3 additions & 2 deletions packages/next/src/lib/download-swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 31 additions & 29 deletions packages/next/src/server/node-polyfill-web-streams.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 1 addition & 1 deletion packages/next/src/server/pipe-readable.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ let postProcessHTML: typeof import('./post-process').postProcessHTML

const DOCTYPE = '<!DOCTYPE html>'

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit 4a3f289

Please sign in to comment.