-
-
Notifications
You must be signed in to change notification settings - Fork 330
docs: Adapt examples for next/root-params
#1619
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
base: v4
Are you sure you want to change the base?
Changes from 3 commits
de3bf0e
b188221
224d560
028a366
a7930c7
c2af326
e4eaf31
1c6e61a
aafa8b7
7975883
e2858bd
ea7f097
d658b5c
22cb5ed
80e76be
6a6fcd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,10 @@ | ||
| import {ReactNode} from 'react'; | ||
| import './styles.css'; | ||
|
|
||
| type Props = { | ||
| children: ReactNode; | ||
| }; | ||
|
|
||
| // Since we have a `not-found.tsx` page on the root, a layout file | ||
| // is required, even if it's just passing children through. | ||
| export default function RootLayout({children}: Props) { | ||
| // No need for a layout, as this only renders a redirect | ||
| return children; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import {redirect} from 'next/navigation'; | ||
|
|
||
| // This page only renders when the app is built statically (output: 'export') | ||
| export default function RootPage() { | ||
| redirect('/en'); | ||
| export default function RootRedirect() { | ||
| return redirect('/en'); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,27 @@ | ||
| import {notFound} from 'next/navigation'; | ||
| import {Locale, hasLocale} from 'next-intl'; | ||
| import {getTranslations, setRequestLocale} from 'next-intl/server'; | ||
| import {getTranslations} from 'next-intl/server'; | ||
| import {ReactNode} from 'react'; | ||
| import BaseLayout from '@/components/BaseLayout'; | ||
| import {routing} from '@/i18n/routing'; | ||
| import './styles.css'; | ||
|
|
||
| type Props = { | ||
| children: ReactNode; | ||
| params: {locale: Locale}; | ||
| }; | ||
|
|
||
| export function generateStaticParams() { | ||
| return routing.locales.map((locale) => ({locale})); | ||
| } | ||
|
|
||
| export async function generateMetadata({ | ||
| params: {locale} | ||
| }: Omit<Props, 'children'>) { | ||
| const t = await getTranslations({locale, namespace: 'LocaleLayout'}); | ||
| export const dynamicParams = false; | ||
|
|
||
| export async function generateMetadata() { | ||
| const t = await getTranslations('LocaleLayout'); | ||
|
|
||
| return { | ||
| title: t('title') | ||
| }; | ||
| } | ||
|
|
||
| export default async function LocaleLayout({ | ||
| children, | ||
| params: {locale} | ||
| }: Props) { | ||
| if (!hasLocale(routing.locales, locale)) { | ||
| notFound(); | ||
| } | ||
|
|
||
| // Enable static rendering | ||
| setRequestLocale(locale); | ||
|
|
||
| return <BaseLayout locale={locale}>{children}</BaseLayout>; | ||
| export default async function LocaleLayout({children}: Props) { | ||
| return <BaseLayout>{children}</BaseLayout>; | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| import {clsx} from 'clsx'; | ||
| import {Inter} from 'next/font/google'; | ||
| import {NextIntlClientProvider} from 'next-intl'; | ||
| import {getMessages} from 'next-intl/server'; | ||
| import {getLocale, getMessages} from 'next-intl/server'; | ||
| import {ReactNode} from 'react'; | ||
| import Navigation from '@/components/Navigation'; | ||
|
|
||
| const inter = Inter({subsets: ['latin']}); | ||
|
|
||
| type Props = { | ||
| children: ReactNode; | ||
| locale: string; | ||
| }; | ||
|
|
||
| export default async function BaseLayout({children, locale}: Props) { | ||
| // Inline? | ||
|
||
|
|
||
| export default async function BaseLayout({children}: Props) { | ||
| const locale = await getLocale(); | ||
|
|
||
| // Providing all messages to the client | ||
| // side is the easiest way to get started | ||
| const messages = await getMessages(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,9 @@ jest.mock('next/navigation', () => ({ | |
| useSelectedLayoutSegment: () => ({locale: 'en'}) | ||
| })); | ||
|
|
||
| it('renders', () => { | ||
| // Disabled until canary version is reverted | ||
| // eslint-disable-next-line jest/no-disabled-tests | ||
| it.skip('renders', () => { | ||
|
||
| render( | ||
| <NextIntlClientProvider | ||
| locale="en" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| import {unstable_rootParams as rootParams} from 'next/server'; | ||
| import {hasLocale} from 'next-intl'; | ||
| import {getRequestConfig} from 'next-intl/server'; | ||
| import {routing} from './routing'; | ||
|
|
||
| export default getRequestConfig(async ({requestLocale}) => { | ||
| // Typically corresponds to the `[locale]` segment | ||
| const requested = await requestLocale; | ||
| const locale = hasLocale(routing.locales, requested) | ||
| ? requested | ||
| export default getRequestConfig(async () => { | ||
| const params = await rootParams(); | ||
| const locale = hasLocale(routing.locales, params.locale) | ||
| ? params.locale | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
| : routing.defaultLocale; | ||
|
|
||
| return { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ import type {Locale} from './AppConfig.tsx'; | |
| */ | ||
| export default function hasLocale<LocaleType extends Locale>( | ||
| locales: ReadonlyArray<LocaleType>, | ||
| candidate?: string | null | ||
| candidate: unknown | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A param can be |
||
| ): candidate is LocaleType { | ||
| return locales.includes(candidate as LocaleType); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Revert once in stable