Skip to content

fix: assign globals to self #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { NextResponse } from 'next/server'
import { MiddlewareRequest, NextRequest } from '@netlify/next'

// Next.js replaces this with a stub polyfill. This import is just to test that stub.
import pointlessFetch from 'isomorphic-unfetch'

export async function middleware(req: NextRequest) {
let response
const { pathname } = req.nextUrl
Expand Down Expand Up @@ -58,6 +61,12 @@ export async function middleware(req: NextRequest) {
}

if (pathname.startsWith('/shows')) {
if (pathname.startsWith('/shows/222')) {
response = NextResponse.next()
const res = await pointlessFetch('http://www.example.com/')
response.headers.set('x-example-server', res.headers.get('server'))
}

if (pathname.startsWith('/shows/rewrite-absolute')) {
response = NextResponse.rewrite(new URL('/shows/100', req.url))
response.headers.set('x-modified-in-rewrite', 'true')
Expand Down
3 changes: 2 additions & 1 deletion demos/middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@netlify/next": "*",
"@netlify/plugin-nextjs": "*",
"isomorphic-unfetch": "^3.1.0",
"next": "^13.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand All @@ -24,4 +25,4 @@
"npm-run-all": "^4.1.5",
"typescript": "^4.6.3"
}
}
}
90 changes: 54 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ import {
// Deno defines "window", but naughty libraries think this means it's a browser
delete globalThis.window
globalThis.process = { env: {...Deno.env.toObject(), NEXT_RUNTIME: 'edge', 'NEXT_PRIVATE_MINIMAL_MODE': '1' } }
// Next uses "self" as a function-scoped global-like object
const self = {}
let _ENTRIES = {}

// Next.js uses this extension to the Headers API implemented by Cloudflare workerd
Expand Down Expand Up @@ -118,6 +116,9 @@ const fetch = async (url, init) => {
}
}

// Next edge runtime uses "self" as a function-scoped global-like object, but some of the older polyfills expect it to equal globalThis
const self = { ...globalThis, fetch }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could we add a permalink to where the Next stub polyfill replacement occurs in case we need to investigate something related to it in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a link to the docs


`

// Slightly different spacing in different versions!
Expand Down