Skip to content

History API State Loss on Reload

Quick summary

You stored data in pushState — after a reload, history.state is null and your app loses the data.

Why this happens

  • Storing essential app state only in history.state

  • Not serializing state to the URL or localStorage as a backup

  • Assuming pushState persists across full page reloads

Minimal example

✗ Broken code
history.pushState({ filter: 'active' }, '', '/items');
// after reload: history.state is null — filter lost
✓ Fixed code
history.pushState({ filter: 'active' }, '', '/items?filter=active');
// on load: const params = new URLSearchParams(location.search);
const filter = params.get('filter') || 'all';

State in history.state is lost on reload. Putting the filter in the URL query string lets the app reconstruct it from location.search on every load.

How to diagnose

  • Reload the page and check history.state — it is null if state was only in memory

  • Inspect the URL to see if the state is encoded there

  • Check whether the app relies on history.state immediately on load

How to fix

  • Encode essential state in the URL (query params or path) so it survives reload

  • Mirror state to localStorage or sessionStorage as a backup

  • Reconstruct state from the URL on page load instead of relying on history.state

How to prevent

  • Treat history.state as ephemeral — always have a URL-based fallback

  • Use a router that syncs state to the URL automatically

Related Resources

Related Glossary

Related Lessons

  • HTTP

    Learn about History API.

← Back to language