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

[Docs]: Next.js Middleware Matcher for next-intl does not handle internal routes like /dashboard #1701

Open
alireza-asgharii opened this issue Feb 5, 2025 · 2 comments
Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.

Comments

@alireza-asgharii
Copy link

alireza-asgharii commented Feb 5, 2025

Link to page

https://next-intl.dev/docs/getting-started/app-router/with-i18n-routing#middleware

Describe the problem

Currently, when using next-intl and internationalized routes with matcher for handling languages (like /fa or /en), internal routes such as /dashboard or any other routes related to different parts of the application are not processed by the middleware. As a result, next-intl fails to recognize and handle these routes properly, causing users to not get redirected to the correct language.

Current middleware.ts Configuration:

export const config = {
  matcher: ["/", "/(fa|en)/:path*"]
};

Problem:

Internal routes like /dashboard are not included in the matcher.
This causes users to not be properly directed to the correct language path when accessing internal pages, and the multi-language setup doesn’t work as expected.

Proposed Solution:
To fix this issue, we need to adjust the matcher configuration so that it includes both internationalized paths and internal routes like /dashboard. Using Regex Negative Lookahead will help us ignore specific paths (like API routes, static files, and meta files) while ensuring that other internal routes are included for processing.

export const config = { matcher: [ "/", "/(fa|en)/:path*", // Internationalized routes "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)" // Exclude specific paths ] };

Explanation of the Solution:

"/" → Covers the home page (root of the site).
"/(fa|en)/:path*" → Handles all internationalized routes (e.g., /fa/dashboard or /en/dashboard).
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)" → Ensures that internal routes like /dashboard are covered while excluding unnecessary paths such as API routes, static files, and meta files.

Outcome:

With this change, all routes are properly processed by next-intl, and users will be able to use the site in multiple languages without issues.

@alireza-asgharii alireza-asgharii added documentation Improvements or additions to documentation unconfirmed Needs triage. labels Feb 5, 2025
@amannn
Copy link
Owner

amannn commented Feb 5, 2025

This is already documented here: Matcher config for pathnames without a locale prefix.

I'm thinking about switching the default matcher that's shown in the docs though to this:

/((?!api|_next|_vercel|.*\\..*).*)

Maybe that could work better for users, but there's also a footgun here (as mentioned in the docs).

@amannn amannn mentioned this issue Feb 5, 2025
8 tasks
@simpleneeraj
Copy link

Hey Thank you works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

3 participants