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

Added CROSSORIGIN and eagerness PRERENDER #434

Closed
wants to merge 18 commits into from

Conversation

giorgiopellegrino
Copy link

Dear @addyosmani @XhmikosR ,

I am submitting this pull request to address issues encountered within our Luxottica e-commerce group, where we observed inconsistent resource prefetching behavior across different browsers.

Currently, three prefetching methods are implemented:

  1. viaDom – Prefetches resources without checking the Access-Control-Allow-Origin header, using the attribute crossorigin="anonymous".
  2. viaXhr – Fetches resources while always validating the Access-Control-Allow-Credentials response header.
  3. viaFetch – Executes a fetch request that defaults to mode: "cors" and checks Access-Control-Allow-Origin, while credentials: "include" ensures Access-Control-Allow-Credentials validation.

Additionally, I have handled resource prioritization for Safari devices, adding the priority: "low|high" property specifically for fetch requests. All parameters are set to false by default but can be adjusted as needed.

The updated method signature for prefetching is as follows:

prefetch(url, isPriority, checkAccessControlAllowOrigin, checkAccessControlAllowCredentials)

I would appreciate your review of these improvements.

Thank you,
Giorgio

c.c. @gilbertococchi

src/prefetch.mjs Outdated
// TODO: Investigate using preload for high-priority
// fetches. May have to sniff file-extension to provide
// valid 'as' values. In the future, we may be able to
// use Priority Hints here.
//
// As of 2018, fetch() is high-priority in Chrome
// and medium-priority in Safari.
return window.fetch ? fetch(url, {credentials: 'include'}) : viaXHR(url);
options = {headers: {accept: '*/*'}};

Check warning

Code scanning / CodeQL

Missing variable declaration Warning

Variable options is used like a local variable, but is missing a declaration.
Copy link
Author

Choose a reason for hiding this comment

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

I have updated the declaration for the variable, defining it as a constant using the const keyword.

@giorgiopellegrino
Copy link
Author

Hello @addyosmani ,

I noticed that the code in the pull request did not pass the size limit test. Could you consider increass the size limits? The increase in resource size is due to the introduction of new arguments and parameters.

Thank you,
Giorgio

@giorgiopellegrino giorgiopellegrino changed the title CROSSORIGIN, CREDENTIALS, PRIORITY and ACCEPT MIME CROSSORIGIN and eagerness PRERENDER Nov 27, 2024
@giorgiopellegrino giorgiopellegrino changed the title CROSSORIGIN and eagerness PRERENDER Added CROSSORIGIN and eagerness PRERENDER Nov 27, 2024
@giorgiopellegrino
Copy link
Author

Hello @addyosmani and @XhmikosR,
I have implemented several functionalities necessary for our company in this PR. Below is a summary of the work that has been done.

Prefetch

  • Added mode: cors property in fetch requests and crossorigin: 'anonymous' attribute in prefetch links:
    image
    image

  • Added functionality to manage the control of the Access-Control-Allow-Credentials header:
    image
    image

  • Added functionality to prefetch elements corresponding to the URL upon the mouseenter event:
    image

  • Fixed the priority for fetch requests, which was not considered. The priority property has been added with values "high" or "low":
    image

Prerender

  • Added the handling of the eagerness attribute of the script in the speculation rules:
    image

  • The prerender fallback for devices that do not handle speculation rules has been implemented by adding prefetch with priority high and if the eagerness mode is ‘moderate’ or ‘conservative’ then prefetch is only triggered at the mouseenter
    image

Listen

All the functions added for prefetch and prerender are available in the options property, and they will only be executed when elements enter the viewport: options.checkAccessControlAllowOrigin, options.checkAccessControlAllowCredentials, options.onlyOnMouseover, options.eagerness.

Usage

window.addEventListener('load', () => {
  quicklink.listen({el, priority, onlyOnMouseover,
                    checkAccessControlAllowOrigin, checkAccessControlAllowCredentials,
                    prerender, eagerness});
});
window.addEventListener('load', () => {
  quicklink.prefetch(url, isPriority, 
                    checkAccessControlAllowOrigin, checkAccessControlAllowCredentials,
                    onlyOnMouseover);
});

quicklink.prerender(url | [urls], eagerness);

To accommodate these functionalities, the size of the compiled modules has increased. Consequently, I have updated the limits in the size-limit.json file as outlined below:
image

I hope these changes will be appreciated, as they are currently in use across our e-commerce platforms. The code is ready for validation. Could you please proceed with the review?

Thank you very much.

Giorgio Pellegrino.

cc @gilbertococchi

@giorgiopellegrino
Copy link
Author

Hello,

I have split the ticket into two separate pull requests to make it easier to review and verify. Below are the links to the pull requests:

For this reason, I will proceed with closing this ticket to avoid duplication.

cc. @gilbertococchi

Thank you.

Giorgio Pellegrino

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.

1 participant