Skip to content

fix: scroll to top on Link navigation with sticky headers#94093

Open
AliMahmoudDev wants to merge 2 commits into
vercel:canaryfrom
AliMahmoudDev:fix/link-scroll-sticky-header
Open

fix: scroll to top on Link navigation with sticky headers#94093
AliMahmoudDev wants to merge 2 commits into
vercel:canaryfrom
AliMahmoudDev:fix/link-scroll-sticky-header

Conversation

@AliMahmoudDev
Copy link
Copy Markdown

Fixes #64441

When a page has a position:sticky header and the scroll position is below zero but less than the header height, clicking a Link to navigate to another page does not scroll to the top.

Root cause: The topOfElementInViewport() early exit check returns true when the content element is visible, even with a non-zero scroll position. With sticky headers, the element rect.top can be positive at small scroll offsets.

Fix: Add an additional check that htmlElement.scrollTop === 0 before the early exit. This ensures scrolling always happens unless the page is truly at the top.

…rule

Fixes vercel#53473

The no-html-link-for-pages rule was hardcoded to only match .js, .jsx,
.ts, .tsx file extensions. When users configure custom pageExtensions
in next.config.js (e.g. ['page.tsx', 'page.ts']), the rule would
produce false positives/negatives.

Changes:
- Added pageExtensions support via eslint settings.next.pageExtensions
- Modified parseUrlForPages and parseUrlForAppDir to accept custom
  extension regex patterns
- Updated getUrlFromPagesDirectories and getUrlFromAppDirectory
  signatures to pass through extension regexes
- Removed hardcoded TODO comments about page extensions support

Usage in .eslintrc:
  settings: {
    next: {
      pageExtensions: ['page.tsx', 'page.ts']
    }
  }
…64441)

When a page has a sticky header and the scroll position is less than
the header height, navigating via <Link> would not scroll to the top.
This happened because topOfElementInViewport() only checked if the
element's rect.top was within [0, viewportHeight], which returned true
even when the document was not scrolled to position 0 (the sticky
header's offset made the element appear to be in the viewport).

The fix adds an additional check for htmlElement.scrollTop === 0 before
exiting early. If the document is scrolled at all, we now always proceed
with the scroll-to-top logic, ensuring pages with sticky headers scroll
to top on navigation as expected.

Applies to both InnerScrollAndFocusHandlerOld (class component) and
InnerScrollHandlerNew (hook-based component with Fragment refs).
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.

<Link> not scrolling top sticky header

1 participant