amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.49k stars 226 forks source link

[Docs]: Middleware documentation is too light #1022

Closed ScreamZ closed 6 months ago

ScreamZ commented 6 months ago

Link to page

https://next-intl-docs.vercel.app/docs/getting-started/app-router#middleware

Describe the problem

Currently you're indicating that we just need to define

import createMiddleware from 'next-intl/middleware';

export default createMiddleware({
  // A list of all locales that are supported
  locales: ['en', 'de'],

  // Used when no locale matches
  defaultLocale: 'en'
});

export const config = {
  // Match only internationalized pathnames
  matcher: ['/', '/(de|en)/:path*']
};

I wanted to use a dynamic configuration, but this is a nextjs limitation it's impossible to write something like

`/(${AvailableLocales.join("|")})/:path*`

Therefore I would suggest to improve the documentation to specify this to those who will try like me

Also I'm giving another matcher matcher: ["/((?!api|_next/static|_next/image|site.webmanifest|favicon).*)"] that seems to work.

amannn commented 6 months ago

There are detailed docs about this in the matcher config section (see also "Can I avoid hardcoding the locales in the matcher config?").

Unfortunately there's no ideal matcher that I know of, the one we suggest in the docs is known to have the least amount of downsides from my experience. If the one you're suggesting works well for your app, that's absolutely fine, just make sure that if you're adding anything to the public folder that you want to update your matcher.