-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
Geting rid of setRequestLocale
#663
Comments
You don’t need to call the function in client-only pages. |
…and update docs to suggest validating the locale in `i18n.ts` (#742) Users are currently struggling with errors that are caused by these two scenarios: 1. An invalid locale was passed to `next-intl` APIs (e.g. [#736](#736)) 2. No locale is available during rendering (e.g. [#716](#716)) **tldr:** 1. We now suggest to validate the incoming `locale` in [`i18n.ts`](https://next-intl-docs.vercel.app/docs/usage/configuration#i18nts). This change is recommended to all users. 2. `next-intl` will call the `notFound()` function when the middleware didn't run on a localized request and `next-intl` APIs are used during rendering. Previously this would throw an error, therefore this is only relevant for you in case you've encountered [the corresponding error](https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale). Make sure you provide a relevant [`not-found` page](https://next-intl-docs.vercel.app/docs/environments/error-files#not-foundjs) that can be rendered in this case. --- **Handling invalid locales** Users run into this error because the locale wasn't sufficiently validated. This is in practice quite hard because we currently educate in the docs that this should happen in [the root layout](https://next-intl-docs.vercel.app/docs/getting-started/app-router#applocalelayouttsx), but due to the parallel rendering capabilities of Next.js, potentially a page or `generateMetadata` runs first. Therefore moving this validation to a more central place seems necessary. Due to this, the docs will now suggest validating the locale in `i18n.ts`. By doing this, we can catch erroneous locale arguments in a single place before e.g. importing JSON files. The only edge case is if an app uses no APIs of `next-intl` in Server Components at all and therefore `i18n.ts` doesn't run. This should be a very rare case though as even `NextIntlClientProvider` will call `i18n.ts`. The only case to run into this is if you're using `NextIntlClientProvider` in a Client Component and delegate all i18n handling to Client Components too. If you have such an app, `i18n.ts` will not be invoked and you should validate the `locale` before passing it to `NextIntlClientProvider`. **Handling missing locales** This warning is probably one of the most annoying errors that users currently run into: ``` Unable to find next-intl locale because the middleware didn't run on this request. ``` The various causes of this error are outlined in [the docs](https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale). Some of these cases should simply be 404s (e.g. when your middleware matcher doesn't match `/unknown.txt`), while others require a fix in the matcher (e.g. considering `/users/jane.doe` when using `localePrefix: 'as-necessary'`). My assumption is that many of these errors are false positives that are caused by the `[locale]` segment acting as a catch-all. As a result, a 500 error is encountered instead of 404s. Due to this, this PR will downgrade the previous error to a dev-only warning and will invoke the `notFound()` function. This should help in the majority of cases. Note that you should define [a `not-found` file](https://next-intl-docs.vercel.app/docs/environments/error-files#not-foundjs) to handle this case. I think this change is a good idea because if you're using `unstable_setRequestLocale` and you have a misconfigured middleware matcher, you can provide any kind of string to `next-intl` (also `unknown.txt`) and not run into this error. Therefore it only affects users with dynamic rendering. Validating the locale in `i18n.ts` is the solution to either case (see above). Also in case something like [`routeParams`](#663) gets added to Next.js, the current warning will be gone entirely—therefore tackling it from a different direction is likely a good idea. The false negatives of this should hopefully be rather small as we consistently point out that you need to adapt your middleware matcher when switching the `localePrefix` to anything other than `always`. Dev-only warnings should help to get further information for these requests. --- Closes #736 Closes #716 Closes #446
I'm curious about whether calling When exporting |
Unfortunately not, please see the corresponding docs: Add |
We'll probably introduce support for providing a locale to |
A realization while working on #1017: We currently support a Developers have previously asked why the In case the developer could manage the reading of the locale from params manually in Ideally, something like this would be possible: import {notFound} from 'next/navigation';
import {getRequestConfig} from 'next-intl/server';
import {getParams} from '???';
// Can be imported from a shared config
const locales = ['en', 'de'];
export default getRequestConfig(async () => {
let {locale} = getParams();
if (!locale) locale = 'en';
// Validate that the incoming `locale` parameter is valid
if (!locales.includes(locale as any)) notFound();
return {
locale,
messages: (await import(`../messages/${locale}.json`)).default
};
}); |
Hello. I recently used next-intl in a job project and now I've rolled it back. As I've worked on such a thing I know the importance of feedback) A bit late comment, just to add to the value of this issue)
I have a feeling that it breaks when it render some clients components (but I can't confirm this in any way). I saw somewhere recommendation to use my context - it has the same problem. Apparently, there are something of component rendering in React. Although in the case of my context, you can set the build to one process and everything will be correct, and the context in case of something is lost immediately on the entire page. So the reasons are different. However, I see an update came out these days, that locale can be set the way you want to (where would I get the getParams method 🤔😁) - thank you, will try it. Overall, thanks for the package, cool API! |
Hey @vordgi! Nice to hear from you here, been following your work with nimpl-getters! :) Can you by chance provide a reproduction where you observe that the context is lost? The important part for Are you using any more exotic features from Next.js? E.g. I could imagine that PPR could potentially conflict with this. Btw. in case you're interested, I had a look at what it'd take for native support from Next.js for providing a "server context" yesterday: vercel/next.js#58862 (comment). Leave a comment there if you have additional context! |
The parameters have been added everywhere. I will try to create an repro, but basically, it looks like this: const Page: React.FC<{ params: { lang: string } }> = async ({ params }) => {
if (!validLocales.includes(params.lang)) return notFound();
unstable_setRequestLocale(params.lang);
const messages = await getMessages();
return (
<NextIntlClientProvider messages={messages}>
{/* Client component with context.provider inside */}
<ContextProvider value={{key1: [],key2: ''}}>
<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify('...')}} />
{/* Asynchronous server component, translations are only at the top level of this component, below they are lost */}
<Layout>
{/* server component whithout translates */}
<Block1 />
</Layout>
</ContextProvider>
</NextIntlClientProvider>
);
}; Yes, there are indeed risks with PPR. But I think that if functionality is unavailable in part of next.js - just block the functionality in that part. That is, I will add the error "Do not use this in PPR". I hope such an opportunity will be. But in fact, with the current structure of next.js, it is very difficult to find out what the component is inside and it is difficult to rewrite this logic (I tried). I am now looking at the possibility of adding the ability to create a page context. But still only at the beginning of the research |
Wish me luck) |
Regarding PPR: On a second thought, maybe this is not an issue at all, since PPR is about pre-rendering as much as possible statically with inner parts remaining dynamic. Dynamic rendering was always the easy part since we can just read a header from the middleware there. Let's see what Next.js 14 brings next week, maybe there are news in regard to PPR and we could do a few first tests.
Oh interesting, thanks for looking into this! As far as I understand the proposal, this would still require providing the locale for every page where you intend to read it, right? In that regard the ergonomics would be similar to I think ideally Next.js should really have a built-in capability to read params deeply in RSC. |
In Next.js, you can add parameters to their internal context and then read it the same way I read in Yes, it will need to be added for each page, it's something comparable to |
Yep, definitely a good idea to discuss which options we have! I think a tricky part about the story with params is that they can be different per layout/page. A single render might consist of multiple layouts that render and a single page. Theoretically there could be different params at every level (e.g. |
Since they want to render the layout separately (this needs to be technically done at first because it works differently now) - I think in the layout the get-params functionality should be blocked or fixed with params higher in the tree. This is logical in such a situation - it was assembled as a segment once - where it lies. |
Currently, you can already read params in the layout via the |
Decided to make a PR with the implementation of get-params (what I talked about above, in the basic version - a very simple change). |
Yes, but what about components 10 levels deeper. They now have no idea - they are inside layout or page. And with the current next.js architecture, it's unreal to find out. So I think next.js need to render layout separately and then we can understand what is being rendered and depending on it, return the necessary params or block the API (although it looks like there shouldnt be any problems with issuance). But it's still not clear how it would be with PPR, yes... |
Hello @amannn. I have to say sorry for incorrect conclusions. The cache works well. The problem was that I used The most annoying thing is that I knew that I rewrote it in So, in general, it is always necessary to consider that the |
Any update on getting rid of this? |
How about writing a SWC plugin to automatically inject Then people would just have to add it to |
Certainly an interesting idea! I don't have experience with writing SWC plugins, but if there's a community effort to build this as a separate package I'd be happy to follow along and potentially explore adding it to the docs or if the approach proves to work well, we could consider adding it to the core. I'm unsure currently if there are edge cases, so this might need so experimentation. The ideal solution would still require support from the Next.js side I believe: vercel/next.js#58862. But if we can improve the current workaround for the time being, I'd be all for it. |
Here's an SWC plugin that could be used for inspiration: |
This is only tangentially related, but I also feel it does not deserve a separate issue: if you have a root layout outside |
I am having the same issues with you. Once I remove the layout.tsx and not-found.tsx that outside |
unstable_setRequestLocale
setRequestLocale
I'm curious about this issue, will it be addressed by Next.js 15 or Next-intl 4? |
@ixartz Please see the corresponding section in the next-intl 3.22 blog post on |
@amannn Thank you so much for sharing it, I'll read carefully https://next-intl-docs.vercel.app/blog/next-intl-3-22, it seems I totally miss this blog post where you share a lot of information. |
A note from the discussion at #1474: Even if we get rid of Another thought: For easier getting started, if we could read params deeply, could we get started with a middleware matcher of |
Finally some news: https://x.com/sebmarkbage/status/1851728589240115459. Not much for now, but very happy to hear that something is coming. |
@amannn can you share what that Tweet said? Looks like Seb recently nuked his account. |
@reiv The tweet just mentioned that the Next.js team is working on something that will address accessing "global" params like Here's another mention from Jimmy Lai: |
It's happening! vercel/next.js#72837 👀 |
I'm confused. I didn't expect rootParams to be async. How does this solve vercel/next.js#71927 ? Unless... Sebastian mentioned something about being able to suspend above |
Have you seen the comment below from Sebastian? |
As mentioned in the static rendering docs,
setRequestLocale
is intended as a stopgap solution to be removed at some point. I've started a discussion for the relevant feature on the Next.js side here: vercel/next.js#58862If you'd like to help simplify the usage of
next-intl
, please join the discussion there and upvote the comment!The text was updated successfully, but these errors were encountered: