-
Notifications
You must be signed in to change notification settings - Fork 26k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
[NEXT-1147] Scroll position is reset when search params are updated #49087
Comments
Looking forward to this getting fixed :-) |
I experience the same issue... I thought it was a bug in my code. 😆 |
…ng issue with the latest useRouter. See: vercel/next.js#49087
I'm experiencing the same bug. The website scrolls to the top every time the URL parameters change. I saw @timneutkens's tweet on the topic. 2023-05-27.09-58-58.mp4 |
I'm having the same problem... My nextJS version is It seems to be fine when I use English, but when I use Korean, it keeps reloading. |
@adrianhajdin I'm having the same issue... |
Facing the same issue... First I would like to mention that I think this is a correct behavior by default, there nothing wrong with it; however, it would be awesome to have the option to persist scroll position indeed. I came up with a quick workaround to solve this temporarily until there's an official fix: 'use client'
import { useCallback, useEffect } from 'react'
import { useRouter, usePathname, useSearchParams } from 'next/navigation'
export default function ExampleClientComponent(): JSX.Element {
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()
useEffect(() => {
// Retrieve scrollY value from localStorage after routing
const persistentScroll = localStorage.getItem('persistentScroll')
if (persistentScroll === null) return
// Restore the window's scroll position
window.scrollTo({ top: Number(persistentScroll) })
// Remove scrollY from localStorage after restoring the scroll position
// This hook will run before and after routing happens so this check is
// here to make we don't delete scrollY before routing
if (Number(persistentScroll) === window.scrollY)
localStorage.removeItem('persistentScroll')
}, [searchParams])
const setSearchParam = useCallback(
(key: string, value: string) => {
const currentParams = searchParams.toString()
const params = new URLSearchParams(currentParams)
params.set(key, value)
// If search params are still the same there's no need to do anything
if (currentParams === params.toString()) return
// Save current scrollY value to localStorage before pushing the new route
localStorage.setItem('persistentScroll', window.scrollY.toString())
router.push(`${pathname}?${params.toString()}`)
},
[searchParams, pathname, router],
)
return (
<button
onClick={() => {
setSearchParam('exampleParam', '9999')
}}
>
Update search params
</button>
)
} Depending on your application's logic, you can package this into a React hook and reuse it in all components that should update the search params. |
@adrianhajdin I got it working by using experimental: {
appDir: true,
}, in At least this works fine while keeping server side rendering until they fix the bug. |
@GBrachetta it won't work when deployed i guess. So, i would suggest people to use that method of restoring the scroll position as shown by @YoussefKababe , that will work in the deployed version as well, though it seems to make a page reload or smthng, but as @GBrachetta said, atleast it's better than converting the component to client side rendering, till the bug's fixed. |
What i have done in the project I followed from you is that I've just put id="searchbar" in the form then append #searchbar in |
Same issue in |
I'm seeing lots of router fixes in Anyone know if a fix for this is in the works anytime soon? |
I'm encountering a similar issue where the webpage consistently jumps to the top whenever there is a modification to the URL parameters. |
same issue :( |
Same issue. |
Same issue :( |
Same issue. |
1 similar comment
Same issue. |
<Link href="/#hashid" scroll={false}>
Scroll to specific id.
</Link> This code does not work with |
same issue |
This issue is really frustrating. I don't want to downgrade my next.js to 13.2.4 just to resolve this bug, or else I will get other unresolved problems. |
Same issue, really annoying and a bit blocking.. 🙄 |
Please provide a reproduction. |
|
@lmatteis I'm guessing you're referring to the reproduction for the scroll being reset when searchParams change. That one I can easily reproduce as of right now it's expected behavior as I shared in #49087 (comment). The comment linked to does not have |
oh I thought +info: I'm using use-query-params with a Next.js adapter. I was able to fix this bug only by this workaround. |
@adrianhajdin In package.json:
|
Thanks, that works! |
Adding {scroll: false} fixed it for me, with Next 13.4.12 release.❤️ In |
@momenqudaih @ShivangKakkar @rupin27 Thanks guys, also worked here... |
Got some docs going here, thanks folks 🙏 Edit: wrong button, disregard |
This comment was marked as off-topic.
This comment was marked as off-topic.
next.js 버전 업데이트하니까 됨,, 뭐냐? vercel/next.js#49087 (comment). 심지어 useParams 리턴값도 바뀜..₩
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
* body에 overscroll-none 적용 * SelectionList에서 스크롤되는 문제 수정 next.js 버전 업데이트하니까 됨,, 뭐냐? vercel/next.js#49087 (comment). 심지어 useParams 리턴값도 바뀜..₩ * 찾아오는 길 내용 추가 * 학부 수시 모집 데이터 추가 * 링크 수정 * 동아리 소개 문단 조정 * 동아리 소개 디자인 QA 반영 * 입학 관련 데이터 추가 * 디자인 QA 반영 * 디자인 QA 반영 * faculty와 staff 폴더 통합 * facultyList 데이터 추가 * EmiritusFaculty 추가 * EmiritusFaculty 타입 추가 * 교수진 상세 정보 추가 * EmiritusFaculty 관련 타입 정리 * SimpleEmeritusFaculty 데이터 추가 * 역대교수 관련 row 처리 * EmeritusFaculty page 레이아웃 수정 * 역대교수 상세정보 추가 * 행정직원 관련 타입 정리 * staffList 데이터 추가 * staff 정보 추가 * overscroll-none 제거 * TCL 정보 추가 * ResearchLabInfo 관련 타입 정리 * researchLabInfos 추가 * StaffMemberPage에 mock api 적용, server component로 변경 * researchLabInfos에 professors 추가 * faculties labId 추가 * description 제외 researchLabs 정보 추가 * 연구실 상세 데이터 구조 개발 * description txt파일들 추가 * 연구실 상세 정보 진행중 * 연구실 상세 정보 추가 * usePost 삭제 * 리뷰 반영 * 모달 로직 수정 * 다크 모드 적용
This comment was marked as spam.
This comment was marked as spam.
I also have same issue, unfortunately in a project we have to switch from server to client side |
|
version:
documentation: https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#disabling-scroll-restoration thanks @timneutkens for the solution |
@ShivangKakkar thanks too In router.push(newPathName, {scroll: false}); |
you can set the scroll options and this will prevent the scrolling when trying to change your searchParams router.push(newPathname, { scroll: false}) |
I'm going to lock this issue as it's already part of our issue tracker and the same solution keeps being repeated which causes notifications to everyone in this thread. |
Hi everyone! I will be moving this to Happy 2024! |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Verify canary release
Provide environment information
Next.js `13.3.5-canary.2`
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/modest-gould-h4nlvd?file=%2Fapp%2Fpage.tsx&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A7%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A7%7D%5D
To Reproduce
Describe the Bug
Context
In Next <= 13.2.4, updating search params was working as intended : client state was kept, and scroll position was kept too.
In Next 13.2.5, a regression made the client components unmount and remount when search params were updated.
@feedthejim fixed that unmounting in #49047 (it's testable on
13.3.5-canary.2
), but there is still an issue now: scroll position is lost on search params updates.Problem: persisting state in search params is very important with the App router - that's a clean way for client components to request updated data from the RSC.
Reproduction Codesandbox
Here's a codesandbox reproducing the bug:
https://codesandbox.io/p/sandbox/modest-gould-h4nlvd?file=%2Fapp%2Fpage.tsx&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A7%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A7%7D%5D
In video
CleanShot.2023-05-02.at.14.18.36-converted.mp4
A real-world example of the problem
Previously, the "name for the fund", kept in client state, was reset when updating the sliders. It's now better, but the scroll position reset is still awkward.
CleanShot.2023-05-02.at.14.31.38.mp4
Expected Behavior
When updating search params, the scroll position should be preserved.
Which browser are you using? (if relevant)
Latest stable Google Chrome
How are you deploying your application? (if relevant)
No response
NEXT-1147
The text was updated successfully, but these errors were encountered: