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

Prerender EAGERNERSS #436

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
49a212f
add isCrossorigin attribute to prefetch method
giorgiopellegrino May 9, 2024
fdd7d23
added javascript doc
giorgiopellegrino Oct 25, 2024
41c8df5
crossorigin call from fetch and prefetch
giorgiopellegrino Oct 25, 2024
1f2aa43
removed xhr and added Access-Control-Allow-Origin header check
giorgiopellegrino Oct 25, 2024
b0cf0d5
javascript doc fixes
giorgiopellegrino Oct 25, 2024
7cd5371
Add attribute to manage AccessControllAllowCredentials
giorgiopellegrino Oct 25, 2024
0087419
2.3.0
giorgiopellegrino Oct 25, 2024
b53152d
2.4.0
giorgiopellegrino Oct 25, 2024
bcb74c8
removed exception handler
giorgiopellegrino Oct 28, 2024
dfb6161
Merge branch 'GoogleChromeLabs:main' into main
giorgiopellegrino Oct 28, 2024
281255c
2.4.0
giorgiopellegrino Oct 29, 2024
34e0980
Added prerender eagerness property
giorgiopellegrino Nov 26, 2024
5b0b288
Added prefetch fallback for eagerness prerender
giorgiopellegrino Nov 27, 2024
c47627e
added missing variable declaration
giorgiopellegrino Nov 27, 2024
dcd924b
added fallback prefetch module refactoring
giorgiopellegrino Nov 28, 2024
ec067f5
2.4.0
giorgiopellegrino Nov 28, 2024
8afc88c
fix decodeURIComponent
giorgiopellegrino Nov 28, 2024
bc6f2cd
Refactoring call prefetchOnHover function to clean up index.mjs
giorgiopellegrino Nov 29, 2024
5ca43f1
add eagerness property to speculation rules
giorgiopellegrino Dec 4, 2024
037e592
Added control of Access-Control-Allow-Origin and Credentials
giorgiopellegrino Dec 5, 2024
7c61055
remove unused parameters
giorgiopellegrino Dec 6, 2024
ded767c
Related compatible URLs
giorgiopellegrino Feb 11, 2025
f3f0e84
Related compatible URLs
giorgiopellegrino Feb 11, 2025
7f5a54e
Relative URLs compatible
giorgiopellegrino Feb 14, 2025
e0eee44
Revert "Relative URLs compatible"
giorgiopellegrino Feb 14, 2025
653d3d6
Revert "Related compatible URLs"
giorgiopellegrino Feb 14, 2025
6d3f53a
bug fix
giorgiopellegrino Feb 14, 2025
767d89e
Relative URLs compatible
giorgiopellegrino Feb 14, 2025
397d9a5
bug fix naming method "viaFetch"
giorgiopellegrino Mar 18, 2025
5ee2e76
fixing JS DOC on prefetch and prerender methods
giorgiopellegrino Mar 18, 2025
4f9f366
fixing JS DOC on prefetch and prerender methods
giorgiopellegrino Mar 18, 2025
114a2f7
fixing JS DOC on prefetch and prerender methods
giorgiopellegrino Mar 18, 2025
e07b2b4
Merge branch 'main' into crossorigin
addyosmani Mar 22, 2025
09a16c6
Merge branch 'main' into prerender-eagerness
addyosmani Mar 22, 2025
932ea86
Merge branch 'main' of https://github.com/giorgiopellegrino/quicklink…
giorgiopellegrino Mar 22, 2025
5a4ab67
Merge branch 'prerender-eagerness' of https://github.com/giorgiopelle…
giorgiopellegrino Mar 22, 2025
a89d008
Merge branch 'crossorigin' of https://github.com/giorgiopellegrino/qu…
giorgiopellegrino Mar 22, 2025
1ab0a32
Merge branch 'main' of https://github.com/giorgiopellegrino/quicklink…
giorgiopellegrino Mar 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add eagerness property to speculation rules
  • Loading branch information
giorgiopellegrino committed Dec 4, 2024
commit 5ca43f14f2876b1b61b2835a8bbf20daef420ff6
8 changes: 4 additions & 4 deletions .size-limit.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[
{
"path": "dist/quicklink.js",
"limit": "2.36 kB",
"limit": "2 kB",
"gzip": true
},
{
"path": "dist/quicklink.mjs",
"limit": "2.36 kB",
"limit": "2 kB",
"gzip": true
},
{
"path": "dist/quicklink.modern.mjs",
"limit": "1.91 kB",
"limit": "1.6 kB",
"gzip": true
},
{
"path": "dist/quicklink.umd.js",
"limit": "2.43 kB",
"limit": "2 kB",
"gzip": true
}
]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quicklink",
"version": "2.4.0",
"version": "2.3.0",
"description": "Faster subsequent page-loads by prefetching in-viewport links during idle time",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions src/chunks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
**/

import throttle from 'throttles';
import {viaFetch, supported} from './prefetch.mjs';
import {priority, supported} from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';

// Cache of URLs we've prefetched
Expand Down Expand Up @@ -145,7 +145,7 @@ export function prefetch(url, isPriority) {
// ~> so that we don't repeat broken links
toPrefetch.add(str);

return (isPriority ? viaFetch : supported)(
return (isPriority ? priority : supported)(
new URL(str, location.href).toString(),
);
}),
Expand Down
15 changes: 4 additions & 11 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
**/

import throttle from 'throttles';
import {prefetchOnHover, supported, viaFetch} from './prefetch.mjs';
import {prefetchOnHover, supported} from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';
import {addSpeculationRules, hasSpecRulesSupport} from './prerender.mjs';

Expand Down Expand Up @@ -72,8 +72,6 @@ function checkConnection(conn) {
* @param {Object} options - Configuration options for quicklink
* @param {Object|Array} [options.el] - DOM element(s) to prefetch in-viewport links of
* @param {Boolean} [options.priority] - Attempt higher priority fetch (low or high)
* @param {Boolean} [options.checkAccessControlAllowOrigin] - Check Access-Control-Allow-Origin response header
* @param {Boolean} [options.checkAccessControlAllowCredentials] - Check the Access-Control-Allow-Credentials response header
* @param {Boolean} [options.onlyOnMouseover] - Enable the prefetch only on mouseover event
* @param {Array} [options.origins] - Allowed origins to prefetch (empty allows all)
* @param {Array|RegExp|Function} [options.ignores] - Custom filter(s) that run after origin checks
Expand Down Expand Up @@ -151,8 +149,7 @@ export function listen(options = {}) {
// Do not prefetch if will match/exceed limit and user has not switched to shouldOnlyPrerender mode
if (toPrefetch.size < limit && !shouldOnlyPrerender) {
toAdd(() => {
prefetch(hrefFn ? hrefFn(entry) : entry.href, options.priority,
options.checkAccessControlAllowOrigin, options.checkAccessControlAllowCredentials, options.onlyOnMouseover)
prefetch(hrefFn ? hrefFn(entry) : entry.href, options.priority, options.onlyOnMouseover)
.then(isDone)
.catch(error => {
isDone();
Expand Down Expand Up @@ -207,13 +204,10 @@ export function listen(options = {}) {
* Prefetch a given URL with an optional preferred fetch priority
* @param {String} url - the URL to fetch
* @param {Boolean} isPriority - if is "high" priority
* @param {Boolean} checkAccessControlAllowOrigin - true to set crossorigin="anonymous" for DOM prefetch
* and mode:'cors' for API fetch
* @param {Boolean} checkAccessControlAllowCredentials - true to set credentials:'include' for API fetch
* @param {Boolean} onlyOnMouseover - true to enable prefetch only on mouseover event
* @return {Object} a Promise
*/
export function prefetch(url, isPriority, checkAccessControlAllowOrigin, checkAccessControlAllowCredentials, onlyOnMouseover) {
export function prefetch(url, isPriority, onlyOnMouseover) {
const chkConn = checkConnection(navigator.connection);
if (chkConn instanceof Error) {
return Promise.reject(new Error(`Cannot prefetch, ${chkConn.message}`));
Expand All @@ -232,8 +226,7 @@ export function prefetch(url, isPriority, checkAccessControlAllowOrigin, checkAc
// ~> so that we don't repeat broken links
toPrefetch.add(str);

return prefetchOnHover((isPriority ? viaFetch : supported), new URL(str, location.href).toString(), onlyOnMouseover,
checkAccessControlAllowOrigin, checkAccessControlAllowCredentials, isPriority);
return prefetchOnHover((isPriority ? viaFetch : supported), new URL(str, location.href).toString(), onlyOnMouseover);
}),
);
}
Expand Down
28 changes: 7 additions & 21 deletions src/prefetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ function hasPrefetch(link) {
/**
* Fetches a given URL using `<link rel=prefetch>`
* @param {string} url - the URL to fetch
* @param {Boolean} hasCrossorigin - true to set crossorigin="anonymous"
* @return {Object} a Promise
*/
function viaDOM(url, hasCrossorigin) {
function viaDOM(url) {
return new Promise((resolve, reject, link) => {
link = document.createElement('link');
link.rel = 'prefetch';
link.href = url;
if (hasCrossorigin) {
link.setAttribute('crossorigin', 'anonymous');
}

link.onload = resolve;
link.onerror = reject;
Expand All @@ -53,16 +49,13 @@ function viaDOM(url, hasCrossorigin) {
/**
* Fetches a given URL using XMLHttpRequest
* @param {string} url - the URL to fetch
* @param {Boolean} hasCredentials - true to set withCredentials:true
* @return {Object} a Promise
*/
function viaXHR(url, hasCredentials) {
function viaXHR(url) {
return new Promise((resolve, reject, request) => {
request = new XMLHttpRequest();

request.open('GET', url, request.withCredentials = hasCredentials);

request.setRequestHeader('Accept', '*/*');
request.open('GET', url, request.withCredentials = true);

request.onload = () => {
if (request.status === 200) {
Expand All @@ -81,26 +74,21 @@ function viaXHR(url, hasCredentials) {
* Fetches a given URL using the Fetch API. Falls back
* to XMLHttpRequest if the API is not supported.
* @param {string} url - the URL to fetch
* @param {Boolean} hasModeCors - true to set mode:'cors'
* @param {Boolean} hasCredentials - true to set credentials:'include'
* @param {Boolean} isPriority - true to set priority:'high'
* @return {Object} a Promise
*/
export function viaFetch(url, hasModeCors, hasCredentials, isPriority) {
export function priority(url) {
// 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.
const options = {headers: {accept: '*/*'}};
if (!hasModeCors) options.mode = 'no-cors';
if (hasCredentials) options.credentials = 'include';
isPriority ? options.priority = 'high' : options.priority = 'low';
return window.fetch ? fetch(url, options) : viaXHR(url, hasCredentials);
return window.fetch ? fetch(url, { credentials: 'include' }) : viaXHR(url);
}

export const supported = hasPrefetch() ? viaDOM : viaXHR;

/**
* Calls the prefetch function immediately
* or only on the mouseover event.
Expand Down Expand Up @@ -137,5 +125,3 @@ export function prefetchOnHover(callback, url, onlyOnMouseover, ...args) {
el.addEventListener('mouseleave', mouseleaveListener);
}
}

export const supported = hasPrefetch() ? viaDOM : viaFetch;