ampproject / amphtml

The AMP web component framework.
https://amp.dev
Apache License 2.0
14.89k stars 3.89k forks source link

[Story localization] Lazy load locale strings on stories #37344

Open mszylkowski opened 2 years ago

mszylkowski commented 2 years ago

Summary

21 language bundles are currently bundled in amp-story-1.0.js, and contribute towards 8.7kB of the bundle size (about 16% as of Jan 10, probably around 20% after other performance improvements). Larger payloads contribute towards delays in rendering as other assets (like images) are throttled, as well as the more critical logic in amp-story.js and v0.js that should run ASAP.

We want to load only the document language bundle without having to download the other 20 unused localization bundles. We can serve the localization strings from the AMP CDN as JSON files (where we can automatically compute fallbacks) and fetch the appropriate bundle after amp-story.js is loaded.

Benefits:

Currently the uses of the localization bundles are synchronous to the component build stages, but we would need to asynchronously use these resources so we don't block the rendering until the language bundle is downloaded [draft].

Most of the localized strings are used for:

We can work to load the localization strings as efficiently as possible for each serving type:

FAQ

1) Does this prevent us from having strings displayed from the beginning of the story?

It doesn't, given how fast the translations get downloaded. This is generally faster than the Poppins font lazy-loaded by amp-story-interactive or amp-story-shopping (which has a size of 8kB per font-weight, compared to 0.6kB for the translations). This results in CLS of 0 for all tests I made with decent speeds, and potentially in the order of 10^-4 CLS on other cases, due to how little the UI would shift (which can be reduced to 0 with good styling). This is unless we want to show a huge string in the middle of the screen initially, in which case the delay between amp-story.js and the translations load could increase LCP just a bit, but the CLS would be the same from adding a large text when amp-story loads, or when the translations load. We could get rid of most of this CLS with SSR or good styles (CLS would only happen if adding the text shifts other things, so leaving the space empty would prevent any CLS).

2) Do we need to signal screen readers the system layer's aria labels are not loaded (before they load)

Screen readers first read the title of the document when a page is accessed, and this alone would give enough time for the translations to load in +99% of cases before the screen reader even has time to reach any buttons.

3) Can we lazy-load the strings of amp-story-auto-ads as well?

Yes, we can either include it on the main locale bundles or have a separate set of bundles. This is not a priority for now though.

Notifications

/cc @ampproject/wg-approvers @ampproject/wg-stories

gmajoulet commented 2 years ago

I very strongly support this.

Key points for me:

newmuis commented 2 years ago

I think this is definitely worth considering, and am happy about the "better LCP" and "more locales" points. I want to see if we can refine the idea further to address a few points:

mszylkowski commented 2 years ago

The plan is:

  1. Use inlined strings if available. On amp-cache the strings for the given version will be inlined. If the version matches the RTV, use the strings inlined and don't install the 20 languages. #37836
  2. Add an experiment called "story-remote-locales" (to start testing the implementation) that fetches the localization strings instead of using the ones bundled with amp-story. This will make early usages of the strings fail because they are currently calling the translations synchronously (which is fine because we'll fix that next). #37843
    • If a language doesn't exist, the cache will make the right fallback.
    • Locally it will fetch from localhost; and on deployed stories from the CDN.
  3. Make all translations fill the strings async: amp-story, system-layer, unsupported browser, 360, interactives, shopping, consent, hint, info-dialog, pagination-buttons, forms, page-attachment, share-menu. #37303
    • Could be done in multiple PRs to make them easier to review.
    • Check any significant changes to the CLS (not expected to happen).
    • Will need to refactor localization service with changes explained below:
      • Register multiple bundles at the same time so we don't need to check every pending string when ergistering every language and bundle.
      • Add a helper that will localize a template async
  4. Clean up synchronous localization code (which will be unused by then).
mszylkowski commented 2 years ago

Update: This was implemented to completion in amp-story, we have an experiment that we need to roll out for the launch, and then clean up the code

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.