examples(i18n-routing): use React.cache() to deduplicate dictionary loading

Wrapping getDictionary with cache() prevents duplicate dictionary
imports when multiple server components request the same locale
within a single request.
parent 6ade4ff6
import "server-only"; import "server-only";
import { cache } from "react";
import type { Locale } from "./i18n-config"; import type { Locale } from "./i18n-config";
// We enumerate all dictionaries here for better linting and typescript support // We enumerate all dictionaries here for better linting and typescript support
...@@ -9,5 +10,7 @@ const dictionaries = { ...@@ -9,5 +10,7 @@ const dictionaries = {
cs: () => import("./dictionaries/cs.json").then((module) => module.default), cs: () => import("./dictionaries/cs.json").then((module) => module.default),
}; };
export const getDictionary = async (locale: Locale) => // cache() deduplicates dictionary loading within a single request
dictionaries[locale]?.() ?? dictionaries.en(); export const getDictionary = cache(
async (locale: Locale) => dictionaries[locale]?.() ?? dictionaries.en(),
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment