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

@kimyvgy/nuxt-instantclick

v1.1.0

Published

Prefetch page's data on link hover for Nuxt.js

Downloads

45

Readme

@kimyvgy/nuxt-instantclick

NPM version

Prefetch page's data on link hover for Nuxt.js. (Instantclick port for Nuxt).

How it works

  1. User hovers on the link
  2. Parse the link and prefetch Json API to prepare data (if prefetchable)
  3. User click on a link, change event will fire (url, response data are included)
  4. Your callback hooked by change event will dispatch => you can set data into Vuex store, and render route as normally.

Install

You can install with NPM/Yarn:

# yarn
yarn add @kimyvgy/nuxt-instantclick

# or npm
npm install @kimyvgy/nuxt-instantclick

Usage

  1. Create a nuxt plugin for the cliend-side only - ./plugins/instantclick.client.js then activate it in nuxt.config.js file:
// ...

plugins: [
    '@/plugins/instantclick.client.js',
    // ...
],
  1. Init Nuxt Instantclick, in @/plugins/instantclick.client.js:
import InstantClick from '@kimyvgy/nuxt-instantclick'

export default ({ store, app: { router } }) => {
    const options = {
        //fetch options 
        options: {},
        
        // delay time before preloading page data
        delayBeforePreload: 0, // 0 ms

        // Time-to-live of preloaded data
        cacheTTL: 5 * 60 * 1000, // 5 min

        isPreloadable: (url) => {
            // please return `true` if the URL is preloadable.
            // nuxt-instantclick will have no action if returning `false`.
            const postPageReg = /\/posts\/.+$/
            return postPageReg.test(url)
        },

        transformURL: (url) => {
            // return URL string that will be used to preload page data.
            const slug = url.match(/([^/-]+)$/)[1]
            return `/api/p/${hashId}`
        },
    }


    InstantClick.init(options)

    // hooked by events
    InstantClick.on('change', async (url, { body, fetchedAt }) => {
        const expired = new Date() - fetchedAt > options.cacheTTL

        if (expired) {
            router.push(removeHostname(url))
            return
        }

        // Apply data then navigate to page.
        store.dispatch('save_preloaded_data', body)
        router.push(removeHostname(url))
    })

    InstantClick.on('exit', (url) => {
        router.push(removeHostname(url))
    })
}

function removeHostname (url) {
    return url.replace(/^https?:\/\/[^\/]+/, '');
}
  1. Add logic to prevent fetch new data in asyncData/fetch method if preloaded data is not expired. Example:
async asyncData({ route }) {
    if (process.client && InstantClick.hasValidCache(route.fullPath)) {
        return { data: InstantClick.getData(window.location.fullPath) }
    } else {
        const data = await fetch('/api/...').then(...)
        return { data }
    }
}

Events

  • preload: start preload URL
  • received: data received
  • change: user clicked on the link, please change page with preloaded data.
  • exit: error when processing click behavior: fetching error / data expired...