next.js icon indicating copy to clipboard operation
next.js copied to clipboard

middleware matcher should support template literals

Open stefanprobst opened this issue 2 years ago • 15 comments

Link to the code that reproduces this issue

https://github.com/stefanprobst/issue-next-middleware-matcher

To Reproduce

  1. clone https://github.com/stefanprobst/issue-next-middleware-matcher
  2. pnpm install && pnpm run dev
  3. see error message in console: 'Next.js can't recognize the exported config field in route "/middleware": Unsupported template literal with expressions at "config.matcher[1]".'

Current vs. Expected behavior

i would want to be able to do this:

const locales = ["de", "en"] as const;

export const config = {
  matcher: ["/", `/(${locales.join("|")})/:path*`],
};

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jul 13 16:27:29 UTC 2
Binaries:
  Node: 18.18.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.8.0
Relevant Packages:
  next: 13.5.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware / Edge (API routes, runtime)

Additional context

No response

stefanprobst avatar Oct 04 '23 06:10 stefanprobst

Also experiencing this issue. Is there an update on this?

Davy-F avatar Nov 23 '23 09:11 Davy-F

Same issue here, any update?

Edit by maintainers: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

bobvdvalk avatar Dec 06 '23 13:12 bobvdvalk

Same issue here...

Edit by maintainers: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

GVALFER avatar Dec 07 '23 12:12 GVALFER

same here, as I'd like to organize some complex matcher rules with js. then I faced this issue.

hydRAnger avatar Dec 27 '23 07:12 hydRAnger

A simpler reproduction:

const base = '/api';
export const config = {
  matcher: `${base}/:path*`,
};

Substituting ${base} with the string directly works.

arduano avatar Dec 29 '23 05:12 arduano

Same issue here:

import createMiddleware from "next-intl/middleware";

import { DEFAULT_LOCALE, LOCALES, LOCALES_TO_MATCH } from "./common/constants";

export default createMiddleware({
  locales: LOCALES,

  defaultLocale: DEFAULT_LOCALE,
});

export const config = {
  matcher: [
    "/",
    `/(${LOCALES_TO_MATCH})/:path*`,
    "/((?!_next|_vercel|.*\\..*).*)",
  ],
};

Yeah, same issue here, required for most of the current internationalization packages.

SalahAdDin avatar Feb 01 '24 20:02 SalahAdDin

fwiw, these docs do state pretty clearly that you cannot do this https://nextjs.org/docs/messages/invalid-page-config

That being said, I'm not sure why and I ended up here because I want to be able to

gmbrown avatar Feb 10 '24 04:02 gmbrown

fwiw, these docs do state pretty clearly that you cannot do this https://nextjs.org/docs/messages/invalid-page-config

That being said, I'm not sure why and I ended up here because I want to be able to

We need a workaround on it, since, just for instance, our project will contain a minimum of 30 locales, and we cannot add them manually.

SalahAdDin avatar Feb 10 '24 04:02 SalahAdDin

Is there any way to do this? Having a lot of locales and updating them manually is too much hassle.

gg-hsi avatar Mar 19 '24 11:03 gg-hsi

A simpler reproduction:

const base = '/api';
export const config = {
  matcher: `${base}/:path*`,
};

Substituting ${base} with the string directly works.

You say this solution works, right?

SalahAdDin avatar Mar 19 '24 15:03 SalahAdDin

Can somebody remind us when there is support for string matutation? As of today, there is no support.

aryankarim avatar Apr 18 '24 22:04 aryankarim