amannn / next-intl

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

Middleware enhancements #1362

Open amannn opened 1 day ago

amannn commented 1 day ago

Is your feature request related to a problem? Please describe.

It seems like there's work happening on the Next.js side on a middleware revamp: https://github.com/vercel/next.js/discussions/46722#discussioncomment-10262088

I'm using this thread to collect aspects about the current next-intl middleware that we could potentially address in case a revamp is relevant after the Next.js change.

Describe the solution you'd like

My personal wishlist for Next.js:

  1. Passing a locale to the rendering phase (see https://github.com/vercel/next.js/discussions/58862). Ideally this would be possible without a middleware in the first place though.
  2. A dynamically created matcher

Mentioned both here.

Separately from this, there are some use cases we could support better that do not rely on Next.js:

  1. https://github.com/amannn/next-intl/issues/1268
  2. https://github.com/amannn/next-intl/issues/454
  3. https://github.com/amannn/next-intl/discussions/1356

We can make these changes in any case, but in case a refactoring is necessary after the Next.js change, maybe it'd be worth waiting a bit.

apavlinovic commented 14 hours ago

We have a big issue with next-intl, which is preventing us from using a middleware today at all.

Here's our issue, maybe this is something that can be addressed in a future update for middlewares, since it's preventing us from recommending next-intl for public production projects:

I just want to highlight a few things about the current behaviour of the next-intl's request configuration:

You can test this for yourself, just add a few more messages to your example-app-router and see how fast the middleware file grows. If you can't think of text, use Lorem Ipsum.

For us, this is the number one problem with your library, and it is very hard to recommend it to anyone - this is simply not scalable, next-intl is accidentally killing a built in NextJS feature.

Middlewares should never be aware of your translation messages, less likely bundle them in their execution code. A middleware gets executed for EVERY request, this is pure insanity.

amannn commented 33 minutes ago

@apavlinovic The issue you've linked to was closed due to inactivity from your side.

I just tried adding a very lengthy message to example-app-router and did not see any increase in middleware size, which btw. is currently at 41.1 kB. The messages are definitely not imported into the middleware. I'm not sure if you're using a special build configuration or you're accidentally importing the messages into the middleware due to your import graph.

If you'd like to get to the bottom of this, please create a new issue using the bug report template and provide a reliable reproduction based on the provided template.