Skip to content
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

feat: support next@15 #426

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@vercel/analytics": "^1.1.1",
"@vercel/speed-insights": "^1.0.2",
"next": "^14.0.4",
"next": "^15.0.0",
"nextra": "^2.13.2",
"nextra-theme-docs": "^2.13.2",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/next-app/package.json
Copy link
Owner

Choose a reason for hiding this comment

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

This example needs to be updated, as the page is accessing params.locale without awaiting it.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"next": "^14.0.4",
"next": "^15.0.0",
"next-international": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/next-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"next": "^14.0.4",
"next": "^15.0.0",
"next-international": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/next-international/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-international",
"version": "1.2.4",
"version": "1.3.0",
"description": "Type-safe internationalization (i18n) for Next.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -50,7 +50,7 @@
},
"devDependencies": {
"@types/react": "^18.2.45",
"next": "^14.0.4",
"next": "^15.0.0",
"react": "^18.2.0",
"tsup": "^8.0.1"
},
Expand Down
31 changes: 17 additions & 14 deletions packages/next-international/src/app/server/create-get-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@
locales: Locales,
config: I18nServerConfig,
) {
const localeCache = new Map<string, ReturnType<typeof createT<Locale, undefined>>>();
const localeCache = new Map<string, Promise<ReturnType<typeof createT<Locale, undefined>>>>();

return async function getI18n() {
const locale = getLocaleCache();
const cached = localeCache.get(locale);
const locale = await getLocaleCache();
let cached = localeCache.get(locale);

Check failure on line 15 in packages/next-international/src/app/server/create-get-i18n.ts

View workflow job for this annotation

GitHub Actions / lint

'cached' is never reassigned. Use 'const' instead

if (cached) {
return cached;
return await cached;
}

const localeFn = createT(
{
localeContent: flattenLocale((await locales[locale]()).default),
fallbackLocale: config.fallbackLocale ? flattenLocale(config.fallbackLocale) : undefined,
locale,
} as LocaleContext<Locale>,
undefined,
);
const localeFnPromise = (async () => {
const localeModule = await locales[locale]();
return createT(
{
localeContent: flattenLocale(localeModule.default),
fallbackLocale: config.fallbackLocale ? flattenLocale(config.fallbackLocale) : undefined,
locale,
} as LocaleContext<Locale>,
undefined,
);
})();

localeCache.set(locale, localeFn);
localeCache.set(locale, localeFnPromise);

return localeFn;
return await localeFnPromise;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@
locales: Locales,
config: I18nServerConfig,
) {
const localeCache = new Map<string, ReturnType<typeof createT<Locale, undefined>>>();
const localeCache = new Map<string, Promise<ReturnType<typeof createT<Locale, undefined>>>>();

return async function getScopedI18n<Scope extends Scopes<Locale>>(
scope: Scope,
): Promise<ReturnType<typeof createT<Locale, Scope>>> {
const locale = getLocaleCache();
const locale = await getLocaleCache();
const cacheKey = `${locale}-${scope}`;
const cached = localeCache.get(cacheKey);
let cached = localeCache.get(cacheKey);

Check failure on line 18 in packages/next-international/src/app/server/create-get-scoped-i18n.ts

View workflow job for this annotation

GitHub Actions / lint

'cached' is never reassigned. Use 'const' instead

if (cached) {
return cached;
return (await cached) as ReturnType<typeof createT<Locale, Scope>>;
}

const localeFn = createT(
{
localeContent: flattenLocale((await locales[locale]()).default),
fallbackLocale: config.fallbackLocale ? flattenLocale(config.fallbackLocale) : undefined,
locale,
} as LocaleContext<Locale>,
scope,
);
const localeFnPromise = (async () => {
const localeModule = await locales[locale]();
return createT(
{
localeContent: flattenLocale(localeModule.default),
fallbackLocale: config.fallbackLocale ? flattenLocale(config.fallbackLocale) : undefined,
locale,
} as LocaleContext<Locale>,
scope,
);
})();

localeCache.set(cacheKey, localeFn);
localeCache.set(cacheKey, localeFnPromise);

return localeFn;
return await localeFnPromise;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export const setStaticParamsLocale = (value: string) => {
getLocale().current = value;
};

export const getLocaleCache = cache(() => {
export const getLocaleCache = cache(async () => {
let locale: string | undefined | null;

locale = getStaticParamsLocale();

if (!locale) {
try {
locale = headers().get(LOCALE_HEADER);
locale = (await headers()).get(LOCALE_HEADER);

if (!locale) {
locale = cookies().get(LOCALE_COOKIE)?.value;
locale = (await cookies()).get(LOCALE_COOKIE)?.value;
}
} catch (e) {
throw new Error(
Expand Down
Loading
Loading