Skip to content

Commit

Permalink
split out navigation apis in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn committed Jan 23, 2025
1 parent 4d4fe97 commit b7f456a
Show file tree
Hide file tree
Showing 29 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useLocale, useTranslations} from 'next-intl';
import LocaleSwitcher from '@/components/LocaleSwitcher';
import PageLayout from '@/components/PageLayout';
import {Link} from '@/i18n/routing';
import {Link} from '@/i18n/navigation';

export default function Index() {
const t = useTranslations('Index');
Expand Down
5 changes: 5 additions & 0 deletions examples/example-app-router-migration/src/i18n/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createNavigation} from 'next-intl/navigation';
import {routing} from './routing';

export const {Link, redirect, usePathname, useRouter} =
createNavigation(routing);
4 changes: 0 additions & 4 deletions examples/example-app-router-migration/src/i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {createNavigation} from 'next-intl/navigation';
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
locales: ['en', 'de'],
defaultLocale: 'en'
});

export const {Link, redirect, usePathname, useRouter} =
createNavigation(routing);
2 changes: 1 addition & 1 deletion examples/example-app-router-mixed-routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ An example of how to achieve locale prefixes on public routes while reading the

1. [`src/middleware.ts`](./src/middleware.ts): Only run middleware on public pages that need to be localized.
2. [`src/i18n/request.ts`](./src/i18n/request.ts): Use the locale from the pathname segment for public routes or return a locale from the user profile for internal app routes.
3. [`src/i18n/routing.public.ts`](./src/i18n/routing.public.ts): These are the navigation APIs that automatically consider the `[locale]` segment for public routes. For internal app routes, the navigation APIs from Next.js should be used directly (see `PublicNavigation.tsx` vs `AppNavigation.tsx`).
3. [`src/i18n/navigation.public.ts`](./src/i18n/navigation.public.ts): These are the navigation APIs that automatically consider the `[locale]` segment for public routes. For internal app routes, the navigation APIs from Next.js should be used directly (see `PublicNavigation.tsx` vs `AppNavigation.tsx`).

Note that while this approach works fine, you can alternatively also consider a monorepo setup and build the public and internal app separately if you'd like to separate the code for the two apps.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {useSelectedLayoutSegment} from 'next/navigation';
import {ComponentProps} from 'react';
import {Link} from '@/i18n/routing.public';
import {Link} from '@/i18n/navigation.public';

export default function NavLink({href, ...rest}: ComponentProps<typeof Link>) {
const selectedLayoutSegment = useSelectedLayoutSegment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {useLocale} from 'next-intl';
import {Locale} from '@/config';
import {Link, usePathname} from '@/i18n/routing.public';
import {Link, usePathname} from '@/i18n/navigation.public';

export default function PublicNavigationLocaleSwitcher() {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createNavigation} from 'next-intl/navigation';
import {routing} from './routing.public';

// Should only be used on public routes in the `[locale]` segment
export const {Link, usePathname} = createNavigation(routing);
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {createNavigation} from 'next-intl/navigation';
import {defineRouting} from 'next-intl/routing';
import {defaultLocale, locales} from '../config';

export const routing = defineRouting({
locales,
defaultLocale
});

// Should only be used on public routes in the `[locale]` segment
export const {Link, usePathname} = createNavigation(routing);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useLocale, useTranslations} from 'next-intl';
import {Link, usePathname} from '@/i18n/routing';
import {Link, usePathname} from '@/i18n/navigation';

export default function LocaleSwitcher() {
const t = useTranslations('LocaleSwitcher');
Expand Down
5 changes: 5 additions & 0 deletions examples/example-app-router-next-auth/src/i18n/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createNavigation} from 'next-intl/navigation';
import {routing} from './routing';

export const {Link, redirect, usePathname, useRouter} =
createNavigation(routing);
4 changes: 0 additions & 4 deletions examples/example-app-router-next-auth/src/i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {createNavigation} from 'next-intl/navigation';
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
locales: ['en', 'de'],
defaultLocale: 'en',
localePrefix: 'as-needed'
});

export const {Link, redirect, usePathname, useRouter} =
createNavigation(routing);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import {useFormatter, useLocale, useNow, useTimeZone} from 'next-intl';
import {Link, usePathname} from '@/i18n/routing';
import {Link, usePathname} from '@/i18n/navigation';

export default function ClientContent() {
const now = useNow();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import {useLocale} from 'next-intl';
import {redirect} from '@/i18n/routing';
import {redirect} from '@/i18n/navigation';

export default function ClientRedirectPage() {
const locale = useLocale();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import {usePathname} from '@/i18n/routing';
import {usePathname} from '@/i18n/navigation';

export default function UnlocalizedPathname() {
return <p data-testid="UnlocalizedPathname">{usePathname()}</p>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Metadata} from 'next';
import {useTranslations} from 'next-intl';
import {use} from 'react';
import {Locale, getPathname} from '@/i18n/routing';
import {Locale} from '@/i18n/routing';
import {getPathname} from '@/i18n/navigation';

type Props = {
params: Promise<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {useFormatter, useNow, useTimeZone, useTranslations} from 'next-intl';
import {use} from 'react';
import DropdownMenu from '@/components/DropdownMenu';
import RichText from '@/components/RichText';
import {Link} from '@/i18n/routing';
import {Link} from '@/i18n/navigation';
import AsyncComponent from '../../components/AsyncComponent';
import AsyncComponentWithNamespaceAndLocale from '../../components/AsyncComponentWithNamespaceAndLocale';
import AsyncComponentWithoutNamespace from '../../components/AsyncComponentWithoutNamespace';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useLocale} from 'next-intl';
import {redirect} from '@/i18n/routing';
import {redirect} from '@/i18n/navigation';

export default function Redirect() {
const locale = useLocale();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import {ComponentProps} from 'react';
import {Link} from '@/i18n/routing';
import {Link} from '@/i18n/navigation';

export default function NavigationLink(props: ComponentProps<typeof Link>) {
return <Link {...props} />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import {useRouter} from '@/i18n/routing';
import {useRouter} from '@/i18n/navigation';

export default function ClientRouterWithoutProvider() {
const router = useRouter();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Dropdown from '@radix-ui/react-dropdown-menu';
import {Link as NextIntlLink} from '@/i18n/routing';
import {Link as NextIntlLink} from '@/i18n/navigation';

export default function DropdownMenu() {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useLocale, useTranslations} from 'next-intl';
import {Link} from '@/i18n/routing';
import {Link} from '@/i18n/navigation';

export default function LocaleSwitcher() {
const t = useTranslations('LocaleSwitcher');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {useSelectedLayoutSegment} from 'next/navigation';
import {ComponentProps} from 'react';
import {Link} from '@/i18n/routing';
import {Link} from '@/i18n/navigation';

export default function NavigationLink({
href,
Expand Down
5 changes: 5 additions & 0 deletions examples/example-app-router-playground/src/i18n/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createNavigation} from 'next-intl/navigation';
import {routing} from './routing';

export const {Link, getPathname, redirect, usePathname, useRouter} =
createNavigation(routing);
4 changes: 0 additions & 4 deletions examples/example-app-router-playground/src/i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {createNavigation} from 'next-intl/navigation';
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
Expand Down Expand Up @@ -62,6 +61,3 @@ export const routing = defineRouting({

export type Pathnames = keyof typeof routing.pathnames;
export type Locale = (typeof routing.locales)[number];

export const {Link, getPathname, redirect, usePathname, useRouter} =
createNavigation(routing);
3 changes: 2 additions & 1 deletion examples/example-app-router/src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {MetadataRoute} from 'next';
import {host} from '@/config';
import {Locale, getPathname, routing} from '@/i18n/routing';
import {routing, Locale} from '@/i18n/routing';
import {getPathname} from '@/i18n/navigation';

export default function sitemap(): MetadataRoute.Sitemap {
return [getEntry('/'), getEntry('/pathnames')];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import clsx from 'clsx';
import {useParams} from 'next/navigation';
import {ChangeEvent, ReactNode, useTransition} from 'react';
import {Locale, usePathname, useRouter} from '@/i18n/routing';
import {Locale} from '@/i18n/routing';
import {usePathname, useRouter} from '@/i18n/navigation';

type Props = {
children: ReactNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import clsx from 'clsx';
import {useSelectedLayoutSegment} from 'next/navigation';
import {ComponentProps} from 'react';
import {Link} from '@/i18n/routing';
import {Link} from '@/i18n/navigation';

export default function NavigationLink({
href,
Expand Down
5 changes: 5 additions & 0 deletions examples/example-app-router/src/i18n/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createNavigation} from 'next-intl/navigation';
import {routing} from './routing';

export const {Link, getPathname, redirect, usePathname, useRouter} =
createNavigation(routing);
4 changes: 0 additions & 4 deletions examples/example-app-router/src/i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {createNavigation} from 'next-intl/navigation';
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
Expand All @@ -15,6 +14,3 @@ export const routing = defineRouting({

export type Pathnames = keyof typeof routing.pathnames;
export type Locale = (typeof routing.locales)[number];

export const {Link, getPathname, redirect, usePathname, useRouter} =
createNavigation(routing);

0 comments on commit b7f456a

Please sign in to comment.