Technology 6 min read

Back/Forward Cache (bfcache): How Browsers Speed Up Navigation and How to Test It

A practical guide to understanding back/forward cache (bfcache), how it differs from HTTP cache, common blocking patterns to avoid, and how to test page restoration in Chrome DevTools.

Ilustrasi browser dengan tombol kembali dan maju, halaman web pulih instan lewat back-forward cache

Ever pressed the Back button in your browser and had the previous page appear instantly, complete with your scroll position and form data intact? One technology that makes this experience possible is back/forward cache, or bfcache. This feature preserves a page’s state when the user navigates away, allowing the browser to restore it without rebuilding the document from scratch.

For web developers, bfcache is more than just a speed trick. It also affects how applications manage sessions, connections, analytics, and data that may change while the page is hidden. Understanding how it works helps improve user experience without sacrificing information accuracy.

What Is Back/Forward Cache?

Bfcache is a browser mechanism that stores a complete snapshot of a page in memory. This snapshot can include the DOM, JavaScript state, scroll position, and various UI states. When the user presses Back or Forward, the browser can reactivate this snapshot rather than re-requesting the document and running through the entire loading process.

Browsers still determine which pages qualify for storage and how long snapshots are retained. Because of this, bfcache is not a guarantee that every navigation will be instant. Pages may fail to qualify, or snapshots may be discarded when memory resources are needed.

Bfcache Is Different from HTTP Cache

HTTP cache typically stores network responses such as HTML, CSS, JavaScript, or images. When a page is reopened, the browser still creates a new document, parses HTML, and executes scripts, even if some assets are fetched from local cache.

In contrast, bfcache preserves the state of an already-running page. This is why the Back button can feel significantly faster and the user’s position remains intact. Enabling HTTP cache does not automatically make a page eligible for bfcache; the two have different purposes and rules.

Why Bfcache Matters for User Experience

Back navigation frequently occurs when reading articles, comparing products, filling out forms, or moving between application pages. If the page must reload, users may lose their scroll position, wait for network requests, or see the interface flicker. Recovery from bfcache can reduce these interruptions and preserve the context of their work.

The benefit is most noticeable on pages with heavy initial processing. However, developers still need to ensure that stale data is refreshed when the page becomes active again. Speed helps little if users see account balances, order statuses, or session indicators that are no longer accurate.

Common Causes of Bfcache Ineligibility

One legacy pattern that often blocks bfcache is the use of the unload event. This event was previously used to clean up resources when leaving a page, but its behavior is unreliable across devices and can disqualify pages from bfcache. For appropriate cleanup tasks, use pagehide or visibilitychange and test their behavior in target browsers.

The beforeunload listener should also not be attached without necessity. If a warning is only needed when a form has unsaved changes, attach the listener when the form actually changes and remove it after data is saved.

Other factors can include active connections or operations, use of certain APIs, opener window references, and browser security or privacy policies. Eligibility rules vary by browser and version. Do not remove security controls blindly just to chase performance scores; identify specific causes and choose fixes that do not reduce data protection.

Use pageshow and pagehide Correctly

Because restored pages do not always undergo fresh loading, application code should not rely on the load event for every visit. The pageshow event fires on initial load as well as when a page is restored. The event.persisted property can help distinguish bfcache restoration from fresh loading.

window.addEventListener('pageshow', (event) => {
  if (event.persisted) {
    // Refresh only data that may have become stale.
    refreshSessionStatus();
    refreshOrderSummary();
  }
});

window.addEventListener('pagehide', () => {
  // Save temporary state or pause work
  // that genuinely needs to stop when the page is left.
});

This example is illustrative: adapt the refresh functions to your actual application. Avoid reloading the entire page without reason, as this would defeat the benefits of bfcache. For applications that open WebSocket connections or use IndexedDB, design pause and reconnection processes carefully to avoid creating duplicate connections after restoration.

How to Test Bfcache in Chrome

  1. Open the page you want to test in Chrome and launch DevTools.
  2. Go to the Application panel, then find the Back/forward cache section under Background services.
  3. Run the provided test, then follow the instructions to navigate away and return.
  4. Check whether the page can be restored and read any reasons reported if the browser rejects it.

Test more than one route and scenario, including pages with forms, login states, iframes, and real-time connections. Local testing does not always represent all user conditions. Monitor field data where available and repeat checks after code changes or third-party dependency updates.

Watch for Sensitive Data and Analytics

Pages returning from bfcache may contain state created before the user switched tabs or before the session changed. For account pages, re-verify authentication status and refresh sensitive data as needed. Do not store secrets in UI state with the assumption that pages will always reload.

Analytics also need attention. If visit logging depends only on initial load events, restored navigations may not be recorded as expected. Use page lifecycle events thoughtfully and avoid counting one visit twice.

Practical Optimization Checklist

  • Avoid unnecessary unload listeners.
  • Limit beforeunload to when there are genuinely unsaved changes.
  • Use pageshow to check for potentially stale data.
  • Pause or reconnect connections safely when pages are left and restored.
  • Test multiple pages with DevTools and address failure reasons that can be handled.
  • Maintain required privacy and security policies; do not blindly modify sensitive headers just for cache eligibility.

Conclusion

Back/forward cache helps browsers restore pages quickly when users press Back or Forward. To realize its benefits, developers need to avoid patterns that block bfcache, handle page lifecycle events, and refresh data that may have changed. Start with DevTools testing, fix measurable causes, then ensure optimizations maintain application security and consistency.

Technical references: bfcache guide in Chrome DevTools and MDN documentation on the pageshow event and pagehide event.

Bagikan artikel ini

No comments yet

    Leave a comment

    Your email address will not be published. Required fields are marked *