Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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]: Change locale in App Router without-i18n-routing #1334

Closed
dellorogiulio opened this issue Sep 11, 2024 · 1 comment
Closed

[Docs]: Change locale in App Router without-i18n-routing #1334

dellorogiulio opened this issue Sep 11, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.

Comments

@dellorogiulio
Copy link

dellorogiulio commented Sep 11, 2024

Link to page

https://next-intl-docs.vercel.app/docs/getting-started/app-router/without-i18n-routing

Describe the problem

I'm using next-intl in the App Router without-i18n-routing configuration.
Point 3 of the linked documentation reports:

export default getRequestConfig(async () => {
  // Provide a static locale, fetch a user setting, <------
  // read from `cookies()`, `headers()`, etc.
  const locale = 'en';
 
  return {
    locale,
    messages: (await import(`../../messages/${locale}.json`)).default
  };
});

But say nothing about how to handle changes in that user setting.

As example, consider the case in which the language is stored in a database (language is a general app setting). When someone changes the database, we expect the language to change too.
So we need a way to refresh the getLocale() calling.

Currently, the only way I found to achieve this is by using both revalidatePath("/", "layout") (on the server) and router.refresh() (on the client)

I have a subscription on my database key changes, once I see a change, i trigger a rootLayout revalidation by using revalidatePath("/", "layout") and then notify the client to refresh the router via router.refresh()

But this solution does not seem stable (see below).

My question (and suggestion for a doc updates) is: is this the correct way to handle locale changes without-i18n-routing?

NOTE:

I said that 'seems not stable' because I saw that next js hits cache while revalidating path. In fact, I see the cookie x-nextjs-cache: HIT in the header response of the revalidatePath("/", "layout")

Looking at your 790, it seems to me that all the magic to avoid cache hitting is done in the middleware, which is not required in the without-i18n-routing

@dellorogiulio dellorogiulio added documentation Improvements or additions to documentation unconfirmed Needs triage. labels Sep 11, 2024
@amannn
Copy link
Owner

amannn commented Sep 17, 2024

But say nothing about how to handle changes in that user setting.

The reason is because this really depends on the approach that you've used in your app.

If you're using a cookie-based approach, you might want to refer to the example App Router without i18n routing that is linked at the bottom of the getting started guide you're referring to.

While creating the example I've found that it's sufficient to call cookies.set(…) in a Server Action for the app to update when using cookies. Next.js is updating its cache heuristics with the upcoming version 15, therefore it might not be a good time to add docs yet, because they might get outdated soon.

Depending on your app, esp. if you don't use cookies, you might need to call router.refresh() or revalidatePath as you've mentioned. Maybe I'll add further docs about this in the future, but for now the example or the Next.js docs are the best source for finding an approach that works for you.

I'm going to move this to a discussion for now.

Repository owner locked and limited conversation to collaborators Sep 17, 2024
@amannn amannn converted this issue into discussion #1342 Sep 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

2 participants