Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readPerformance

Prerender next pages with speculation rules

Use a moderate speculation rule on a likely next link, then check whether Chrome activated the prerendered page.

When readers follow a guide’s Next link, they wait for another document to load. Use a moderate speculation rule to let Chrome start rendering that page when the pointer pauses over the link.

Mark the link and set moderate eagerness

Serve this document and a second page at /guide/next.html on the same origin. Use a local HTTP server for the example. The target can be any static page with a heading so you can tell when navigation completes.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Guide introduction</title>
<h1>Guide introduction</h1>
<a href="/guide/next.html" data-prerender>Next chapter</a>
<a href="/archive.html">Browse all chapters</a>
<script type="speculationrules">
{
  "prerender": [{
    "where": { "selector_matches": "a[data-prerender]" },
    "eagerness": "moderate"
  }]
}
</script>
</html>

On desktop Chrome, move the pointer onto Next chapter and leave it there. The link qualifies for speculation after about 200 ms of hover. Browse all chapters is outside the rule because the link has no data-prerender attribute.

moderate eagerness can also trigger on pointerdown if it comes first. The browser can fetch and render the next document in the background. If it has an eligible prerender when you follow the link, it can activate the prepared document.

diagram

Two ways to trigger moderate eagerness

The hover and quick-click paths give the same Next chapter link different amounts of preparation time.

Check that Chrome used the prerender

Open DevTools and inspect Speculative loads in the Application panel. Open the Speculations tab, find the URL for Next chapter and check the attempt status. Turn off Disable cache in the Network panel if you enabled it for another test. Seeing a request by itself tells you that Chrome did some work. The navigation may still load the document through the ordinary path.

After clicking, inspect performance.getEntriesByType('navigation')[0].activationStart on the destination. A positive value means Chrome activated a prerendered document. If the browser declines the speculation, the link still works as a normal navigation.

Pick pages that can render before a click

A prerender can run the destination’s scripts before the page becomes visible. Start with a read-only document such as the next chapter. If it records a page view, check document.prerendering and wait for prerenderingchange before recording the view. That keeps abandoned prerenders out of your page-view count.

Keep the rule scoped to links you expect readers to follow. If DevTools shows attempts for an entire menu, narrow the selector before changing the eagerness.