Skip to content

Improve speculation rules handling based on element visibility #446

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

Merged

Conversation

giorgiopellegrino
Copy link
Contributor

Pull Request Description:
This pull request addresses the issue raised in #442 by enhancing how speculation rules are managed based on the visibility of DOM elements.

Specifically, speculation rules are now added only when the target element is within the viewport and removed when it exits the viewport. This change enables developers to safely use quicklink.listen(el, { prerender: true, eagerness: 'immediate' }) on mobile devices without the risk of hitting the browser-imposed limit of 10 prerendered pages.

This implementation improves resource management and ensures a more efficient and predictable prerendering behavior.

Thanks,

Giorgio Pellegrino.

cc @gilbertococchi

Copy link
Contributor

@gilbertococchi gilbertococchi left a comment

Choose a reason for hiding this comment

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

This is great Giorgio!

I've run some tests and it looks like a great start!

Would you be open to have a testing HTML page added to verify the behaviour and also add some documentation changes that explains what this combination will work on QuickLink?

@XhmikosR
Copy link
Collaborator

CI fails, please make sure tests are valid.

@gilbertococchi
Copy link
Contributor

gilbertococchi commented May 26, 2025

TODO in order to complete merge this PR:

  • Document the behaviour in the Documentation docs
  • Fix failing test
  • Removing eagerness=immediate condition on unobserve call.

Changed "urlsToPrerender" property type on JSDoc

Co-authored-by: Weston Ruter <[email protected]>
Copy link
Contributor

@gilbertococchi gilbertococchi left a comment

Choose a reason for hiding this comment

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

Thanks Giorgio, all the changes here looks good now.

@gilbertococchi
Copy link
Contributor

Thanks @giorgiopellegrino for adding the documentation changes, can you please add them as part of this PR?

Context: giorgiopellegrino@f522eef

giorgiopellegrino and others added 2 commits June 2, 2025 15:56
Error return type object

Co-authored-by: Weston Ruter <[email protected]>
const variable specRulesInViewport

Co-authored-by: Weston Ruter <[email protected]>
Added functionality to the listen({ prerender: true }) method to allow prerendering only of content within the viewport, and introduced the eagerness property to the prerender method.
@addyosmani
Copy link
Collaborator

This is looking close to ready to merge! @giorgiopellegrino can we update it to be merge-friendly with the main branch and double check CI is passing?

@giorgiopellegrino
Copy link
Contributor Author

This is looking close to ready to merge! @giorgiopellegrino can we update it to be merge-friendly with the main branch and double check CI is passing?

Hi @addyosmani ,
I've merged from the updated main branch and can confirm that it passes the local tests.

giorgiopellegrino and others added 5 commits June 10, 2025 10:34
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Copy link
Contributor

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

LGTM!

@addyosmani
Copy link
Collaborator

A big thank you to everyone for the very thorough reviews on these changes and to @giorgiopellegrino for the additions. These LGTM to be merged and will be included in the next release.

@addyosmani addyosmani merged commit 805477a into GoogleChromeLabs:main Jun 15, 2025
9 of 10 checks passed
@@ -168,6 +178,9 @@
if (index > -1) {
hrefsInViewport.splice(index);
}
if (specRulesInViewport.has(entry.href)) {
specRulesInViewport = removeSpeculationRule(specRulesInViewport, entry.href);

Check failure

Code scanning / CodeQL

Assignment to constant Error

Assignment to variable specRulesInViewport, which is
declared
constant.
@XhmikosR
Copy link
Collaborator

This shouldn't have landed as is; there's a syntax error as shown by CodeQL :/

@XhmikosR
Copy link
Collaborator

Also, the size limit check failed, I increased in on main but should have been increased in this PR too.

@XhmikosR
Copy link
Collaborator

Oh, also the site docs should reflect the new addition.

@XhmikosR
Copy link
Collaborator

XhmikosR commented Jun 18, 2025

Makes me wonder why tests (not lint) didn't catch the const assignment. It's like tests don't cover this path...

Regardless, the docs and the error should be fixed and in the future we better not merge when CI is red :)

I also explicitly enabled the related ESLint rule on main.

@XhmikosR XhmikosR mentioned this pull request Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants