Skip to content

Commit

Permalink
docs: Static export and update Next.js in examples (#333) [skip ci]
Browse files Browse the repository at this point in the history
Closes #334
Closes #313
Closes #330
  • Loading branch information
amannn committed Jun 19, 2023
1 parent cae135d commit c9c2070
Show file tree
Hide file tree
Showing 13 changed files with 3,751 additions and 2,154 deletions.
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@tailwindcss/typography": "^0.5.9",
"clsx": "^1.2.1",
"http-status-codes": "^2.2.0",
"next": "^13.4.0",
"next": "^13.4.6",
"nextra": "^2.4.2",
"nextra-theme-docs": "^2.4.2",
"react": "^18.2.0",
Expand Down
39 changes: 36 additions & 3 deletions docs/pages/docs/next-13/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export const config = {

There are two strategies for detecting the locale:

1. [Prefix-based routing (default)](#prefix-based-routing-default)
1. [Prefix-based routing (default)](#prefix-based-routing)
2. [Domain-based routing](#domain-based-routing)

Once a locale is detected, it will be saved in a cookie.

### Prefix-based routing (default)
### Prefix-based routing (default) [#prefix-based-routing]

Since your pages are nested within a `[locale]` folder, all routes are prefixed with one of your supported locales (e.g. `/de/about`). To keep the URL short, requests for the default locale are rewritten internally to work without a locale prefix.

Expand Down Expand Up @@ -112,7 +112,7 @@ The locale is detected based on these priorities:

<Callout>

Since unknown domains will be handled with [prefix-based routing](#prefix-based-routing-default), this strategy can be used for local development where the host is `localhost`.
Since unknown domains will be handled with [prefix-based routing](#prefix-based-routing), this strategy can be used for local development where the host is `localhost`.

</Callout>

Expand Down Expand Up @@ -302,3 +302,36 @@ There's a working [example that combines `next-intl` with Auth.js](https://githu
</Callout>

Many thanks to [narakhan](https://github.com/narakhan) for [sharing his middleware implementation](https://github.com/amannn/next-intl/pull/149#issuecomment-1509990635)!

## Usage without middleware (static export)

If you're using the [static export feature from Next.js](https://nextjs.org/docs/app/building-your-application/deploying/static-exports) (`output: 'export'`), the middleware will not run. You can use [prefix-based routing](#prefix-based-routing) nontheless to internationalize your app, but a few tradeoffs apply.

**Static export limitations:**

1. There's no default locale that can be used without a prefix (same as [`localePrefix: 'always'`](#always-use-a-locale-prefix))
2. The locale can't be negotiated at runtime (same as [`localeDetection: false`](#disable-automatic-locale-detection))
3. You need to add a redirect for the root of the app

```tsx filename="app/page.tsx"
import {redirect} from 'next/navigation';

// Redirect the user to the default locale when the app root is requested
export default function RootPage() {
redirect('/en');
}
```

<Callout type="warning">
Note that this is currently limited to the usage of `next-intl` in [Client
Components](/docs/next-13/client-components) (not [Server
Components](/docs/next-13/server-components)).
</Callout>

You can explore a working demo by building [the Next.js 13 example](/examples/next-13) after enabling the static export:

```tsx filename="next.config.js"
module.exports = {
output: 'export'
};
```
3 changes: 1 addition & 2 deletions docs/theme.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ export default {
project: {
link: 'https://github.com/amannn/next-intl'
},
docsRepositoryBase:
'https://github.com/amannn/next-intl/blob/main/packages/website',
docsRepositoryBase: 'https://github.com/amannn/next-intl/blob/main/docs',
useNextSeoProps() {
return {
titleTemplate: '%s – Internationalization (i18n) for Next.js'
Expand Down
2 changes: 1 addition & 1 deletion examples/example-advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"accept-language-parser": "^1.5.0",
"lodash": "^4.17.21",
"next": "^13.4.0",
"next": "^13.4.6",
"next-intl": "^2.14.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/example-next-13-next-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.0",
"next": "^13.4.6",
"next-auth": "^4.22.1",
"next-intl": "^2.14.3",
"react": "^18.2.0",
Expand Down
1 change: 1 addition & 0 deletions examples/example-next-13/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tsconfig.tsbuildinfo
/test-results/
/playwright-report/
/playwright/.cache/
out
3 changes: 0 additions & 3 deletions examples/example-next-13/next.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion examples/example-next-13/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@next/font": "^13.1.6",
"clsx": "^1.2.1",
"next": "^13.4.0",
"next": "^13.4.6",
"next-intl": "^2.14.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
4 changes: 4 additions & 0 deletions examples/example-next-13/src/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async function getMessages(locale: string) {
}
}

export async function generateStaticParams() {
return ['en', 'de'].map((locale) => ({locale}));
}

export async function generateMetadata({params: {locale}}: Props) {
const messages = await getMessages(locale);

Expand Down
6 changes: 6 additions & 0 deletions examples/example-next-13/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +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');
}
2 changes: 1 addition & 1 deletion examples/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"date-fns": "^2.16.1",
"next": "^13.4.0",
"next": "^13.4.6",
"next-intl": "^2.14.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"eslint": "^8.39.0",
"eslint-config-molindo": "^6.0.0",
"eslint-plugin-deprecation": "^1.4.1",
"next": "^13.4.0",
"next": "^13.4.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ts-jest": "^29.1.0",
Expand Down
Loading

3 comments on commit c9c2070

@vercel
Copy link

@vercel vercel bot commented on c9c2070 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-intl-docs – ./docs

next-intl-docs.vercel.app
next-intl-docs-next-intl.vercel.app
next-intl-docs-git-main-next-intl.vercel.app

@vercel
Copy link

@vercel vercel bot commented on c9c2070 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-intl-example-next-13 – ./examples/example-next-13

next-intl-example-next-13-next-intl.vercel.app
next-intl-example-next-13.vercel.app
next-intl-example-next-13-git-main-next-intl.vercel.app

@vercel
Copy link

@vercel vercel bot commented on c9c2070 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

example-next-13-next-auth – ./examples/example-next-13-next-auth

example-next-13-next-auth-git-main-next-intl.vercel.app
example-next-13-next-auth-next-intl.vercel.app
example-next-13-next-auth.vercel.app

Please sign in to comment.