Replies: 2 comments 6 replies
-
Have looked at other solution a bit too, while next-intl is my go-for for various reasons, I like the way some handle our issue: |
Beta Was this translation helpful? Give feedback.
-
Hey Andréas, thank you so, so much for the thoughtful post! I luckily didn't die suddenly while looking at it, so all good! 😄 This kind of feedback is immensely helpful—definitely keep it coming in case you have more! ❤️ I think you have a valid point here. The current design for Reading a locale from the params that are passed to That being said, I think the story is a different one for initializing the locale in case none was provided by the middleware. Use cases could be:
In regard to (1) I'd personally recommend an explicit contract like a search param In regard to (2), this would unlock two cases that have been somewhat tough to support so far:
An example snippet for using the default locale for such cases could look like this: import {routing} from './routing';
export default getRequestConfig(async ({ locale = routing.defaultLocale }) => {
// Validate that the incoming `locale` parameter is valid
if (!routing.locales.includes(locale as any)) notFound();
return {
locale,
// ....
}
}); As you're rightfully saying, probably not much to ask from a user, while providing a more frictionless experience, and at the same time providing more flexibility. The error "Unable to find next-intl locale because the middleware didn't run (…)" is currently one of the most annoying ones in The change would involve:
To consider:
One drawback that comes to my mind:
I probably need to prototype this to get a deeper understanding about the implications, but from what I can currently tell, this would be a welcome change! 💯 Regarding rollout we'd have to make this a breaking change, since the I'm making a note of this in #779! |
Beta Was this translation helpful? Give feedback.
-
Hi, first I would like to say that I'm a huge fan of all this work!
This post is my first rock on a series of thoughts about the current state of the library. I have a small Trello with things I've detected and I would like to report, here is one, that I encounter very often.
Hope you'll like the suggestion and not die suddenly when you'll see the length of the post 🤣 I'll try to be concise.
Important
First I would like to remember that I'm using a highly hybrid approach for the localized path and not the « full localized one ».
Note
I'm aware that the next-intl documentation invites us not to use this pattern as we already discussed it in another thread if I'm not wrong. It rather suggests to split the application into two apps or starting localizing API routes. But there are some edges cases like for Next auth.
When we chose that path, it's advised to have 0 code relying on next-intl automated function that gets locale, like
getLocale
without args, orLinks
.I'm trying my best when not in the internationalized section to not use those APIs, but sometimes as I'm using nested components or functions that rely on that elsewhere (even in RSC), I get shot silently and I discover my issues when they are in production (and a lot of users have cases that I didn't found or tested. People ends-up in Not Found page, and it's hard to track. I just spent 3 hours making a git diff to detect that 10 minutes ago.
I ended up making a kind of mimic of what the middleware does for those sections of the app that aren't in the i18n router context. But less aggressive
People might say:
I would like to, but the issue is that the middleware will automatically try to re-route if the path does not match a localized path. Which is useful most of the time, but damn annoying in my case.
Here are my thoughts about the problem and possible solutions I have, but I need some help and design! As I might not be aware of all the side effects this could have. You're the expect here @amannn after all! And I would be pleased to learn from you things ;)
Problem: Aggressive NotFound Redirection in Locale Middleware
When we call
getLocale
(or other APIs that rely on locale), if the middleware hasn’t been executed, anotFound()
is triggered immediately.This behavior feels a bit too aggressive to me, especially in cases where we would prefer to handle locale resolution more flexibly.
If we take the source of the problem, we need to execute the middleware to guess the locale, as I do with my
validateLocaleOrFallback
function above, except that in that one, I don't have access to the path name.If we look at the documentation without i18n routing we see a cool thing:
❗️However the issue is:
Currently, there’s no way to use
getRequestContext
to determine the final locale and return a different one. The locale middleware acts strictly and enforces its logic without room for adjustment.If you use
This doesn't work. Because you cannot read the locale and return another with the current setup. Why is this a restriction? I understand this is a guard. But with proper documentation and naming, this could be easy to not fail.
Suggested Solution
What I propose is a more flexible approach IMO where:
getRequestContext
to allow the user to determine the final locale.getRequestContext
, (we can even pass what matched: route/cookie/accept-Language header/fallback)For client-side calls, with the context provider, I guess we have the same logic. As it's prefilled with data coming from
getRequestContext
Another workaround I found is wrapping
getLocale()
with my custom code thattry/catch
around and fallback. But this still display the warning in the logs, its kind of hacky and doesn't solve the issue for API like the localizedLinks
.Benefits
notFound()
, the system allows for fallback or custom handling logic, making the application more user-friendly and adaptable to different use cases. We can even redirect to one page that invite you to choose the locale, and not to thenotFound
page. Which is a lovely feature not possible yet.Current Limitation
getRequestContext
and returning a different one is not allowed, which limits the customization potential for developers.getRequestContext
and why not give helpers to help redirecting?Conclusion
By relaxing the restrictions around locale handling and letting the middleware serve as a helper rather than an enforcer, we can make the system both more flexible and developer-friendly.
Would love to hear feedback or ideas on this!
Andréas
Beta Was this translation helpful? Give feedback.
All reactions