Skip to content

Commit 993766b

Browse files
fix: assign globals to self (#1823)
* fix: assign globals to `self` * chore: add demo * chore: better wording * chore: better wording * fix: fix TypeError * style: lint Co-authored-by: Erica Pisani <[email protected]>
1 parent e915f1f commit 993766b

File tree

5 files changed

+80
-39
lines changed

5 files changed

+80
-39
lines changed

demos/middleware/middleware.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { NextResponse } from 'next/server'
22
import { MiddlewareRequest, NextRequest } from '@netlify/next'
33

4+
// Next.js replaces this with a stub polyfill. This import is just to test that stub.
5+
import pointlessFetch from 'isomorphic-unfetch'
6+
47
export async function middleware(req: NextRequest) {
58
let response
69
const { pathname } = req.nextUrl
@@ -58,6 +61,12 @@ export async function middleware(req: NextRequest) {
5861
}
5962

6063
if (pathname.startsWith('/shows')) {
64+
if (pathname.startsWith('/shows/222')) {
65+
response = NextResponse.next()
66+
const res = await pointlessFetch('http://www.example.com/')
67+
response.headers.set('x-example-server', res.headers.get('server'))
68+
}
69+
6170
if (pathname.startsWith('/shows/rewrite-absolute')) {
6271
response = NextResponse.rewrite(new URL('/shows/100', req.url))
6372
response.headers.set('x-modified-in-rewrite', 'true')

demos/middleware/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@netlify/next": "*",
1313
"@netlify/plugin-nextjs": "*",
14+
"isomorphic-unfetch": "^3.1.0",
1415
"next": "^13.0.3",
1516
"react": "^18.2.0",
1617
"react-dom": "^18.2.0"
@@ -24,4 +25,4 @@
2425
"npm-run-all": "^4.1.5",
2526
"typescript": "^4.6.3"
2627
}
27-
}
28+
}

package-lock.json

+54-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/helpers/edge.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ import {
8585
// Deno defines "window", but naughty libraries think this means it's a browser
8686
delete globalThis.window
8787
globalThis.process = { env: {...Deno.env.toObject(), NEXT_RUNTIME: 'edge', 'NEXT_PRIVATE_MINIMAL_MODE': '1' } }
88-
// Next uses "self" as a function-scoped global-like object
89-
const self = {}
9088
let _ENTRIES = {}
9189
9290
// Next.js uses this extension to the Headers API implemented by Cloudflare workerd
@@ -118,6 +116,10 @@ const fetch = async (url, init) => {
118116
}
119117
}
120118
119+
// Next edge runtime uses "self" as a function-scoped global-like object, but some of the older polyfills expect it to equal globalThis
120+
// See https://nextjs.org/docs/basic-features/supported-browsers-features#polyfills
121+
const self = { ...globalThis, fetch }
122+
121123
`
122124

123125
// Slightly different spacing in different versions!

packages/runtime/src/templates/edge/next-dev.js

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import { buildResponse } from '../edge-shared/utils.ts'
66
globalThis.NFRequestContextMap ||= new Map()
77
globalThis.__dirname = fromFileUrl(new URL('./', import.meta.url)).slice(0, -1)
88

9+
// Next.js uses this extension to the Headers API implemented by Cloudflare workerd
10+
if (!('getAll' in Headers.prototype)) {
11+
Headers.prototype.getAll = function getAll(name) {
12+
name = name.toLowerCase()
13+
if (name !== 'set-cookie') {
14+
throw new Error('Headers.getAll is only supported for Set-Cookie')
15+
}
16+
return [...this.entries()].filter(([key]) => key === name).map(([, value]) => value)
17+
}
18+
}
19+
920
// Check if a file exists, given a relative path
1021
const exists = async (relativePath) => {
1122
const path = fromFileUrl(new URL(relativePath, import.meta.url))

0 commit comments

Comments
 (0)