You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -110,11 +108,24 @@ const t = Locale.use(locale)
110
108
</html>
111
109
```
112
110
113
-
> **Note:** In `server` mode, omit `getStaticPaths` — pages are rendered on demand.
111
+
In `server` mode, omit `getStaticPaths` and opt out of prerendering explicitly:
112
+
113
+
```astro
114
+
---
115
+
export const prerender = false
116
+
117
+
import { Locale } from "@mannisto/astro-i18n/runtime"
118
+
119
+
const locale = Locale.from(Astro.url)
120
+
const t = Locale.use(locale)
121
+
---
122
+
```
123
+
124
+
Without `prerender = false`, Astro will treat dynamic routes as static and throw a `GetStaticPathsRequired` error even in server mode.
114
125
115
126
### Layout
116
127
117
-
Your layout should derive the locale from the URL and sync it to a cookie on every page load. This ensures the correct locale is remembered across visits.
128
+
Your layout should derive the locale from the URL and sync it to a cookie on every page load. This ensures the correct locale is remembered across visits and that 404 pages detect the locale correctly.
Create a `src/pages/404.astro` at the root. In `hybrid` and `server` mode, unknown paths are redirected to their locale-prefixed equivalent before the 404 renders (e.g. `/banana` → `/en/banana`), so `Locale.from(Astro.url)` always returns the correct locale. In `static` mode, `Locale.from` falls back to `defaultLocale` for bare unprefixed paths, but locale-prefixed paths like `/fi/banana` still resolve correctly.
154
+
155
+
```astro
156
+
---
157
+
import { Locale } from "@mannisto/astro-i18n/runtime"
158
+
import Layout from "@layouts/Layout.astro"
159
+
160
+
const locale = Locale.from(Astro.url)
161
+
const t = Locale.use(locale)
162
+
---
163
+
164
+
<Layout title={t("error.title")}>
165
+
<h1>{t("error.title")}</h1>
166
+
<p>{t("error.description")}</p>
167
+
</Layout>
168
+
```
169
+
140
170
### Language switcher
141
171
142
172
```astro
@@ -164,9 +194,9 @@ const locales = Locale.get()
164
194
</script>
165
195
```
166
196
167
-
### Middleware (server mode)
197
+
### Middleware
168
198
169
-
The middleware is auto-registered in `server` mode. It redirects unprefixed URLs (e.g. `/about` → `/en/about`) and keeps the locale cookie in sync.
199
+
The middleware is auto-registered in `server`and `hybrid`mode. It redirects unprefixed URLs (e.g. `/about` → `/en/about`), keeps the locale cookie in sync, and automatically skips prerendered pages to avoid warnings about unavailable request headers.
170
200
171
201
You can also compose it manually with other middleware:
172
202
@@ -179,39 +209,37 @@ import { onRequest as myMiddleware } from "./my-middleware"
0 commit comments