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

📦 externalized portal locales #21945

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vikaspotluri123
Copy link
Member

no issue

All locales were previously included in the main bundle, even though at most 1 locale is used.
By separating out the locales, we can reduce the bundle size by ~60%:

raw gzip
Before 1,542.72 kB 365.86 kB
After 546.38 kB 156.29 kB
Savings 996.34 kB (65%) 209.57 kB (57%)
After (+1 locale) 564.48 kB 162.54 kB
Savings (+1 locale) 978.24 kB (63%) 203.32 kB (56%)
  • Added an async i18n factory that fetches locales on-demand
    • Implemented in a separate browser entrypoint for tree shaking
  • Created an i18n singleton/cache to avoid downloading the same locale multiple times
  • Allowed the locale fetch root to be overridden via data-locale-root
  • Added vite-plugin-ghost-i18n (plugin.js) in @tryghost/i18n to:
    • Emit each portal locale as a build asset
    • Minify the locales (using JSON.stringify)
    • Prevent rewriting import.meta.urlGot some code for us? Awesome 🎊!

Backwards Compatibility

The new plugin (vite-plugin-ghost-i18n) supports incremental adoption, based on stability. Since this hasn't been tested at-scale, Portal's stability is currently opt-in.

Stability Mode Main Bundle Type Secondary Bundle Name Secondary Bundle Type
opt-in No change (inline locales) {ns}-no-locales.min.js Async locales
opt-out Async locales {ns}-locales.min.js Inline locales
solo Async locales N/A N/A

no issue

All locales were previously included in the main bundle, even though at most 1 locale is used.
By separating out the locales, we can reduce the bundle size by ~60%:

|                     | raw             | gzip            |
| ------------------- | --------------- | --------------- |
| Before              | 1,542.72 kB     | 365.86 kB       |
| After               | 546.38 kB       | 156.29 kB       |
| Savings             | 996.34 kB (65%) | 209.57 kB (57%) |
| After (+1 locale)   | 564.48 kB       | 162.54 kB       |
| Savings (+1 locale) | 978.24 kB (63%) | 203.32 kB (56%) |

 - Added an async i18n factory that fetches locales on-demand
  - Implemented in a separate browser entrypoint for tree shaking
 - Created an i18n singleton/cache to avoid downloading the same locale multiple times
 - Allowed the locale fetch root to be overridden via `data-locale-root`
 - Added `vite-plugin-ghost-i18n` (plugin.js) in `@tryghost/i18n` to:
   - Emit each portal locale as a build asset
   - Minify the locales (using `JSON.stringify`)
   - Prevent rewriting `import.meta.url`
3 Modes:
 - `opt-in` generates `${ns}-no-locales.min.js` with async locales and  `${ns}.min.js` with inlined locales
 - `opt-out` generates `${ns}-locales.min.js` with inlined locales and `${ns}.min.js` with async locales
 - `solo` generates `${ns}.min.js` with async locales
`import.meta.url` is not available unless the script is executing in a module context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant