-
Notifications
You must be signed in to change notification settings - Fork 7
Load the FAQ articles on demand instead of in every route's locale bundle #1616
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| "use client"; | ||
|
|
||
| import { primeEnglishFaq } from "./faq"; | ||
|
|
||
| interface Props { | ||
| resources: { static: { faq: Record<string, string> } }; | ||
| } | ||
|
|
||
| /** | ||
| * Hands the server-loaded English FAQ articles to the client before the FAQ | ||
| * page's client components hydrate. English only, see primeEnglishFaq; a | ||
| * visitor on another language gets the whole locale file through loadLocale | ||
| * when the client switches. Rendered by the server page ahead of them, so the | ||
| * registration (idempotent, no listeners) happens in tree order during the | ||
| * hydration render and those components see the same strings the server | ||
| * rendered; loading them in an effect instead would hydrate raw keys first and | ||
| * then flash to the text (#1598). | ||
| */ | ||
| export function FaqResources({ resources }: Props) { | ||
| primeEnglishFaq(resources); | ||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Splits the FAQ articles out of the eagerly bundled en-US locale. | ||
| // | ||
| // `static.faq.*-header` / `*-body` are the FAQ articles: 262 keys, ~17 KB | ||
| // gzipped, about a quarter of en-US.json, and only the FAQ surfaces render | ||
| // them. Without this every route shipped them in first-load JS (#1598). | ||
| // | ||
| // This is a webpack loader (see next.config.js) applied to | ||
| // `features/i18n/locales/en-US.json`: | ||
| // - `require("./locales/en-US.json")` -> the locale WITHOUT the articles | ||
| // - `import("./locales/en-US.json?faq")` -> ONLY the articles | ||
| // The JSON on disk stays whole, so Crowdin keeps one source file and the other | ||
| // locales (already loaded on demand as whole files) need no change. | ||
| // | ||
| // Plain CommonJS: webpack loads it at build time, and the spec imports it. | ||
| const FAQ_CONTENT_KEY = /-(header|body)$/; | ||
|
|
||
| function splitFaq(locale) { | ||
| const faq = (locale.static && locale.static.faq) || {}; | ||
| const coreFaq = {}; | ||
| const contentFaq = {}; | ||
| for (const key of Object.keys(faq)) { | ||
| (FAQ_CONTENT_KEY.test(key) ? contentFaq : coreFaq)[key] = faq[key]; | ||
| } | ||
| const core = { ...locale, static: { ...locale.static, faq: coreFaq } }; | ||
| return { core, faq: { static: { faq: contentFaq } } }; | ||
| } | ||
|
|
||
| function isFaqQuery(resourceQuery) { | ||
| return typeof resourceQuery === "string" && /(^|[?&])faq(=|&|$)/.test(resourceQuery); | ||
| } | ||
|
|
||
| function loader(source) { | ||
| const { core, faq } = splitFaq(JSON.parse(source)); | ||
| const out = isFaqQuery(this.resourceQuery) ? faq : core; | ||
| return `module.exports = ${JSON.stringify(out)};`; | ||
| } | ||
|
|
||
| module.exports = loader; | ||
| module.exports.splitFaq = splitFaq; | ||
| module.exports.isFaqQuery = isFaqQuery; | ||
| module.exports.FAQ_CONTENT_KEY = FAQ_CONTENT_KEY; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import i18n from "i18next"; | ||
| import { loadLocale } from "./index"; | ||
|
|
||
| /** | ||
| * FAQ articles (`static.faq.*-header` / `*-body`) are split out of the eagerly | ||
| * bundled en-US locale by the webpack loader in ./faq-split.js and merged back | ||
| * into the "translation" namespace on demand, so every existing | ||
| * `i18next.t("static.faq.…")` call keeps working once `ensureFaqLoaded` has | ||
| * resolved for the active language (#1598). | ||
| * | ||
| * Other locales are loaded on demand as whole files (see loadLocale), articles | ||
| * included, so for them "loaded" simply means the locale bundle is present. | ||
| */ | ||
| const PROBE_KEY = "what-is-ecency-header"; | ||
| const pending = new Map<string, Promise<void>>(); | ||
|
|
||
| type LocaleJson = { static?: { faq?: Record<string, string> } } & Record<string, unknown>; | ||
|
|
||
| // The loader returns only `{ static: { faq } }`; without it (vitest, or a | ||
| // bundler that ignores the rule) the import resolves to the whole locale, so | ||
| // reduce it to the same shape either way. | ||
| function pickFaq(mod: unknown): { static: { faq: Record<string, string> } } { | ||
| const json = ((mod as { default?: LocaleJson }).default ?? mod) as LocaleJson; | ||
| return { static: { faq: json.static?.faq ?? {} } }; | ||
| } | ||
|
|
||
| // Probes the bundle itself rather than remembering what was loaded, so a | ||
| // bundle that is replaced or removed elsewhere is simply loaded again. | ||
| export function isFaqLoaded(lang: string = i18n.language): boolean { | ||
| const bundle = i18n.getResourceBundle(lang, "translation") as LocaleJson | undefined; | ||
| return Boolean(bundle?.static?.faq?.[PROBE_KEY]); | ||
| } | ||
|
|
||
| /** | ||
| * Register the ENGLISH articles synchronously (idempotent). English only: the | ||
| * other locales are owned by loadLocale as whole files, and registering a | ||
| * partial bundle for one of them would make loadLocale believe the locale is | ||
| * already present and never fetch it, leaving the rest of the UI untranslated. | ||
| */ | ||
| export function primeEnglishFaq(resources: { static: { faq: Record<string, string> } }) { | ||
| if (isFaqLoaded("en-US")) return; | ||
| i18n.addResourceBundle("en-US", "translation", resources, true, true); | ||
| } | ||
|
|
||
| export function ensureFaqLoaded(lang: string = i18n.language || "en-US"): Promise<void> { | ||
| if (isFaqLoaded(lang)) return Promise.resolve(); | ||
| const inFlight = pending.get(lang); | ||
| if (inFlight) return inFlight; | ||
| const task = (async () => { | ||
| if (lang === "en-US") { | ||
| const mod = await import(/* webpackChunkName: "i18n-faq" */ "./locales/en-US.json?faq"); | ||
| primeEnglishFaq(pickFaq(mod)); | ||
| } else { | ||
| // Whole-file locale, articles included. en-US is the fallback language | ||
| // for any article a translation lacks, and its articles are no longer | ||
| // in the eager bundle, so load them alongside or an untranslated | ||
| // article would render as its raw key. | ||
| await Promise.all([loadLocale(lang), ensureFaqLoaded("en-US")]); | ||
| } | ||
| })().finally(() => pending.delete(lang)); | ||
| pending.set(lang, task); | ||
| return task; | ||
| } | ||
|
|
||
| /** | ||
| * The English articles, for a server component to hand to <FaqResources> so | ||
| * client components (which hydrate in the browser's language, English until a | ||
| * switch loads a whole locale file) have them before they render. Call after | ||
| * ensureFaqLoaded("en-US"). | ||
| */ | ||
| export function getEnglishFaqResources(): { static: { faq: Record<string, string> } } { | ||
| const bundle = i18n.getResourceBundle("en-US", "translation") as LocaleJson | undefined; | ||
| const faq = bundle?.static?.faq ?? {}; | ||
| const content: Record<string, string> = {}; | ||
| for (const key of Object.keys(faq)) { | ||
| if (/-(header|body)$/.test(key)) content[key] = faq[key]; | ||
| } | ||
| return { static: { faq: content } }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // `./locales/en-US.json?faq` is served by the webpack loader in ./faq-split.js | ||
| // (the FAQ articles only). Type it loosely; faq.ts reduces whatever comes back | ||
| // to the shape it needs. | ||
| declare module "*.json?faq" { | ||
| const value: unknown; | ||
| export default value; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.