-
Notifications
You must be signed in to change notification settings - Fork 10
Fix PJAX scroll position restoration on back/forward navigation #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The issue was that the pjax library's built-in scroll restoration happened BEFORE the layout adjustments in pjax:complete handler, causing the visual scroll position to shift after layout changes. This fix: 1. Disables built-in scroll restoration (scrollRestoration: false) 2. Disables auto scroll to top (scrollTo: false) 3. Manually saves scroll position before navigation (pjax:send) 4. Restores scroll position AFTER all layout adjustments (pjax:complete) 5. Only restores scroll on back/forward navigation (popstate) 6. Removes redundant popstate event listener Co-authored-by: Shiroiame-Kusu <42903464+Shiroiame-Kusu@users.noreply.github.com>
…ndition handling 1. Added 'let' declaration for 'selectors' variable to fix implicit global 2. Replaced boolean flag with navigation ID tracking system: - navigationId: increments on each pjax:send - popstateExpectedId: set to navigationId + 1 on popstate - Only restores scroll when IDs match, preventing race conditions Co-authored-by: Shiroiame-Kusu <42903464+Shiroiame-Kusu@users.noreply.github.com>
Co-authored-by: Shiroiame-Kusu <42903464+Shiroiame-Kusu@users.noreply.github.com>
|
@copilot you delete line 134-140: window.addEventListener('popstate', (e) => { |
Investigated the pjax library's popstate handling. When back/forward navigation occurs:
The removed listener was calling |
|
@copilot Currently, all pages share the same Y-axis position/scroll progress, which is obviously not what we want. What we need is that when entering a page or navigating back to a previous page, it should resume from the last viewed position. If the page has not been scrolled before, it should start from the top, rather than all pages sharing a single scroll progress. |
…rward - Forward navigation (clicking links): now scrolls to top - Back/forward navigation (popstate): restores saved scroll position - Each page maintains its own scroll position via sessionStorage - URLs with hash fragments preserve scroll behavior Co-authored-by: Shiroiame-Kusu <42903464+Shiroiame-Kusu@users.noreply.github.com>
Fixed in fc44048. The issue was that Now:
|
Summary
This fix addresses the issue where page scroll position was abnormal when navigating back with PJAX enabled.
Behavior
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.