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

Instant Search: Lazy load #19049

Merged
merged 5 commits into from
Mar 12, 2021
Merged

Instant Search: Lazy load #19049

merged 5 commits into from
Mar 12, 2021

Conversation

sgomes
Copy link
Contributor

@sgomes sgomes commented Mar 5, 2021

Instant Search is currently served up as a (mostly) single bundle, with an additional polyfill chunk that only gets loaded for IE 11. Because of the way JS loading is traditionally done in WordPress, this is loaded as part of the critical path, delaying first render and page interactivity.

This PR explores one approach for changing this paradigm, by loading a much smaller amount of code in the critical path. It accomplishes this by moving most of the code to a dynamic import that only runs on DOMContentLoaded (i.e., when the document is done parsing).

This results in an order of magnitude less JavaScript in the critical path (from somewhere in the 40KB gzipped range to somewhere in the 2KB gzipped range) for the Instant Search feature, with relatively minor changes to the JS code and no minor changes needed to the PHP side of things.

Incidentally, the benefits extend to CSS as well, which automatically gets split and loaded similarly.

Changes proposed in this Pull Request:

  • Lazily load Instant Search

Jetpack product discussion

None.

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

  • Load up a page using instant search, and smoke-test its functionality
  • Ensure that loading a page with a search string (https://example.com/?s=query) correctly pops up Instant Search on open.

I tested this on my wpcom sandbox, so it may be a good idea for reviewers to try it out on e.g. a local install.

Proposed changelog entry for your changes:

  • Instant Search: improve performance by lazily loading more of the library

@sgomes sgomes requested review from bluefuton, jeherve, jsnmoon and a team March 5, 2021 16:07
@sgomes sgomes self-assigned this Mar 5, 2021
@github-actions github-actions bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Feature] Search For all things related to Search labels Mar 5, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2021

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


If you are an automattician, once your PR is ready for review add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.undefined


jetpack plugin:

  • Next scheduled release: April 6, 2021.
  • Scheduled code freeze: March 29, 2021

Copy link
Member

@jsnmoon jsnmoon left a comment

Choose a reason for hiding this comment

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

Great changes all around. The loader bundle is now 2.31 kB gzipped, which is an order of magnitude better than loading the entire application bundle at once (46.05 kB gzipped).

This change as-is breaks our bundle size limit testing, unfortunately (yarn test-search-size). Once that's squared away, this PR should be good to go.

@jsnmoon jsnmoon added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Team Review labels Mar 5, 2021
@sgomes
Copy link
Contributor Author

sgomes commented Mar 8, 2021

This change as-is breaks our bundle size limit testing, unfortunately (yarn test-search-size). Once that's squared away, this PR should be good to go.

It should be taken care of now 👍

@sgomes sgomes requested a review from jsnmoon March 8, 2021 11:43
@sgomes sgomes added [Status] Needs Team Review and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Mar 8, 2021
Copy link
Member

@jsnmoon jsnmoon left a comment

Choose a reason for hiding this comment

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

Changes look good, but no search bundles are being loaded at the moment due to this conditional; this is happening due to _inc/build/instant-search/jp-search-main.bundle.css no longer existing.

I think deleting all mentions of this bundled CSS like so should fix this regression.

@jsnmoon
Copy link
Member

jsnmoon commented Mar 8, 2021

Just an aside: yarn clean-search is currently disabled on WPCOM at the moment -- I haven't been able to figure out how to modify the Jetpack Blocks Build job to automatically invoke svn delete on removed files. As a consequence, _inc/build/instant-search/ in WPCOM has been steadily accumulating outdated/obsolete jp-search.chunk-*.js files thus far. With this change, I imagine we'll start accumulating even more obsolete files going forward.

EDIT: Noted in p3topS-Sn-p2.

@sgomes
Copy link
Contributor Author

sgomes commented Mar 9, 2021

Changes look good, but no search bundles are being loaded at the moment due to this conditional; this is happening due to _inc/build/instant-search/jp-search-main.bundle.css no longer existing.

I think deleting all mentions of this bundled CSS like so should fix this regression.

Good catch, thanks @jsnmoon! I've been testing this in wpcom, and that conditional doesn't seem to be present there. This should now be fixed.

@sgomes sgomes force-pushed the update/lazy-load-instant-search branch from d62e30b to 0929d5b Compare March 9, 2021 12:05
@sgomes
Copy link
Contributor Author

sgomes commented Mar 9, 2021

Rebased.

@sgomes sgomes requested a review from jsnmoon March 9, 2021 12:06
jsnmoon
jsnmoon previously approved these changes Mar 9, 2021
Copy link
Member

@jsnmoon jsnmoon left a comment

Choose a reason for hiding this comment

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

Great work! 🚢

@sgomes
Copy link
Contributor Author

sgomes commented Mar 10, 2021

Thank you, @jsnmoon! I was going to commit, but it seems that the patch can't apply cleanly to wpcom (probably because of the aforementioned differences), which is blocking the PR.

What do you think would be the best way forward? It's not clear to me whether the different code in wpcom is intentional or the result of some sort of syncing flaw.

@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello sgomes! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer and confirm D58433-code works as expected before merging this PR. Once this PR is merged, please commit the changes to WP.com. Thank you!
This revision will be updated with each commit to this PR

@jeherve jeherve added this to the 9.6 milestone Mar 10, 2021
@jsnmoon
Copy link
Member

jsnmoon commented Mar 11, 2021

What do you think would be the best way forward? It's not clear to me whether the different code in wpcom is intentional or the result of some sort of syncing flaw. (@sgomes)

I went ahead and manually sorted out the patch conflicts for D58433-code. It should be good to go, but some smoke-testing before shipping might be a good idea!

Aside: I'd consider the mismatch between Jetpack code and WPCOM code to be tech debt and will likely need to be ironed out in the near future.

@sgomes
Copy link
Contributor Author

sgomes commented Mar 12, 2021

I went ahead and manually sorted out the patch conflicts for D58433-code. It should be good to go, but some smoke-testing before shipping might be a good idea!

I checked and everything was good 👍

Adding the changelog, which is now mandatory, seems to have invalidated the previous review. I wish that check was smarter, as a changelog file is clearly not code and doesn't need a review.

In order to move things along, I deployed the wpcom change. I'm not sure if that was a good idea, as now the wpcom check is complaining again (I should have foreseen that).

Sorry for all the extra work. The Jetpack deployment process is very complex, and I haven't fully wrapped my head around it 😕

Copy link
Member

@jsnmoon jsnmoon left a comment

Choose a reason for hiding this comment

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

Happy to move things along, this is a great change! Forced the WPCOM check to true via MC's fusion tool.

@jsnmoon jsnmoon merged commit c95c00f into master Mar 12, 2021
@jsnmoon jsnmoon deleted the update/lazy-load-instant-search branch March 12, 2021 21:13
@jsnmoon
Copy link
Member

jsnmoon commented Mar 12, 2021

@sgomes shipped to WPCOM in r222614-wpcom.

@github-actions
Copy link
Contributor

Great news! One last step: head over to your WordPress.com diff, D58433-code, and commit it.
Once you've done so, come back to this PR and add a comment with your changeset ID.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Search For all things related to Search [Focus] Performance [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ Touches WP.com Files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants