@verbaly/next 0.47.0 → 0.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -1
- package/dist/server.d.ts +10 -1
- package/dist/server.js +47 -5
- package/dist/server.js.map +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -94,6 +94,33 @@ export function LocalePicker() {
|
|
|
94
94
|
}
|
|
95
95
|
```
|
|
96
96
|
|
|
97
|
+
**5. If your URLs carry the language** (`app/[locale]/…`), hand Verbaly the segment. This is what
|
|
98
|
+
keeps the route statically rendered, because the alternative is reading request headers:
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
// app/[locale]/layout.tsx
|
|
102
|
+
import { setRequestLocale, getVerbalyProps, getAlternates } from '@verbaly/next/server';
|
|
103
|
+
import { locales } from 'virtual:verbaly';
|
|
104
|
+
|
|
105
|
+
export function generateStaticParams() {
|
|
106
|
+
return locales.map((locale) => ({ locale }));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
|
|
110
|
+
const { locale } = await params;
|
|
111
|
+
return { alternates: getAlternates({ path: `/${locale}`, baseUrl: 'https://example.com' }) };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export default async function LocaleLayout({ children, params }) {
|
|
115
|
+
const { locale } = await params;
|
|
116
|
+
setRequestLocale(locale); // before any getT() in this tree
|
|
117
|
+
return <VerbalyProvider {...await getVerbalyProps()}>{children}</VerbalyProvider>;
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Without `setRequestLocale`, Verbaly negotiates from the cookie and `Accept-Language`, and that read
|
|
122
|
+
is what makes the route dynamic. With it, no header is read at all.
|
|
123
|
+
|
|
97
124
|
That's it. `next dev` extracts your messages live (catalogs + `verbaly.d.ts` stay fresh); `next build` blocks on missing translations.
|
|
98
125
|
|
|
99
126
|
## 📖 Options
|
|
@@ -106,7 +133,8 @@ Second argument of `withVerbaly`: every [`verbaly.config`](https://www.npmjs.com
|
|
|
106
133
|
| `fallback` | Locale when nothing matches (defaults to the source locale). |
|
|
107
134
|
| `failOnMissing` | `false` opts out of the build gate. |
|
|
108
135
|
|
|
109
|
-
- Negotiation reads `headers()`/`cookies()`, so
|
|
136
|
+
- **Negotiation reads `headers()`/`cookies()`, which makes a route dynamic.** Call `setRequestLocale` with your `[locale]` segment and that read never happens, so the route prerenders. For a fully static site with no server at all, [`verbaly render`](https://www.npmjs.com/package/@verbaly/compiler) mirrors the output per locale.
|
|
137
|
+
- `getAlternates({ path, baseUrl? })` returns `{ canonical, languages }` for `generateMetadata`, the same hreflang set the static mirror writes. It is empty under `no-prefix` routing, where one URL answers every language.
|
|
110
138
|
- The generated `.verbaly/` directory is build output (it ships its own `.gitignore`).
|
|
111
139
|
|
|
112
140
|
## 📚 Docs
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { TFunction, Verbaly } from "verbaly";
|
|
2
2
|
//#region src/server.d.ts
|
|
3
|
+
declare function setRequestLocale(locale: string): void;
|
|
4
|
+
interface AlternatesOptions {
|
|
5
|
+
path: string;
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
}
|
|
8
|
+
declare function getAlternates(options: AlternatesOptions): {
|
|
9
|
+
canonical?: string;
|
|
10
|
+
languages: Record<string, string>;
|
|
11
|
+
};
|
|
3
12
|
declare function getVerbaly(): Promise<Verbaly>;
|
|
4
13
|
declare function getT(): Promise<TFunction>;
|
|
5
14
|
declare function getRequestLocale(): Promise<string>;
|
|
@@ -9,5 +18,5 @@ interface VerbalyProviderProps {
|
|
|
9
18
|
}
|
|
10
19
|
declare function getVerbalyProps(): Promise<VerbalyProviderProps>;
|
|
11
20
|
//#endregion
|
|
12
|
-
export { VerbalyProviderProps, getRequestLocale, getT, getVerbaly, getVerbalyProps };
|
|
21
|
+
export { AlternatesOptions, VerbalyProviderProps, getAlternates, getRequestLocale, getT, getVerbaly, getVerbalyProps, setRequestLocale };
|
|
13
22
|
//# sourceMappingURL=server.d.ts.map
|
package/dist/server.js
CHANGED
|
@@ -1,23 +1,65 @@
|
|
|
1
1
|
import { cookies, headers } from "next/headers.js";
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
import { LOCALE_STORAGE_KEY, resolveRequestLocale } from "verbaly";
|
|
4
|
-
import { createRequestInstance, loadMessages, locales, requestOptions, sourceLocale } from "virtual:verbaly";
|
|
3
|
+
import { LOCALE_STORAGE_KEY, alternateLinks, localeFromPath, resolveRequestLocale, warnOnce } from "verbaly";
|
|
4
|
+
import { createRequestInstance, loadMessages, locales, requestOptions, routing, sourceLocale } from "virtual:verbaly";
|
|
5
5
|
//#region src/server.ts
|
|
6
|
-
const
|
|
6
|
+
const perRequest = React.cache ?? ((fn) => fn);
|
|
7
|
+
const scopedHolder = perRequest(() => ({}));
|
|
8
|
+
const moduleHolder = {};
|
|
9
|
+
function holder() {
|
|
10
|
+
const scoped = scopedHolder();
|
|
11
|
+
return scoped === scopedHolder() ? scoped : moduleHolder;
|
|
12
|
+
}
|
|
13
|
+
function setRequestLocale(locale) {
|
|
14
|
+
holder().locale = locale;
|
|
15
|
+
}
|
|
16
|
+
function declaredLocale() {
|
|
17
|
+
const raw = holder().locale;
|
|
18
|
+
if (raw === void 0) return void 0;
|
|
19
|
+
const match = localeFromPath({
|
|
20
|
+
supported: locales,
|
|
21
|
+
path: `/${raw}`
|
|
22
|
+
});
|
|
23
|
+
if (match) return match;
|
|
24
|
+
warnOnce(`setRequestLocale was given a locale the project does not have, so it was ignored`);
|
|
25
|
+
}
|
|
26
|
+
async function negotiate() {
|
|
7
27
|
const cookieName = requestOptions?.cookie ?? LOCALE_STORAGE_KEY;
|
|
8
28
|
const headerStore = await headers();
|
|
9
29
|
const cookieValue = cookieName === false ? void 0 : (await cookies()).get(cookieName)?.value;
|
|
10
|
-
|
|
30
|
+
return resolveRequestLocale({
|
|
11
31
|
supported: locales,
|
|
32
|
+
routing,
|
|
12
33
|
cookie: cookieValue,
|
|
13
34
|
header: headerStore.get("accept-language"),
|
|
14
35
|
fallback: requestOptions?.fallback ?? sourceLocale
|
|
15
36
|
});
|
|
37
|
+
}
|
|
38
|
+
const getRequestState = perRequest(async () => {
|
|
39
|
+
const locale = declaredLocale() ?? await negotiate();
|
|
16
40
|
return {
|
|
17
41
|
locale,
|
|
18
42
|
instance: await createRequestInstance(locale)
|
|
19
43
|
};
|
|
20
44
|
});
|
|
45
|
+
function getAlternates(options) {
|
|
46
|
+
const links = alternateLinks({
|
|
47
|
+
supported: locales,
|
|
48
|
+
sourceLocale,
|
|
49
|
+
routing,
|
|
50
|
+
path: options.path,
|
|
51
|
+
baseUrl: options.baseUrl
|
|
52
|
+
});
|
|
53
|
+
const languages = {};
|
|
54
|
+
for (const link of links) languages[link.hreflang] = link.href;
|
|
55
|
+
return {
|
|
56
|
+
canonical: languages[localeFromPath({
|
|
57
|
+
supported: locales,
|
|
58
|
+
path: options.path
|
|
59
|
+
}) ?? sourceLocale],
|
|
60
|
+
languages
|
|
61
|
+
};
|
|
62
|
+
}
|
|
21
63
|
async function getVerbaly() {
|
|
22
64
|
return (await getRequestState()).instance;
|
|
23
65
|
}
|
|
@@ -36,6 +78,6 @@ async function getVerbalyProps() {
|
|
|
36
78
|
};
|
|
37
79
|
}
|
|
38
80
|
//#endregion
|
|
39
|
-
export { getRequestLocale, getT, getVerbaly, getVerbalyProps };
|
|
81
|
+
export { getAlternates, getRequestLocale, getT, getVerbaly, getVerbalyProps, setRequestLocale };
|
|
40
82
|
|
|
41
83
|
//# sourceMappingURL=server.js.map
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","names":[],"sources":["../src/server.ts"],"sourcesContent":["import { cookies, headers } from 'next/headers';\nimport * as React from 'react';\nimport {
|
|
1
|
+
{"version":3,"file":"server.js","names":[],"sources":["../src/server.ts"],"sourcesContent":["import { cookies, headers } from 'next/headers';\nimport * as React from 'react';\nimport {\n alternateLinks,\n localeFromPath,\n LOCALE_STORAGE_KEY,\n resolveRequestLocale,\n warnOnce,\n type AlternateLink,\n type TFunction,\n type Verbaly,\n} from 'verbaly';\nimport {\n createRequestInstance,\n loadMessages,\n locales,\n requestOptions,\n routing,\n sourceLocale,\n} from 'virtual:verbaly';\n\ninterface RequestState {\n locale: string;\n instance: Verbaly;\n}\n\n// React.cache = per-request dedupe in RSC; identity fallback keeps plain-node tests runnable\nconst perRequest: <T extends (...args: never[]) => unknown>(fn: T) => T =\n (React as { cache?: <T>(fn: T) => T }).cache ?? ((fn) => fn);\n\ninterface LocaleHolder {\n locale?: string;\n}\n\nconst scopedHolder = perRequest((): LocaleHolder => ({}));\nconst moduleHolder: LocaleHolder = {};\n\n// React.cache only holds inside a request, and outside one there is a single request to hold for\nfunction holder(): LocaleHolder {\n const scoped = scopedHolder();\n return scoped === scopedHolder() ? scoped : moduleHolder;\n}\n\n// call it from the layout with the [locale] segment: reading headers() kills static rendering\nexport function setRequestLocale(locale: string): void {\n holder().locale = locale;\n}\n\n// a route segment is a url segment, so it narrows like one: a slug must never become a locale\nfunction declaredLocale(): string | undefined {\n const raw = holder().locale;\n if (raw === undefined) return undefined;\n const match = localeFromPath({ supported: locales, path: `/${raw}` });\n if (match) return match;\n warnOnce(`setRequestLocale was given a locale the project does not have, so it was ignored`);\n return undefined;\n}\n\n// only reached when no segment declared it: this read is what opts a route out of static\nasync function negotiate(): Promise<string> {\n const cookieName = requestOptions?.cookie ?? LOCALE_STORAGE_KEY;\n const headerStore = await headers();\n const cookieValue = cookieName === false ? undefined : (await cookies()).get(cookieName)?.value;\n return resolveRequestLocale({\n supported: locales,\n routing,\n cookie: cookieValue,\n header: headerStore.get('accept-language'),\n fallback: requestOptions?.fallback ?? sourceLocale,\n });\n}\n\nconst getRequestState = perRequest(async (): Promise<RequestState> => {\n const locale = declaredLocale() ?? (await negotiate());\n return { locale, instance: await createRequestInstance(locale) };\n});\n\nexport interface AlternatesOptions {\n path: string;\n baseUrl?: string;\n}\n\n// the hreflang set for generateMetadata, in the shape Next's alternates field takes\nexport function getAlternates(options: AlternatesOptions): {\n canonical?: string;\n languages: Record<string, string>;\n} {\n const links: AlternateLink[] = alternateLinks({\n supported: locales,\n sourceLocale,\n routing,\n path: options.path,\n baseUrl: options.baseUrl,\n });\n const languages: Record<string, string> = {};\n for (const link of links) languages[link.hreflang] = link.href;\n const self = localeFromPath({ supported: locales, path: options.path }) ?? sourceLocale;\n return { canonical: languages[self], languages };\n}\n\n// request-scoped instance: never the virtual:verbaly singleton (locale would leak between requests)\nexport async function getVerbaly(): Promise<Verbaly> {\n return (await getRequestState()).instance;\n}\n\nexport async function getT(): Promise<TFunction> {\n return (await getRequestState()).instance.t;\n}\n\nexport async function getRequestLocale(): Promise<string> {\n return (await getRequestState()).locale;\n}\n\nexport interface VerbalyProviderProps {\n locale: string;\n messages?: Record<string, string>;\n}\n\n// messages omitted for the source locale: it already ships inline in the client bundle\nexport async function getVerbalyProps(): Promise<VerbalyProviderProps> {\n const { locale } = await getRequestState();\n if (locale === sourceLocale) return { locale };\n return { locale, messages: await loadMessages(locale) };\n}\n"],"mappings":";;;;;AA2BA,MAAM,aACH,MAAsC,WAAW,OAAO;AAM3D,MAAM,eAAe,kBAAgC,CAAC,EAAE;AACxD,MAAM,eAA6B,CAAC;AAGpC,SAAS,SAAuB;CAC9B,MAAM,SAAS,aAAa;CAC5B,OAAO,WAAW,aAAa,IAAI,SAAS;AAC9C;AAGA,SAAgB,iBAAiB,QAAsB;CACrD,OAAO,CAAC,CAAC,SAAS;AACpB;AAGA,SAAS,iBAAqC;CAC5C,MAAM,MAAM,OAAO,CAAC,CAAC;CACrB,IAAI,QAAQ,KAAA,GAAW,OAAO,KAAA;CAC9B,MAAM,QAAQ,eAAe;EAAE,WAAW;EAAS,MAAM,IAAI;CAAM,CAAC;CACpE,IAAI,OAAO,OAAO;CAClB,SAAS,kFAAkF;AAE7F;AAGA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
|
3
|
+
"version": "0.49.0",
|
|
4
4
|
"description": "Next.js integration for Verbaly: App Router/RSC with per-request locale negotiation, Turbopack and webpack support, flash-free hydration.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -64,23 +64,23 @@
|
|
|
64
64
|
"access": "public"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@verbaly/compiler": "^0.
|
|
67
|
+
"@verbaly/compiler": "^0.49.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"next": "^15.0.0 || ^16.0.0",
|
|
71
71
|
"react": "^18.0.0 || ^19.0.0",
|
|
72
|
-
"@verbaly/react": "^0.
|
|
73
|
-
"verbaly": "^0.
|
|
72
|
+
"@verbaly/react": "^0.49.0",
|
|
73
|
+
"verbaly": "^0.49.0"
|
|
74
74
|
},
|
|
75
75
|
"devDependencies": {
|
|
76
76
|
"@types/react": "^19.2.18",
|
|
77
77
|
"@types/react-dom": "^19.2.5",
|
|
78
|
-
"happy-dom": "^20.12.
|
|
79
|
-
"next": "^16.3.
|
|
78
|
+
"happy-dom": "^20.12.1",
|
|
79
|
+
"next": "^16.3.4",
|
|
80
80
|
"react": "^19.2.8",
|
|
81
81
|
"react-dom": "^19.2.8",
|
|
82
|
-
"@verbaly/react": "^0.
|
|
83
|
-
"verbaly": "^0.
|
|
82
|
+
"@verbaly/react": "^0.49.0",
|
|
83
|
+
"verbaly": "^0.49.0"
|
|
84
84
|
},
|
|
85
85
|
"scripts": {
|
|
86
86
|
"build": "tsdown",
|