Skip to content

fix: scroll to top when changing routes - #21

Draft
danieldanielecki wants to merge 2 commits into
masterfrom
cursor/scroll-to-top-route-change-5393
Draft

danieldanielecki wants to merge 2 commits into
masterfrom
cursor/scroll-to-top-route-change-5393

Conversation

@danieldanielecki

@danieldanielecki danieldanielecki commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #15: after SPA navigation, the viewport stayed wherever the previous page was scrolled (often the bottom on long pages / mobile).

Cause: The app bootstraps with standalone provideRouter(routes) in app.config.ts. scrollPositionRestoration lived only on unused RoutingModule, so Angular never reset scroll on route change. html, body { height: 100%; overflow-x: hidden } can also make body the scroller, so window restoration alone is not enough.

Fix:

  • withInMemoryScrolling({ scrollPositionRestoration: 'top' }) on the live standalone router
  • Align RoutingModule to 'top'
  • On NavigationEnd, reset window, documentElement, and body scroll (browser only)

Test plan

  • Unit: AppComponent scrolls to top on navigation (ng test include app.component.spec.ts + app-routing.module.spec.ts: 6 passed)
  • Unit: RoutingModule uses scrollPositionRestoration: 'top'
  • Playwright: scroll down on /, navigate to /about-us, viewport is at top (e2e/scroll.spec.ts passed)
Open in Web Open in Cursor 

The standalone router never applied RoutingModule's scroll restoration, so SPA
navigations kept the previous viewport offset. Restore to top on NavigationEnd
and align router config with that behavior.

Co-authored-by: ✅ Daniel Danielecki <danieldanielecki@users.noreply.github.com>
Standalone withInMemoryScrolling does not populate ROUTER_CONFIGURATION, so
drop that assertion and drive the AppComponent test through a real navigation.

Co-authored-by: ✅ Daniel Danielecki <danieldanielecki@users.noreply.github.com>
@cursor
cursor Bot deployed to Preview October 21, 2026 11:47 Active
@cursor
cursor Bot deployed to Production October 21, 2026 11:47 Active

This branch was successfully deployed

2 active deployments
Preview — a7f5a90d Deployed Oct 21, 2026 by cursor[bot] via Build (staging) #84
Production — a7f5a90d Deployed Oct 21, 2026 by cursor[bot] via Build (production) #84
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scroll To Top while changing routes

2 participants