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

webpack-prefetch-chunk

v1.0.0

Published

Webpack plugin to dynamically prefetch chunks

Downloads

25

Readme

npm

Webpack Prefetch Chunk Plugin

This plugin provides a helper function for prefetching chunks dynamically whenever you want.

You might know the import(/* webpackPrefetch: true */ 'MyModule'); syntax (see documentation). Using that magic comment, the chunk containing MyModule will be prefetched as soon as the chunk containing the import(...) statement is installed. There are use cases where this is too early and unflexible.

Example use case

Consider the following setup for vue-router (this plugin works for any async chunks, vue-router is just an example):

const PageA = () => import(/* webpackChunkName: "page-a" */'./PageA.vue')
const PageB = () => import(/* webpackChunkName: "page-b" */'./PageB.vue')
const PageC = () => import(/* webpackChunkName: "page-c" */'./PageC.vue')

const router = new VueRouter({
  routes: [
    { path: '/a', component: PageA },
    { path: '/b', component: PageB },
    { path: '/c', component: PageC },
  ]
})

Adding /* webpackPrefetch: true */ to any of the above import(...)s will prefetch that chunk directly when the router is being set up. You might end up prefetching a lot of stuff unnecessarily, thereby wasting your user's bandwidth.

Assume we want to prefetch page-b only when the user accesses /a. To do so, we call __webpack_require__.pfc('page-b') somewhere in PageA.vue, for example in the mounted hook.

Real-world use case: A login form. When the user submits the form then a tiny request to your API is made to check the credentials and additionally a large request to load the chunk that renders a dashboard (first page after login). You could prefetch that chunk as soon as the user starts typing something into the login form. Just call __webpack_require__.pfc('chunk-with-dashboard') from the change handler for the input fields.

Install

yarn add webpack-prefetch-chunk

or

npm install webpack-prefetch-chunk

Usage

const PrefetchChunkPlugin = require('webpack-prefetch-chunk');

module.exports = {
    plugins: [
        new PrefetchChunkPlugin()
    ]
};

The plugin adds a __webpack_require__.pfc function to the manifest (pfc = prefetch chunk). Call that function in any module you wish. Pass it the name of the chunk you want to load, i.e. the name specified with /* webpackChunkName: "my-chunk-name" */.

At runtime, a <link rel="prefetch" as="script" href="..."> element is appended to the document head. Nothing is done if the chunk is already loaded or is currently loading/prefetching.

The implementation is inspired by webpack itself. The rest of the code is to ensure that the helper function can be used regardless of the optimization.namedChunks setting.

Using Typescript?

Add the following to a .d.ts file somewhere in your include path specified in tsconfig.json:

declare global {
    const __webpack_require__: {
        pfc(chunkId: string): void,
    }
}