📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

next-scroll-restorer

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-scroll-restorer

![NPM Version](https://img.shields.io/npm/v/next-scroll-restorer) ![NPM Downloads](https://img.shields.io/npm/dm/next-scroll-restorer) ![NPM License](https://img.shields.io/npm/l/next-scroll-restorer) ![npm bundle size](https://img.shields.io/bundlephobia

0.9.9
latest
Source
npm
Version published
Weekly downloads
574
3.99%
Maintainers
0
Weekly downloads
 
Created
Source

NextJs Scroll Restorer

NPM Version NPM Downloads NPM License npm bundle size NPM Type Definitions

next-scroll-restorer handles scroll restoration for Next.js apps built with app directory. Fixed bugs that Next.js team ignored.

Important! This component works only for application written with Next.js 'app' directory.

Install

  • npm install next-scroll-restorer for NPM
  • pnpm add next-scroll-restorer for pnpm
  • yarn add next-scroll-restorer for Yarn

Key features

Presequences

Before you start, keep in mind following rules.

/** @type {import('next').NextConfig} */
const nextConfig = {
    experimental:{
        //Only For Next.js versions prior to 14.1.0 because it is enabled by default since version 14.1.0 
        windowHistorySupport:true 
    },
}
module.exports = nextConfig

Usage

Step 1

Create component named ClientSideScrollRestorer in your src directory with useScrollRestorer hook and "use client" directive to prevent server errors.

src/ClientSideScrollRestorer.tsx

"use client"
import {useScrollRestorer} from 'next-scroll-restorer';
const ClientSideScrollRestorer = () => {
    useScrollRestorer()
    return <></>
}
export default ClientSideScrollRestorer

Step 2

Import component created in a previous step to your root layout file (layout.tsx). Wrap it wih React <Suspense/> to avoid possible client-side deopting for entire page.

app/layout.tsx

import ClientSideScrollRestorer from '../src/ClientSideScrollRestorer'
import {ReactNode, Suspense} from "react";

type Props = {
    children: ReactNode
}
const RootLayout = ({children}) => {
    return (
        <html lang="uk">
        <body>{children}</body>
            <Suspense>
              <ClientSideScrollRestorer/>
            </Suspense>
        </html>
    )
}

export default RootLayout

It can be any nesting layout shared by group of routes in case you do not want to enable scroll restoration for the whole application.

FAQs

Package last updated on 03 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts