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

vue-router-webcache

v1.1.6

Published

Enable vue-router routing for google webcache(and same caches)

Downloads

2,996

Readme

Vue-router-webcache

npm GitHub Workflow Status npm GH Pages license

A set of helpers for vue-router to working with google webcache and similar

Table of Contents

Install

yarn add vue-router-webcache # npm i vue-router-webcache

Compatibility caches

From the box this package support https://webcache.googleusercontent.com/ and https://yandexwebcache.net caches for SPA/SSR. If you need more cache types, you always can reassign the cacheList by add cache to any of helpers as second arg.

import { isCacheUrl, defaultCacheUrls } from 'vue-router-webcache';

const additionalCacheUrls = [
  {
    hostname: 'localhost',
    pathname: '/search',
    getRealUrl: (url) => url,
  },
];

const isCache = isCacheUrl(window.location.href, [
  ...defaultCacheUrls,
  ...additionalCacheUrls,
]);

getRealUrl - is optional prop, to extract realUrl for SPA apps or for SSR without vuex-router-sync.

Example of cache

Compatibility Vue

Current package support Vue 2 (vue-router@3) and Vue 3 (vue-router@4).

Nuxt basic usage

  1. Install package
  2. Install @nuxtjs/router
  3. Add vue-router-webcache/nuxt and @nuxtjs/router to nuxt.config.js
  4. Add router.js to .gitignore
export default {
  buildModules: [
    'vue-router-webcache/nuxt',
    ['@nuxtjs/router', {
      keepDefaultRouter: true,
    }],
  ]
};

After first build, package create router.js in root of project nuxt-community/router-module#107

If you change generated router.js, don't forget remove first two lines

By default vue-router-webcache/nuxt use nuxt-vuex-router-sync to get real url

Example in test/fixtures/nuxt

Nuxt advanced usage

If you use custom router or you need more caches, you can customize options or use only helpers without module

export default {
  buildModules: [
    ['vue-router-webcache/nuxt', {
      cacheList: [{
        hostname: '',
        pathname: '',
        getRealUrl: (url) => url,
      }],
      replacePush: true,
      forceVuexRouterSync: false,
      urlGetter: () => '',
    }],
    ['@nuxtjs/router', {
      keepDefaultRouter: true,
    }],
  ]
};

cacheList - list of caches, checks work through URL

replacePush - default @nuxtjs/router replace router.push, you can disable it

urlGetter - function that return fullPath or current real url, serialized by Function.toString, exec in browser

forceVuexRouterSync - option to force add module nuxt-vuex-router-sync and use urlGetter for it. Can be enabled automatically if some of cache's in cacheList hasn't getRealUrl and you don't reassign urlGetter


Default vue-router-webcache/nuxt config

Full example in test/fixtures/nuxt-custom

If you don't change urlGetter, but add some cache without getRealUrl, vue-router-webcache/nuxt add nuxt-vuex-router-sync module to save realUrl in store.state

Vue-router usage

  1. Install package
  2. Create Router by helper
import createRouter from 'vue-router-webcache';

const router = createRouter(routerOptions);

How it work

  • Check if window.location.href is url of some caches
  • Change mode of router to abstract
  • Get real url of cached page
    • Nuxt, add nuxt-vuex-router-sync (or analogs) and get window.__NUXT__.state.route.fullPath
    • Vue, get url from window.location.query for example
  • After Router created, set current route
    • Nuxt, mock first router.resolve call patchNuxtRouter
    • Call router.replace(realUrl)

Api

createRouter

Helper that create vue-router instance with routerOptions, but change mode to abstract and call router.replace if page in caches list

import createRouter from 'vue-router-webcache';

const router = createRouter(routerOptions, [
  {
    hostname: '',
    pathname: '',
    getRealUrl: (url) => url,
  },
]);

routerOptions - simple vue-router options

additionalCacheList - caches that will be added to the defaults (optional)

defaultCacheUrls

Array of default cache urls

import { defaultCacheUrls } from 'vue-router-webcache';

getCache

Get cache by fullUrl

import { getCache } from 'vue-router-webcache';

const cache = getCache(window.location.href, [
  {
    hostname: '',
    pathname: '',
    getRealUrl: (url) => url,
  },
]);

fullUrl - url of checked page

cacheList - list of caches to check (optional)

isCacheUrl

Url checker

import { isCacheUrl } from 'vue-router-webcache';

const isCache = isCacheUrl(window.location.href, [
  {
    hostname: '',
    pathname: '',
  },
]);

fullUrl - url of checked page

cacheList - list of caches to check (optional)

getRealUrl

Helper trying to get realUrl from cached url (use getRealUrl functions from cacheList)

import { getRealUrl } from 'vue-router-webcache';

const realUrl = getRealUrl(window.location.href, [
  {
    hostname: '',
    pathname: '',
    getRealUrl: (url) => url,
  },
]);

fullUrl - url of checked page

cacheList - list of caches to check (optional)

getFullPath

Helper extract fullPath from realUrl and cut base(second argument)

import { getFullPath } from 'vue-router-webcache';

const fullPath = getFullPath('http://example.com/foo/bar', '/foo');
// /bar

fullUrl - url for extracting

base - base url of your app (default=/)

patchNuxtRouter

Router patcher for nuxt, mock first router.resolve

import { patchNuxtRouter } from 'vue-router-webcache';

...

patchNuxtRouter(router, realUrl);

router - instance of vue-router href - real url of current page

Examples

All usage examples you can see in test/fixtures

GH Pages example

URL

Helpers use URL constructor to parse url's. Mb you need install polyfill to use it.