Skip to content

fix: scroll to top when changing routes - #21

Merged
danieldanielecki merged 3 commits into
masterfrom
cursor/scroll-to-top-route-change-5393
Nov 17, 2026
Merged

danieldanielecki merged 3 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

@danieldanielecki danieldanielecki left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's test in production yaay

@danieldanielecki
danieldanielecki marked this pull request as ready for review November 17, 2026 07:56

@danieldanielecki danieldanielecki left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's test in production

@danieldanielecki danieldanielecki left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's test in prod

@danieldanielecki
danieldanielecki merged commit 9686184 into master Nov 17, 2026
9 of 10 checks passed
@danieldanielecki
danieldanielecki deleted the cursor/scroll-to-top-route-change-5393 branch November 17, 2026 08:28

This branch was successfully deployed

2 active deployments
Preview — d275c340 Deployed Nov 17, 2026 by danieldanielecki via Build (staging) #85
Production — d275c340 Deployed Nov 17, 2026 by danieldanielecki via Build (production) #85
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