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
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
39 changes: 26 additions & 13 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import throttle from 'throttles';
import {prefetchOnHover, supported, viaFetch} from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';
import {addSpeculationRules, hasSpecRulesSupport} from './prerender.mjs';
import {addSpeculationRules, removeSpeculationRule, hasSpecRulesSupport} from './prerender.mjs';

// Cache of URLs we've prefetched
// Its `size` is compared against `opts.limit` value.
Expand Down Expand Up @@ -102,6 +102,7 @@ export function listen(options = {}) {
const ignores = options.ignores || [];
const delay = options.delay || 0;
const hrefsInViewport = [];
specRulesInViewport = new Map();

const timeoutFn = options.timeoutFn || requestIdleCallback;
const hrefFn = typeof options.hrefFn === 'function' && options.hrefFn;
Expand Down Expand Up @@ -131,19 +132,26 @@ export function listen(options = {}) {
// Do not prefetch if not found in viewport
if (!hrefsInViewport.includes(entry.href)) return;

observer.unobserve(entry);
if (!shouldOnlyPrerender && !shouldPrerenderAndPrefetch) observer.unobserve(entry);

// prerender, if..
// either it's the prerender + prefetch mode or it's prerender *only* mode
// Prerendering limit is following options.limit. UA may impose arbitraty numeric limit
if ((shouldPrerenderAndPrefetch || shouldOnlyPrerender) && toPrerender.size < limit) {
prerender(hrefFn ? hrefFn(entry) : entry.href, options.eagerness).catch(error => {
if (options.onError) {
options.onError(error);
} else {
throw error;
}
});
// The same URL is not already present as a speculation rule
if ((shouldPrerenderAndPrefetch || shouldOnlyPrerender) && toPrerender.size < limit && !specRulesInViewport.has(entry.href)) {
prerender(hrefFn ? hrefFn(entry) : entry.href, options.eagerness)
.then(specMap => {
for (const [key, value] of specMap) {
specRulesInViewport.set(key, value);
}
})
.catch(error => {
if (options.onError) {
options.onError(error);
} else {
throw error;
}
});

return;
}
Expand All @@ -168,6 +176,9 @@ export function listen(options = {}) {
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.
}
}
});
}, {
Expand Down Expand Up @@ -245,6 +256,8 @@ export function prefetch(urls, isPriority, checkAccessControlAllowOrigin, checkA
* @return {Object} a Promise
*/
export function prerender(urls, eagerness = 'immediate') {
urls = [].concat(urls);

const chkConn = checkConnection(navigator.connection);
if (chkConn instanceof Error) {
return Promise.reject(new Error(`Cannot prerender, ${chkConn.message}`));
Expand All @@ -258,7 +271,7 @@ export function prerender(urls, eagerness = 'immediate') {
return Promise.reject(new Error('This browser does not support the speculation rules API. Falling back to prefetch.'));
}

for (const url of [].concat(urls)) {
for (const url of urls) {
toPrerender.add(url);
}

Expand All @@ -267,6 +280,6 @@ export function prerender(urls, eagerness = 'immediate') {
console.warn('[Warning] You are using both prefetching and prerendering on the same document');
}

const addSpecRules = addSpeculationRules(toPrerender, eagerness);
return addSpecRules === true ? Promise.resolve() : Promise.reject(addSpecRules);
const specMap = addSpeculationRules(urls, eagerness);
return specMap.size > 0 ? Promise.resolve(specMap) : Promise.reject(specMap);
}
47 changes: 38 additions & 9 deletions src/prerender.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,52 @@

/**
* Add a given set of urls to the speculation rules
* @param {Set} urlsToPrerender - the URLs to add to speculation rules
* @param {String[]} urlsToPrerender - the URLs to add to speculation rules
* @param {String} eagerness - prerender eagerness mode
* @return {Boolean|Object} boolean or Error Object
* @return {Map<HTMLScriptElement, string>|Object} Map of script elements to their URLs or Error Object
*/
export function addSpeculationRules(urlsToPrerender, eagerness) {
const specScript = document.createElement('script');
specScript.type = 'speculationrules';
specScript.text = `{"prerender":[{"source": "list",
"urls": ["${Array.from(urlsToPrerender).join('","')}"],
"eagerness": "${eagerness}"}]}`;
const specMap = new Map();

try {
for (const url of urlsToPrerender) {
const specScript = document.createElement('script');
specScript.type = 'speculationrules';
specScript.text = JSON.stringify({
prerender: [{
source: 'list',
urls: [url],
eagerness: eagerness,
}],
});

document.head.appendChild(specScript);
specMap.set(url, specScript);
}
} catch (error) {
return error;
}

return specMap;
}

/**
* Removes a speculation rule script associated with a given URL
* @param {Map<string, HTMLScriptElement>} specMap - Map of URLs to their script elements
* @param {string} url - The URL whose speculation rule should be removed
* @return {Map<string, HTMLScriptElement>|Object} The updated map after removal or Error Object
*/
export function removeSpeculationRule(specMap, url) {
const specScript = specMap.get(url);

try {
document.head.appendChild(specScript);
specScript.remove();
specMap.delete(url);
} catch (error) {
return error;
}

return true;
return specMap;
}

/**
Expand Down