Skip to content
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

Closed
1 task done
jonahallibone opened this issue Jun 28, 2022 · 11 comments · Fixed by #39219
Closed
1 task done

Cannot find name 'URLPattern' #38131

jonahallibone opened this issue Jun 28, 2022 · 11 comments · Fixed by #39219
Assignees
Labels
Runtime Related to Node.js or Edge Runtime with Next.js. TypeScript Related to types with Next.js.

Comments

@jonahallibone
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
    Binaries:
      Node: 16.13.1
      npm: 8.9.0
      Yarn: 1.22.10
      pnpm: 4.14.4
    Relevant packages:
      next: 12.2.0
      eslint-config-next: 12.2.0
      react: 18.2.0
      react-dom: 18.2.0

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

const { page }  = req
...
const isActualPage = !!page.name

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 next 12.2 I had begun following the guide found here to upgrade my application to use URLPattern 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 on URLPattern at all in other applications, so debugging as been tough.

Screen Shot 2022-06-28 at 6 26 47 PM

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 globals

Link to reproduction

https://codesandbox.io/s/ecstatic-marco-s3lo6n?file=/middleware.ts

To Reproduce

  1. Create a middleware.ts in your src or root folder
  2. Try and use new URLPattern()
@jonahallibone jonahallibone added the bug Issue was opened via the bug report template. label Jun 28, 2022
@SukkaW
Copy link
Contributor

SukkaW commented Jun 29, 2022

URLPattern is a new web standard that landed recently, check URLPattern - MDN and URLPattern - WICG.

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?

@jonahallibone
Copy link
Author

jonahallibone commented Jun 29, 2022

@SukkaW It would be great if you guys could add the TypeDef!

@balazsorban44 balazsorban44 added kind: bug TypeScript Related to types with Next.js. Runtime Related to Node.js or Edge Runtime with Next.js. and removed bug Issue was opened via the bug report template. labels Jun 29, 2022
@balazsorban44
Copy link
Member

balazsorban44 commented Jun 29, 2022

Related #38103. Thanks for reporting, we are reviewing this. 👍

@jonahallibone
Copy link
Author

jonahallibone commented Jun 29, 2022

@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

@balazsorban44
Copy link
Member

Already working on it!

@javivelasco
Copy link
Member

Thanks for reporting this! We thought of a nice solution and we are working on it like @balazsorban44 said. Coming ASAP!

@szaveri
Copy link

szaveri commented Jul 7, 2022

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 Pattern interface as well to match the output expected in https://nextjs.org/docs/messages/middleware-upgrade-guide#after-3

interface PatternHandlerResult {
  [key: string]: string | undefined
}
export interface Pattern extends Array<[URLPattern, (pathname: URLPatternResult) => PatternHandlerResult]> {}

@jonahallibone
Copy link
Author

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).

@Schniz Schniz self-assigned this Jul 28, 2022
@sannajammeh
Copy link
Contributor

sannajammeh commented Jul 30, 2022

Workaround:
npm i urlpattern-polyfill -D

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";
}

@kodiakhq kodiakhq bot closed this as completed in #39219 Aug 2, 2022
kodiakhq bot pushed a commit that referenced this issue Aug 2, 2022
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]>
@zenyr
Copy link

zenyr commented Aug 30, 2022

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)

Or you can safely add this to �globals.d.ts right next to next.config.js

/// <reference types="urlpattern-polyfill" />

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Runtime Related to Node.js or Edge Runtime with Next.js. TypeScript Related to types with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants