Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Matteo Mazzarolo
Matteo Mazzarolo

Posted on • Originally published atmmazzarolo.com on

     

Scroll restoration in Next.js

While working onRemotebear, I recently discovered that Next.js doesn’t handle scroll restoration automatically. So, for example, if you navigate back to a previous page of your app, Next.js will always show it scrolled to the top, regardless of the scroll position it had when you left it.

Experimental scroll restoration flag

Luckily, Next.js hasan experimentalscrollRestoration flag that you can enable to automatically restore the scroll positions.

You can enable it in yournext.config.js file this way:

module.exports={experimental:{scrollRestoration:true,},};
Enter fullscreen modeExit fullscreen mode

next-router-scroll

For my use case, this solution is working fine, but there are some cases where you need to take control of how your application scroll is handled; namely, you may want to restore scroll when the user is navigating within your application pages, but you need to do extra work before or after the page has changed, either by using some sort of page transition or any other feature.

In these cases, I’d suggest you give@moxy/next-router-scroll a try: This package is built on top ofscroll-behavior and it actively listens to Next.js router events, writing the scroll values associated with the current location in the Session Storage and reading these values wheneverupdateScroll() is called.

Completely disabling scroll restoration

There’s one inconsistency I noticed around not making scroll restoration work automatically in Next.js: by default, scroll restoration doesn’t work when the navigation logic is being handled by JavaScript, but it works fine when it’s handled by the browser (e.g.: on a full-refresh or while navigating whit JavaScript disabled).

So, in the rare occasions where you want to fully disable scroll restoration, remember to add this snippet to the<head> of your project:

importHeadfrom"next/head";exportdefaultfunctionScrollRestorationDisabler(){return(<Head>{/* Tell the browser to never restore the scroll position on load */}<scriptdangerouslySetInnerHTML={{__html:`history.scrollRestoration = "manual"`,}}/></Head>);}
Enter fullscreen modeExit fullscreen mode

Top comments(6)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
hayyaun profile image
Hayyan Hami
Web developer. Animator. Dreamer. ✨ As a passionate web developer, I weave digital magic into every line of code. 🪄
  • Email
  • Location
    Rasht, Iran
  • Education
    CE at SUT
  • Work
    Web Development
  • Joined

Thank you!At first it was working only with hashtag navigation, but then I realized that my main scrolling element is not thebody tag. Now it's all working properly.

CollapseExpand
 
endrureza profile image
Endru Reza
New stuff refreshes mind
  • Location
    Indonesia
  • Education
    Master
  • Work
    Sr. Backend Developer at Fabelio
  • Joined

hey nice find dude. thanks, i need this one too

CollapseExpand
 
ongkay profile image
ongkay
Hi, I am Ongkay fullstack dev from bali indonesian
  • Education
    otodidak
  • Work
    freelancer
  • Joined

how use it in nextjs 13?

CollapseExpand
 
azizc0der_66 profile image
Azizxo'ja Saidrahmonov
  • Joined

+

CollapseExpand
 
sinanyilmaz profile image
Sinan Yilmaz
  • Work
    Software Engineer
  • Joined
• Edited on• Edited

This ist actually not true in my experience. The commit you shared above is also over 1 year old. On Static pages the scroll restoration works without any configuration.

CollapseExpand
 
thanhtutzaw profile image
ThanHtutZaw
  • Joined

Your static pages mean Only HTML ,CSS and Javascript ?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

🍉
  • Location
    Italy
  • Joined

More fromMatteo Mazzarolo

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp