Write a function that picks speculation rules for a page's links.
Leaf and Kettle's pages know their links, sorted by how often visitors open each one next, most likely first, from the shop's analytics. Write pickSpeculationRules(links, budget), which returns the speculation rules the page puts, as JSON, in its <script type="speculationrules">. Each link is { href, sameOrigin, hasQuery, isLogout }: its address as the page writes it, whether it's on the page's own origin, whether it has a query string, and whether opening it signs the visitor out.
Never speculate on a link to another origin or on a sign-out link: Chrome prerenders only same-origin pages unless a page on the same site opts in, and fetching a sign-out page signs the visitor out even if they never click. A link listed more than once counts once, where it first appears. Prerender the first budget of the remaining links that have no query string, in order; the shop never prerenders its search and filter pages, the only ones with a query string. Prefetch every other remaining link, in order: those with a query string, and those past the budget. budget is a whole number, 0 or more.
Return the object as the HTML Standard's speculation rules define it: a key per action, "prerender" and "prefetch", each a list holding one list rule, { urls, eagerness: "moderate" }. "moderate" makes Chrome wait for a sign of interest, such as the pointer resting on the link, instead of starting at once, which is a list rule's default. Leave out an action with no URLs, and return {} when there's nothing to speculate on.
pickSpeculationRules(
[
{ href: "/teas/mint", sameOrigin: true, hasQuery: false, isLogout: false },
{ href: "/search?q=green", sameOrigin: true, hasQuery: true, isLogout: false },
{ href: "/teas/oolong", sameOrigin: true, hasQuery: false, isLogout: false },
{ href: "https://maps.example/leaf-and-kettle", sameOrigin: false, hasQuery: false, isLogout: false },
{ href: "/account/sign-out", sameOrigin: true, hasQuery: false, isLogout: true },
{ href: "/teas/rooibos", sameOrigin: true, hasQuery: false, isLogout: false },
],
2,
);
// {
// prerender: [{ urls: ["/teas/mint", "/teas/oolong"], eagerness: "moderate" }],
// prefetch: [{ urls: ["/search?q=green", "/teas/rooibos"], eagerness: "moderate" }],
// }
solution.js// The shop's pages call this with their links, most likely next first, and put
// JSON.stringify(pickSpeculationRules(links, 2)) in a <script type="speculationrules">.
function pickSpeculationRules(links, budget) {
// Today it prerenders every link at once: too many pages, and some it must never touch.
return { prerender: [{ urls: links.map((link) => link.href) }] };
}
- prerenders the most likely plain links within the budget and prefetches the rest, in order
- never speculates on another origin or on the sign-out link, and they don't use up the budget
- a link with a query string is only prefetched, however likely it is
- a link listed twice is speculated once, where it first appears
- a budget of 0 prerenders nothing, and an action with no URLs is left out
- nothing to speculate on gives an empty object
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…