# Back/Forward Cache (bfcache) vs Speculation Rules & Prerendering

- **Canonical URL:** https://frontend.rocks/vs/bfcache-navigation-vs-speculation-rules-prerender/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** bfcache accelerates Back/Forward history traversal; Speculation Rules accelerates forward navigations.

## Comparison Matrix

| Dimension | Back/Forward Cache (bfcache) | Speculation Rules & Prerendering |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Experimental |
| **Domain** | Network & Performance | Network & Performance |
| **Mental Model** | In-memory cache snapshotting the full JS heap and DOM tree on outbound navigation to restore the page instantaneously on Back/Forward. | Declarative JSON rules that prefetch or fully prerender predicted next navigations in a hidden browsing context, delivering near-instant 0 ms LCP on click. |
| **Prerequisites** | None | None |
| **When Not to Use** | When not to use bfcache: On banking or authenticated views displaying sensitive data after logout, enforce Cache-Control: no-store or clear sensitive DOM on pagehide. | When not to use prerender: Avoid prerendering URLs with server-side side effects (logout links, add-to-cart GET endpoints) or pages heavy on WebGL/video memory. |

## Back/Forward Cache (bfcache) — Under the Hood
Unlike the HTTP disk cache (which only saves raw response bytes and forces a full rebuild), bfcache acts like a laptop's hibernate-to-RAM: the entire DOM, [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline) state, and V8 heap are snapshotted intact and thawed on return.

1. On outbound navigation, the engine checks for blockers (`unload` listeners, open WebSockets/IndexedDB transactions, `Cache-Control: no-store`).
2. If eligible, the engine fires `pagehide` with `event.persisted === true` and freezes all [Event Loop](/concepts/event-loop-tasks-microtasks) task queues and timers.
3. On Back/Forward navigation, the memory snapshot is thawed and `pageshow` (`event.persisted === true`) fires without re-firing `DOMContentLoaded` or `load`.

```js
window.addEventListener('pageshow', (event) => {
  if (event.persisted) {
    // Page was thawed from bfcache: refresh volatile auth or cart state
    refreshSessionBanner();
  }
});
```

## Speculation Rules & Prerendering — Under the Hood
While [bfcache](/concepts/bfcache-navigation) handles instant Back/Forward navigation and the [Preload Scanner](/concepts/preload-scanner-hints) handles subresources on the current page, Speculation Rules tackle forward clicks. Think of it as staging the next theater set behind a closed curtain before the scene change.

1. A `<script type="speculationrules">` block (or `Speculation-Rules` HTTP header) defines `prefetch` or `prerender` rules using URL lists or CSS `where` selectors.
2. When an `eagerness` trigger fires (`moderate` on 200 ms hover, `conservative` on pointerdown), the engine allocates an isolated hidden browsing context.
3. The hidden document parses HTML, runs JS, and paints frames, while blocking intrusive APIs (camera, audio, `alert`) until activation.
4. On click, `document.prerendering` flips from `true` to `false` and `prerenderingchange` fires.

```html
<script type="speculationrules">
{
  "prerender": [{
    "where": { "href_matches": "/docs/*" },
    "eagerness": "moderate"
  }]
}
</script>
```

## Interactive Labs & Dissections
- [Back/Forward Cache (bfcache) Lab](https://frontend.rocks/concepts/bfcache-navigation/#lab) ([Markdown](https://frontend.rocks/concepts/bfcache-navigation.md))
- [Speculation Rules & Prerendering Lab](https://frontend.rocks/concepts/speculation-rules-prerender/#lab) ([Markdown](https://frontend.rocks/concepts/speculation-rules-prerender.md))

## Related Architecture Autopsies
- [Autopsy #04: The Zombie Bundle After a Production Deployment](https://frontend.rocks/autopsies/zombie-bundle-after-deploy/) ([Markdown](https://frontend.rocks/autopsies/zombie-bundle-after-deploy.md)): Active tabs throw ChunkLoadError or corrupt local IndexedDB state after a production deploy.

## Primary Sources
- https://web.dev/articles/bfcache
- https://developer.mozilla.org/en-US/docs/Glossary/bfcache
- https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API
- https://developer.chrome.com/docs/web-platform/prerender-pages
- https://wicg.github.io/nav-speculation/speculation-rules.html
