question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find name 'URLPattern'

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
szavericommented, 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]> {}
2reactions
jonahallibonecommented, Jun 29, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot find name 'URL' error in TypeScript | bobbyhadz
To solve the "Cannot find name URL" error, make sure to add the DOM string to the lib array in your tsconfig.json file...
Read more >
TypeScript getting error TS2304: cannot find name ' require'
I am getting the error "TS2304: Cannot find name 'require' " when I attempt to transpile a simple TypeScript Node.js page. I have...
Read more >
urlpattern-polyfill - npm
Polyfill for the URLPattern API. Latest version: 6.0.2, last published: a month ago. Start using urlpattern-polyfill in your project by ...
Read more >
URL Pattern API - MDN Web Docs
When the URLPattern() constructor is called with a structured object of component patterns any missing components default to the * wildcard ...
Read more >
django.urls utility functions
viewname can be a URL pattern name or the callable view object. ... object # (This is discouraged because you can't reverse namespaced...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found