Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue-route-prefetch

Clarkkkk204MIT3.0.2TypeScript support: included

A Vue plugin to prefetch the route components.

vue, prefetch, preload, vue-router, prefetch route, prefetch page, quicklink

readme

vue-route-prefetch

NPM version NPM Downloads License Minified Size Build Status

Note: This is a fork of vue-router-prefetch with only Vue 3 support. If you are using Vue 2, please consider the original vue-router-prefetch.

Features

  • Prefetch links when they are visible in viewport.
  • Provide a composable for manually prefetch.
  • Tiny-size.

Install

npm i vue-route-prefetch

Or

pnpm i vue-route-prefetch
yarn add vue-route-prefetch

Usage

You need to use this plugin after vue-router:

import { createApp } from 'vue'
import { createRouter } from 'vue-router'
import PrefetchPlugin from 'vue-route-prefetch'

const app = createApp()
const router = createRouter()
app.use(router).use(PrefetchPlugin)

Now you can replace your <RouterLink> that needs to prefetch to <PrefetchLink>. When this component is visible in viewport, it will automatically prefetch the (async) route component.

Check out the live demo.

You can also register the component with another name:

app.use(RouterPrefetch, {
  componentName: 'QuickLink'
})

Now you can use it as <quick-link> in your app.

Props

All props of <RouterLink> are still available in <PrefetchLink>, additional props are listed below.

prefetch

  • Type: boolean
  • Default: true

Whether to prefetch the matched route component.

You can also set meta.prefetch on vue-router's route object to disable prefetching this route for all <router-link>s:

createRouter({
  routes: [
    {
      path: '/some-async-page',
      meta: { prefetch: false },
      component: () => import('./async-page.vue')
    }
  ]
})

It's also possible to turn of prefetching globally:

app.use(RouterPrefetch, {
  prefetch: false
})

prefetchFiles

  • Type: string[]
  • Examples: ['/foo.css']

A list of additional files to prefetch. By default we only prefetch the route component.

You can also set meta.prefetchFiles on vue-router's route object, it will be merged with the prop value:

createRouter({
  routes: [
    {
      path: '/some-async-page',
      meta: { prefetchFiles: ['/foo.css'] },
      component: () => import('./async-page.vue')
    }
  ]
})

timeout

  • Type: number
  • Default: 2000 (ms)

Timeout after which prefetching will occur.

Manully prefetch

You can prefetch manually by using usePrefetch.

Signature:

function usePrefetch(): {
    prefetchRoute: (link: RouteLocationRaw) => void;
    prefetchFiles: (files: string[]) => void;
}
<script setup>
import { useRouter } from 'vue-router'
import { usePrefetch } from 'vue-route-prefetch'

const router = useRouter()
const { prefetchRoute, prefetchFiles } = usePrefetch()
</script>
<template>
  <div>
    <button @mouseenter="prefetchRoute('/details')" @click="router.push('/details')">
      See details
    </button>
    <button @mouseenter="prefetchFiles('/theme.css')">
      Switch theme
    </button>
  </div>
</template>

Browser Support

It works on the browsers with the support of Intersection Observer API (See compatibility). Otherwise, you may need a polyfill.

Credits

Forked from vue-router-prefetch. Inspired by quicklink and nuxt-link.

Acknowledgment

If you found it useful somehow, I would be grateful if you could leave a star in the project's GitHub repository.

Thank you.

changelog

3.0.2 (2023-10-21)

Bug Fixes

Chores

3.0.1 (2023-10-21)

Bug Fixes

  • externalize vue-router when building (5110d2a5)

3.0.0 (2023-10-20)

Features

  • refactor the plugin and expose a composable (e3f3796b)

    Description

    • refactor the plugin with composition API
    • rewrite all the code with TypeScript
    • export a composable usePrefetch for manully prefetching
### **BREAKING CHANGE**

- `componentName` defaults to 'PrefetchLink' now

Documentation

Chores

2.0.2 (2021-08-12)

Features

  • Upgrade to Vue 3 (#44) (a9c916b3)

    BREAKING CHANGE

    Require Vue 3 and Vue Router 4

  • add typings (#8) (75280f43)

  • support prefetch and prefetchFiles on route object (e39e99b9)
  • increase default timeout, expose as prop (8ef61718)
  • Make use of requestIdleCallback if available (#6) (31eb933f)

    Description

    requestIdleCallback will start up the observation in the next available idle time of the browser, without getting in the way of anything important within the app.

  • use bili (3b88be93)

  • support commonjs modules (#4) (8a391a14) closes #3 * add prefetchFiles prop, closes #1 (1a7fdd76)

Bug Fixes

Documentation

Test

  • add cypress for e2e test (#10) (a63e7ca1)

Chores