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

Preload mainJsBundle bundle #6635

Merged
merged 1 commit into from
Dec 17, 2024
Merged

Preload mainJsBundle bundle #6635

merged 1 commit into from
Dec 17, 2024

Conversation

GHaberis
Copy link
Contributor

@GHaberis GHaberis commented Dec 16, 2024

What are you doing in this PR?

"mainJsBundle" is just an example name here...

Currently on the support site we put our <script async src="myScript.js"></script> tag at the bottom of the <body>, doing so in combination with the async attribute ensures that the script is downloaded without blocking other resources or rendering and is executed as soon as it is ready. However the browser needs to parse the page before it reaches this <script> tag, and we don't initialise download until then.

Including <link href="myScript.js" rel="preload" as="script"> in the <head> instructs the browser to start fetching the specified resource (myScript.js) as soon as it encounters the <link> tag in the <head>. This helps reduce latency because the browser doesn't need to wait until it encounters the <script> tag to start downloading the script. See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload for further info.

If a script is critical to the page rendering (as ours are), preloading can be a good optimisation because it ensures the download starts earlier, even before the browser reaches the <script> tag.

I tested main vs this branch on CODE using https://pagespeed.web.dev, which tested as an Emulated Moto G Power device and Slow 4G throttling (using Lighthouse).

The results look positive, I can't see a downside to including this <link>. A further optimisation could be using an HTTP/2 server push to deliver myScript.js even earlier.

3-tier landing page (https://support.code.dev-theguardian.com/uk/contribute)

Metric main gh-preload-js
PageSpeed Analysis (mobile) PageSpeed Analysis (mobile)
FCP (How soon did text and images start to appear) 5.30 1.8
Speed Index (How soon did the page look usable) 5.90 3.6
LCP (When did the largest visible content finish loading) 6.20 4

Generic checkout (https://support.code.dev-theguardian.com/uk/checkout?product=SupporterPlus&ratePlan=Monthly)

Metric main gh-preload-js
PageSpeed Analysis (mobile) PageSpeed Analysis (mobile)
FCP (How soon did text and images start to appear) 1.8 1.8
Speed Index (How soon did the page look usable) 7.60 5.3
LCP (When did the largest visible content finish loading) 4.8 4

New one-time checkout (https://support.code.dev-theguardian.com/uk/one-time-checkout)

Metric main gh-preload-js
PageSpeed Analysis (mobile) PageSpeed Analysis (mobile)
FCP (How soon did text and images start to appear) 1.8 1.8
Speed Index (How soon did the page look usable) 6.8 5.4
LCP (When did the largest visible content finish loading) 1.8 1.8

@GHaberis GHaberis changed the title Preload main js bundle Preload mainJsBundle bundle Dec 16, 2024
Copy link
Contributor

Size Change: 0 B

Total Size: 2.23 MB

ℹ️ View Unchanged
Filename Size
./public/compiled-assets/javascripts/[countryGroupId]/events/router.js 90 kB
./public/compiled-assets/javascripts/[countryGroupId]/router.js 264 kB
./public/compiled-assets/javascripts/ausMomentMap.js 108 kB
./public/compiled-assets/javascripts/contributionsRedirectStyles.js 20 B
./public/compiled-assets/javascripts/digitalSubscriptionLandingPage.js 235 kB
./public/compiled-assets/javascripts/downForMaintenancePage.js 70.4 kB
./public/compiled-assets/javascripts/error404Page.js 70.3 kB
./public/compiled-assets/javascripts/error500Page.js 70.3 kB
./public/compiled-assets/javascripts/favicons.js 617 B
./public/compiled-assets/javascripts/paperSubscriptionCheckoutPage.js 174 kB
./public/compiled-assets/javascripts/paperSubscriptionLandingPage.js 87.2 kB
./public/compiled-assets/javascripts/payPalErrorPage.js 68.6 kB
./public/compiled-assets/javascripts/payPalErrorPageStyles.js 20 B
./public/compiled-assets/javascripts/promotionTerms.js 73.3 kB
./public/compiled-assets/javascripts/subscriptionsLandingPage.js 72.6 kB
./public/compiled-assets/javascripts/subscriptionsRedemptionPage.js 127 kB
./public/compiled-assets/javascripts/supporterPlusLandingPage.js 307 kB
./public/compiled-assets/javascripts/unsupportedBrowserStyles.js 20 B
./public/compiled-assets/javascripts/weeklySubscriptionCheckoutPage.js 171 kB
./public/compiled-assets/javascripts/weeklySubscriptionLandingPage.js 87.1 kB
./public/compiled-assets/webpack/136.js 2.17 kB
./public/compiled-assets/webpack/187.js 21.4 kB
./public/compiled-assets/webpack/344.js 2.01 kB
./public/compiled-assets/webpack/643.js 22.4 kB
./public/compiled-assets/webpack/706.js 107 kB

compressed-size-action

Copy link
Member

@tjmw tjmw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 👏

@GHaberis GHaberis merged commit f963469 into main Dec 17, 2024
20 checks passed
@GHaberis GHaberis deleted the gh-preload-js branch December 17, 2024 10:19
@prout-bot
Copy link

Seen on PROD (merged by @GHaberis 10 minutes and 3 seconds ago)

Sentry Release: support-client-side, support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants