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

perf(v2): load algolia JS only when user interacts with search #2076

Merged
merged 2 commits into from
Dec 2, 2019

Conversation

endiliey
Copy link
Contributor

@endiliey endiliey commented Dec 2, 2019

Motivation

Docsearch bundle size is a whooping 35kbs gzipped. That's huge ...

js files in v2docusaurus

Trying to improve our TTI performance by only loading algolia when user interact with the search

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

Still working ✌️
lazyload algolia normal

Notice how the bundle is only loaded when user interact with it
lazyload algolia

Benefit.

  • Less bundle size shipped to user. 35kbs gzipped is huge. At first page load, our main thread is already busy parsing other javascript.

This increase our lighthouse TTI by 1s
After is 2.8s+
lighthouse 99

Before was 3.9s +
lighthouse 96

Slight caveat:

  • There might be some kind of delay when user "search" with algolia for the first time since the JS is lazily loaded. Almost unnoticeable though, depends on internet speed. But since main thread is no longer busy, it will definitely be faster. see gif preview or try it on netlify

@endiliey endiliey added the pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient. label Dec 2, 2019
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Dec 2, 2019
@endiliey endiliey changed the title perf(v2): lazy load algolia JS when the user interacts with the search perf(v2): load algolia JS only when user interacts with search Dec 2, 2019
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Dec 2, 2019

Deploy preview for docusaurus-2 ready!

Built with commit 998fc89

https://deploy-preview-2076--docusaurus-2.netlify.com

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Dec 2, 2019

Deploy preview for docusaurus-preview ready!

Built with commit 998fc89

https://deploy-preview-2076--docusaurus-preview.netlify.com

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Very nice! Refer to comments :smile

});
const loadAlgoliaJS = () => {
if (!loadedJs) {
loadJS().then(a => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wanna add some error handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dont know what kind of error should be introduced. Previously doesnt have it too.

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

LGTM

@yangshun yangshun merged commit cc2201e into master Dec 2, 2019
@yangshun yangshun deleted the endi/lazyloadalgolia branch December 2, 2019 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants