-
Notifications
You must be signed in to change notification settings - Fork 86
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
Conversation
✅ Deploy Preview for netlify-plugin-nextjs-export-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for nextjs-plugin-custom-routes-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-canary ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-edge-middleware ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-i18next-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small thing.
I also tried tested in a local environment (ntl dev
) and ran into a TypeError
:
TypeError: this._headers.getAll is not a function
full stack trace for debugging:
[next-dev] TypeError: this._headers.getAll is not a function
at new ResponseCookies (file:///Users/ericapisani/netlify/next-runtime/demos/middleware/.netlify/middleware.js#10:809:39)
at new NextResponse2 (file:///Users/ericapisani/netlify/next-runtime/demos/middleware/.netlify/middleware.js#10:998:20)
at Function.next (file:///Users/ericapisani/netlify/next-runtime/demos/middleware/.netlify/middleware.js#10:1053:16)
at middleware (file:///Users/ericapisani/netlify/next-runtime/demos/middleware/.netlify/middleware.js#10:1632:45)
at Object.handler [as function] (file:///Users/ericapisani/netlify/next-runtime/demos/middleware/.netlify/edge-functions/next-dev/index.js:75:28)
at async FunctionChain.runFunction (https://637cf7ce9214b300099b3aa8--edge.netlify.app/bootstrap/function_chain.ts:298:24)
at async FunctionChain.run (https://637cf7ce9214b300099b3aa8--edge.netlify.app/bootstrap/function_chain.ts:251:22)
at async handleRequest (https://637cf7ce9214b300099b3aa8--edge.netlify.app/bootstrap/handler.ts:59:22)
at async Server.#respond (https://deno.land/[email protected]/http/server.ts:298:18)
@@ -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 } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@ericapisani Was that with |
818aa89
to
7b4fff8
Compare
@ascorbic It wasn't when I originally posted my comment but trying it again with |
Summary
Next.js middleware and edge functions define
self
as a global-like, function-scoped object. However some of Next's built-in polyfill stubs expectself
to have browser globals. These polyfills aren't supposed to be needed in edge runtime, but Next automatically includes them if a site has certain dependencies (e.g.unfetch
). This means that for those sites, edge functions were failing because those globals were missing.This PR adds globals to
self
, meaning they're available if the polyfill needs them.Test plan
This PR adds
isomorphic-unfetch
to the middleware demo site, to simulate this bug. To test it, visit the middleware demo and load/shows/222
. The response headers should includex-example-server
.Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal
Fixes https://github.com/netlify/pod-ecosystem-frameworks/issues/315
Standard checks:
🧪 Once merged, make sure to update the version if needed and that it was published correctly.