npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-navigation-history

v0.3.0

Published

Navigation History Plugin for Next.js

Downloads

3,984

Readme

next-navigation-history

Navigation history plugin for Next.js

npm version License: MIT

This plugin is usefull for restoring state when users press "Back", "Foward" and "Reload".

Features

  • Restore a last state when going forward or back.
  • Restore a state when reloading.
  • Restore a last state when going forward or back after reloading.

Supported Vuersion

  • Next.js v12-13

Install

Using npm:

npm install next-navigation-history

Setup

import type { AppProps } from 'next/app'
import { withNavigationHistory } from 'next-navigation-history'

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

export default withNavigationHistory(MyApp, {
  ...
})

Options

maxHistoryLength

Sets the maximum length of hisotries that can hold data.

When this option is not set, it depends on a max history length of a browser.

Default: undefined

overrideScrollRestoration

Indicates whether this plugin override a default scroll restoration of the browser.

If you set this option to true, it manages a scroll restoration by using own saved position.

Default: true

scrollingElements

Indicates to which element the overrode behavior is applied.

If you set this option to a selecter, it applies the scrolling to the selector, in addition to the window.

Default: undefined

Usage

If you want to access backup data, you have to use a useNavigationHistory.

import { useNavigationHistory } from "next-navigation-history"

const SamplePage: NextPage = () => {
  const [value, setValue] = useState({})
  const [info, setInfo] = useState("")

  // If you just want to get a navigationHistory
  const nav = useNavigationHistory()

  // If you want to backup and restore state
  const nav = useNavigationHistory(() => {
    // Backup state
    return { value }
  })

  // NavigationHistory is not accessible on the server side
  useEffect(() => {
    // Restore backup state
    const state = nav.state
    if (state) {
      setValue(state.value)
    }

    // Get info value sent by the transition source.
    const info = nav.info
    if (info) {
      setInfo(info)
    }
  }, [])

  function next() {
    // Send info value to the transition destination.
    nav.back("send infomation!")
  }
}

API

NavigationHistory

type

A navigation type.

  • navigate: When a new page is navigated.
  • reload: When a page is reloaded.
  • push: When a history.push is called.
  • back: When a back navigation is occurred.
  • forward: When a forward navigation is occurred.

This method cannot be used on the server.

visited

If the navigation type is back, forward or reload, this property returns true.

page: number

A current page number (an integer beginning with 0).

This method cannot be used on the server.

state: object?

A backup state.

If you want to clear the backup state, you set undefined to this property.

This method always returns undefined on the server.

length: number

A history length.

This method cannot be used on the server.

getItem(page): HistoryItem?

You can get a location and data of the specified page number.

If you set 'overrideScrollRestoration' option to true, the item has scrollPositions property.

This method cannot use on the server.

getItems(): HistoryItem[]

You can get a list of item.

This method cannot be used on the server.

canGoBack: boolean / canGoForward: boolean

You can test if you can go back/forward.

This method cannot be used on the server.

findBackPage(location): number? / findForwardPage(location): number?

You can get a page number of the first match history, searching backward/forward in the continuous same site starting at the current page. If a history item is not found or is not in the continuous same site, this method will return undefined.

If the partial option sets true, it matches any subset of the location.

This method cannot be used on the server.

const page = nav.findBackPage({
    path: '/test'
    // hash: ...
    // query: ...
    // partial: true (default: false)
})
if (page != null) {
    // remove backup state
    nav.getItem(page).state = undefined

    // go back to the page in site.
    nav.goToPage(page)
}

push(url, info?)

This method is almost the same as router.push(url).

If you set info parameter, it passes info data (like a message) to the next page.

reload(info?)

This method is almost the same as window.location.reload().

If you set info parameter, it passes info data (like a message) to the reloaded current page.

back(info?)

This method is almost the same as window.history.back().

If you set info parameter, it passes info data (like a message) to the backwarded page.

forward(info?)

This method is almost the same as window.history.forward().

If you set info parameter, it passes info data (like a message) to the forwarded page.

goToPage(page, info?)

This method is almost the same as window.history.go(page - nav.page).

If you set info parameter, it passes info data (like a message) to the page.

HistoryItem

location: { path?, query?, hash? }

A location of this saved page.

state: object?

A backup state.

If you want to clear the backup state, you set undefined to this property.

scrollPositions: object

A saved scroll positions. A root window is obtained with 'window' key, the others by the selector.

License

MIT License

Copyright (c) Hidekatsu Izuno ([email protected])