Skip to content

Scroll Position Unexpectedly Restored

Quick summary

After reload the page jumps to the old scroll position — your SPA intended to start at the top.

Why this happens

  • Default history.scrollRestoration is 'auto' which restores scroll

  • SPA reload expects top but the browser restores the cached position

  • Long content where restored scroll looks like a bug to the user

Minimal example

✗ Broken code
// default: history.scrollRestoration === 'auto'
// reload jumps to old scroll position unexpectedly
✓ Fixed code
if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual';
}
window.scrollTo(0, 0); // start at top on load

The browser restores scroll by default. Setting scrollRestoration to 'manual' gives the SPA control, and scrollTo(0,0) ensures it starts at the top.

How to diagnose

  • Check history.scrollRestoration — if 'auto', the browser restores scroll

  • Reload and observe whether the page jumps to a non-top position

  • Log window.scrollY on load to see the restored value

How to fix

  • Set history.scrollRestoration = 'manual' and control scroll yourself

  • Call window.scrollTo(0, 0) on load if you want to start at the top

  • Restore scroll only for routes where it makes sense

How to prevent

  • Set scrollRestoration to 'manual' in SPAs and handle scroll explicitly

  • Decide per-route whether scroll restoration is desired

Related Resources

Related Glossary

Related Lessons

  • DOM

    Learn about scroll.

← Back to language