-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
Cannot find name 'URLPattern' #38131
Comments
Although it is supported by Next.js' edge runtime, it is not implemented in Node.js and TypeScript:
@Kikobeats Maybe we should include the type definition for URLPattern? |
@SukkaW It would be great if you guys could add the TypeDef! |
Related #38103. Thanks for reporting, we are reviewing this. 👍 |
@balazsorban44 it seems like that PR got closed without a merge, safe to say that we'll still get this in the future? Probably won't upgrade to 12.2 until the edge globals are available |
Already working on it! |
Thanks for reporting this! We thought of a nice solution and we are working on it like @balazsorban44 said. Coming ASAP! |
Recently upgraded to NextJS 12.2.0 and as a workaround for anyone looking to upgrade (plan to change this once this issue is resolved) import type { URLPattern } from 'types/urlpattern';
export const setPatterns = (pattern: string): Pattern => [
[
/* eslint-disable no-undef */
// @ts-expect-error
new URLPattern({ pathname: pattern }) as URLPattern,
({ pathname }) => pathname.groups,
],
]; with the type definition here https://github.com/kenchris/urlpattern-polyfill#api-reference I created a custom interface PatternHandlerResult {
[key: string]: string | undefined
}
export interface Pattern extends Array<[URLPattern, (pathname: URLPatternResult) => PatternHandlerResult]> {} |
Any update on this? I am going to implement @szaveri's solution ASAP because not upgrading our middleware is starting to affect our billing on Vercel (since changes to middleware quota's coincided with the 12.2.0 release). |
Workaround: In global.d.ts or any other d.ts file (make sure its a d.ts to avoid accidentally including this polyfill's JS code) declare global {
import URLPattern from "urlpattern-polyfill";
} |
This commit allows the users to import URLPattern from `next/server`, by defining a key that uses `global.URLPattern`. Why is this any good? or: why don't we add URLPattern to the global namespace? URLPattern is exposed as global on Edge Runtime _only_. This means that if we define a constructor in global namespace in our TypeScript definitions, people might have runtime errors in their Node.js functions. Importing from `next/server` enables users to get the constructor without risking in runtime errors and wrong type definitions. Keep in mind, that with the current implementation, we do not check if the constructor actually exists, but `next/server` shouldn't be imported in Node.js functions, AFAIK. ## Related - Fixes #38131 ## Bug - [x] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper <[email protected]>
Or you can safely add this to /// <reference types="urlpattern-polyfill" /> |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
vercel
Describe the Bug
I am trying to upgrade to the GA Middlware. I previously had used logic such as
to test against the URL in my middleware so i could be sure that I was only affecting urls that corresponded to my
pages
directory. Since this was removed in next12.2
I had begun following the guide found here to upgrade my application to useURLPattern
instead. I am, however, having trouble using that as VSCode and TS are telling that name doesn't exist. I am having a hard time finding any information onURLPattern
at all in other applications, so debugging as been tough.If there's something I need to add my global namespaces or otherwise, this just doesn't seem to work with the default Next.js TS config.
Expected Behavior
new URLPattern()
should be available in VSCode/TS globalsLink to reproduction
https://codesandbox.io/s/ecstatic-marco-s3lo6n?file=/middleware.ts
To Reproduce
middleware.ts
in your src or root foldernew URLPattern()
The text was updated successfully, but these errors were encountered: