Skip to content

Commit

Permalink
fix: use cache async
Browse files Browse the repository at this point in the history
  • Loading branch information
blechatellier committed Oct 22, 2024
1 parent 69884f3 commit c5e91d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
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 @@ export function createGetI18n<Locales extends ImportedLocales, Locale extends Ba
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 @@ export function createGetScopedI18n<Locales extends ImportedLocales, Locale exte
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;
};
}

0 comments on commit c5e91d5

Please sign in to comment.