Skip to content
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]: Update example in LocaleLayout for Next.js 15 #1626

Closed
valentin-harrang opened this issue Dec 19, 2024 · 2 comments
Closed

[Docs]: Update example in LocaleLayout for Next.js 15 #1626

valentin-harrang opened this issue Dec 19, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.

Comments

@valentin-harrang
Copy link

Link to page

https://next-intl.dev/docs/getting-started/app-router/with-i18n-routing#layout

Describe the problem

The LocaleLayout example provided in the documentation is outdated for the latest versions of Next.js. Since Next.js 15, the params in app directory layouts are now a promise. This requires using await when destructuring params in the LocaleLayout.

Current example from the docs:

export default async function LocaleLayout({
  children,
  params: {locale}
}: {
  children: React.ReactNode;
  params: {locale: string};
}) {
  // Ensure that the incoming `locale` is valid
  if (!routing.locales.includes(locale as any)) {
    notFound();
  }

  // Providing all messages to the client
  // side is the easiest way to get started
  const messages = await getMessages();

  return (
    <html lang={locale}>
      <body>
        <NextIntlClientProvider messages={messages}>
          {children}
        </NextIntlClientProvider>
      </body>
    </html>
  );
}

Proposed update:

export default async function LocaleLayout({
  children,
  params
}: {
  children: React.ReactNode;
  params: Promise<{locale: string}>;
}) {
  const { locale } = await params;

  // Ensure that the incoming `locale` is valid
  if (!routing.locales.includes(locale as any)) {
    notFound();
  }

  // Providing all messages to the client
  // side is the easiest way to get started
  const messages = await getMessages();

  return (
    <html lang={locale}>
      <body>
        <NextIntlClientProvider messages={messages}>
          {children}
        </NextIntlClientProvider>
      </body>
    </html>
  );
}

Why this change is necessary:

The current example leads to a runtime error in Next.js 15+ because params is not destructurable until resolved as a promise. This adjustment will align the documentation with the latest Next.js API changes.

@valentin-harrang valentin-harrang added documentation Improvements or additions to documentation unconfirmed Needs triage. labels Dec 19, 2024
@C-Jeril
Copy link

C-Jeril commented Dec 20, 2024

I may have encountered the same problem, but I finally modified it like this to temporarily solve it

export async function generateMetadata(props: { params: any }): Promise<Metadata> { const { locale } = await props.params as { locale: SupportedLocale };

export default async function NPage({ params }: { params: Promise<{ locale: SupportedLocale }> }) { const { locale } = await params;

@amannn
Copy link
Owner

amannn commented Dec 20, 2024

Thanks for the report! There's however already an existing one at #1587 and a PR is open to address this.

@amannn amannn closed this as completed Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

3 participants