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

nuxt-history-state

v0.6.3

Published

Nuxt.js module to backup or restore historical states

Downloads

776

Readme

History State Module for Nuxt.js

npm version License: MIT

Nuxt.js module to backup or restore historical states.

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. (optional)

Install

Using npm:

npm install nuxt-history-state

Setup

nuxt.config.js

module.exports = {
    // enable a module
    modules: [
        'nuxt-history-state'
    ],

    // set options (see below section)
    historyState: {
        maxHistoryLength: 50, // or any positive integer
        reloadable: false, // or true
        overrideDefaultScrollBehavior: true, // or false
        scrollingElements: '#scroll' // or any selector
    }
}

Options

maxHistoryLength

Sets the maximum length of hisotries that can hold data.

When this option is not set, it is unlimited.

Default: undefined (unlimited)

reloadable

Indicates whether this module works properly after reloading.

It uses HTML5 History API state by default. However It does not fully support reloading. This API does not work properly when goes back or forward after reloading.

If you set this option to true, it adds a parameter _p to url instead of using HTML5 History API state.

Default: false

overrideDefaultScrollBehavior

Indicates whether this module override a default scroll behavior of the router.

If you set this option to true, it manages a scroll behavior 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: null

Usage

Backup component data

If you want to backup data, you have to define a backupData lifecycle method. I recommend that you define it to page components only.

export default {
    backupData() {
        return this.$data;
    }
}

Restore component data

You can access to backup data through $historyState object of this or context.

export default {
    // Access to backup data in a asyncData or fetch method.
    async asyncData({ $historyState, $http }) {
        // Overwrite value on a new page only
        if ($historyState.action === 'navigate' || $historyState.action === 'push') {
            return await $http.$get(...);
        }
        return {};
    }

    // Access to backup data in lifecycle methods of the instance.
    data() {
        return this.$historyState.data || {
            ...
        };
    }
}

API

$historyState

action

A action type that caused a navigation.

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

By default this method returns basically 'navigate' on server. But many browsers send cache-control='maxage=0' when reloading. It heuristically returns 'reload' then.

If you set the reloadable option to true, it detects 'navigate' or 'reload' by using _p parameter.

page

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

By defalut this method always returns 0 on server. If you set the reloadable option to true, it returns the currect page number by using _p parameter.

data

A backup data.

This method always returns null on server.

length

A history length.

This method cannot use on server.

getItem(page)

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

This method cannot use on server.

getItems()

You can get a list of item.

This method cannot use on server.

findBackPosition(location, partial = false)

You can get the relative position of the first matched history, searching backward starting at the current page. If a history state is not found, this method will return null.

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

This method cannot use on server.

const delta = this.$historyState.findBackPosition({
    path: 'test'
    // hash: ...
    // query: ...
    // name: ...
    // params: ...
});
if (delta != null) {
    this.$router.go(delta);
}

clearItemData(page)

You can clear a data of the specified page number. And it returns the previous data.

This method cannot use on server.

License

MIT License

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