next.js: [middlewares] globalThis and globalThis.CryptoKey is missing
What version of Next.js are you using?
12.0.1
What version of Node.js are you using?
v14.18.0
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Both Vercel and next dev
Describe the Bug
Testing a universal JOSE (JWT/JWK/etc) library on Middlewares I’ve encountered an issue where the CryptoKey constructor is not exposed via globalThis.CryptoKey.
This is the case for all browsers, Deno, and Cloudflare Workers runtimes but not for Middlewares deployed via vercel or running via next dev
.
Not having access to this constructor prohibits my particular library from validating user inputs.
In next dev
globalThis.CryptoKey is missing, in vercel.com runtime globalThis
is missing completely.
Expected Behavior
globalThis
, globalThis.CryptoKey
, and CryptoKey
are exposed both running via next dev
or deployed via vercel.com
To Reproduce
Create a middleware like so, both in development as well as vercel.com, observe the failures are there and also, different.
export default async function middleware(req, ev) {
try {
if (typeof CryptoKey === 'undefined') {
throw new Error('CryptoKey is missing')
}
if (typeof globalThis === 'undefined') {
throw new Error('globalThis is missing')
}
if (!globalThis.CryptoKey) {
throw new Error('globalThis.CryptoKey is missing')
}
return new Response(JSON.stringify({}), { status: 200 });
} catch (err) {
return new Response(JSON.stringify({ error: err.stack }), { status: 400 });
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 15 (13 by maintainers)
Commits related to this issue
- fix(middleware): expose `CryptoKey` and `globalThis.CryptoKey` (#31193) closes https://github.com/vercel/next.js/issues/30475 — committed to vercel/next.js by Kikobeats 3 years ago
- fix(middleware): expose `CryptoKey` and `globalThis.CryptoKey` (#31193) closes https://github.com/vercel/next.js/issues/30475 — committed to natew/next.js by Kikobeats 3 years ago
@panva this has now been rolled to stable on Vercel, please re-deploy and give a try!