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

refactor(core): prefetch/preload refactor #7282

Merged
merged 1 commit into from
May 2, 2022
Merged

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented May 2, 2022

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

With this change:

  • We are using Set instead of an object as the memoization layer, which should be more performant & has better semantics.
  • The duplicated prefetch memoization has been deduplicated. prefetchHelper now does not memoize on itself; only window.docusaurus.prefetch does. This is fine, because we don't call prefetchHelper directly anywhere else.
  • window.docusaurus.preload and window.docusaurus.prefetch now return Promise<void[]> instead of true, which can allow external callers to wait for them instead of having floating promises.

Test Plan

Prefetching work the same. You can confirm by scrolling down a page and see the network cascade expanding whenever a new link is in viewport.

@Josh-Cena Josh-Cena added the pr: maintenance This PR does not produce any behavior differences to end users when upgrading. label May 2, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 2, 2022
@netlify
Copy link

netlify bot commented May 2, 2022

[V2]

Name Link
🔨 Latest commit 6bc09e2
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/626f6003753aab0008712d30
😎 Deploy Preview https://deploy-preview-7282--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented May 2, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 69
🟢 Accessibility 100
🟢 Best practices 92
🟢 SEO 100
🟢 PWA 90

Lighthouse ran on https://deploy-preview-7282--docusaurus-2.netlify.app/

@github-actions
Copy link

github-actions bot commented May 2, 2022

Size Change: -1 B (0%)

Total Size: 805 kB

ℹ️ View Unchanged
Filename Size Change
website/.docusaurus/globalData.json 50.5 kB 0 B
website/build/assets/css/styles.********.css 107 kB 0 B
website/build/assets/js/main.********.js 609 kB -1 B (0%)
website/build/index.html 38.8 kB 0 B

compressed-size-action

@Josh-Cena
Copy link
Collaborator Author

Hmmm, zero size reduction. Slightly disappointed.

The pnpm failure is unrelated. It's because pnpm 7.0 was released yesterday with pnpm/pnpm#4427. I will follow-up to fix it.

@Josh-Cena Josh-Cena merged commit 53564f3 into main May 2, 2022
@Josh-Cena Josh-Cena deleted the jc/refactor-prefetch branch May 2, 2022 04:56
Comment on lines +352 to +353
prefetch: (url: string) => false | Promise<void[]>;
preload: (url: string) => false | Promise<void[]>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit but what's the advantage of having void[] here?

Maybe it would be better to use await Promise.all([]) and just have Promise<void>?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ahhh... you are right. Not a huge problem though.

Copy link
Collaborator Author

@Josh-Cena Josh-Cena May 4, 2022

Choose a reason for hiding this comment

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

Ah, wait, we can't use await. We never use async-await in client code because that bloats the bundle (in fact I also want to reduce ?? and ?. usages). So in order to make it return void, we have to chain another .then(() => {}) which is not really ideal.

In addition, when it can't prefetch, false is returned synchronously, instead of being wrapped in a promise.

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: maintenance This PR does not produce any behavior differences to end users when upgrading.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants