You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! I've been using this library for a couple of months with SSR. I have followed the locale-router example.
In that example, the layout.server.ts is in charge of loading the needed translations and sending them to the other pages (namely, layout.ts/layout.js). After loading the translations, layout.server.ts returns translations: translations.get(). As noted in the locale-param examples, the translations variable on server contain all translations loaded by different clients.
This means that, after loading the needed translations based on route and locale, the server then sends ALL loaded translations to the client, not just the needed ones. This can easily be verified on the HTML output: all translations that the server has loaded so far are sent, including unneeded languages and unneeded keys from other routes.
I haven't found an option in the library to automatically strip unneeded translations, so I have developed my own utility function in layout.server.ts:
// Remove unneeded translation keys and languagesconstgetStrippedTranslations=(lang: string,route: string)=>{constkeysToLoad=[
...newSet(config.loaders.filter((loader)=>!loader.routes?.length||loader.routes.some((r)=>newRegExp(r).test(route))).map((loader)=>loader.key))];return{[lang]: Object.fromEntries(Object.entries(translations.get()[lang]).filter(([key])=>{// eslint-disable-next-line @typescript-eslint/no-explicit-anyif(AVAILABLE_LANGUAGES.includes(keyasany))returntrue;if(keysToLoad.some((keyToLoad)=>key.startsWith(keyToLoad)))returntrue;returnfalse;}))};};exportconstload: LayoutServerLoad=async({ url, locals })=>{const{ pathname }=url;const{ lang }=locals;awaitloadTranslations(lang,pathname);return{i18n: {route: pathname, lang },translations: getStrippedTranslations(lang,pathname)};};
I believe this optimization should be handled automatically by the library, as the payload can become quite large for websites with a lot of content.
The text was updated successfully, but these errors were encountered:
Hello! I've been using this library for a couple of months with SSR. I have followed the
locale-router
example.In that example, the
layout.server.ts
is in charge of loading the needed translations and sending them to the other pages (namely,layout.ts
/layout.js
). After loading the translations,layout.server.ts
returnstranslations: translations.get()
. As noted in thelocale-param
examples, thetranslations
variable on server contain all translations loaded by different clients.This means that, after loading the needed translations based on route and locale, the server then sends ALL loaded translations to the client, not just the needed ones. This can easily be verified on the HTML output: all translations that the server has loaded so far are sent, including unneeded languages and unneeded keys from other routes.
I haven't found an option in the library to automatically strip unneeded translations, so I have developed my own utility function in
layout.server.ts
:I believe this optimization should be handled automatically by the library, as the payload can become quite large for websites with a lot of content.
The text was updated successfully, but these errors were encountered: