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

html-webpack-preload-react-router

v1.0.0

Published

this plugin injects a script that preloads chunks according to the react router plain objects and the current URL path

Downloads

6

Readme

html-webpack-preload-react-router

A webpack plugin that injects an inline script tag to preload chunks based on react router plain objects and the current URL path. It preloads only the relevant chunks for the current page.

Note: This is an extension plugin for html-webpack-plugin - a plugin that simplifies the creation of HTML files to serve your webpack bundles.

Introduction

This plugin aims to reduce page load times by preloading JavaScript and CSS chunks earlier than the traditional approach.

When using React router with code splitting (lazy loading), the traditional page load process is:

  1. Downloading and executing the initial HTML file.
  2. The HTML fetches and executes only the initial Javascripts and CSS chunks.
  3. The react router code examines the current URL path and runs loader method if defined.
  4. The react router code examines the current URL path and fetching and executing only the relevant chunk files based on element property.

traditional way

Each step occurs in a row and has a network request. It is a waste of time! With this plugin 2nd, 3rd and 4th steps will be parallelized network requests.

parallelized way

Installation

This plugin requires html-webpack-plugin as a peer dependency.

To install the Webpack plugin, use the following command:

  npm install --save-dev html-webpack-preload-react-router

Configuration

In your webpack.config.js, require() the plugin as follows:

const ReactRouterPreloadChunksPlugin = require('html-webpack-preload-react-router');

Specify the entry route filename as follows:

const entryRouteFilename = path.resolve(`src/pages/entryRoute.route.js`);

Finally, add the plugin to your webpack configuration's plugins array after HtmlWebpackPlugin:

plugins: [
  new HtmlWebpackPlugin({
    scriptLoading: 'module'
  }),
  new ReactRouterPreloadChunksPlugin({
    entryRoute: entryRouteFilename
  })
]

How to use

When utilizing dynamic imports (lazy loading), include the magic comment webpackChunkName in every import() statement.

For example:

const Home = lazy(()=> import(/* webpackChunkName: 'Home' */ "src/pages/Home"));

Additionally, in every route object employ the chunkName property with the same name used in webpackChunkName.

export const mainRoutes = [{
+   chunkName: "Home",
    path: "",
    element: <Home />,
//...
}]

Recommendation for an article

I recommend reading my article, Boost Your Webpage Load Performance with the new Webpack plugin. It includes an example of using this Webpack plugin and explains how to make your react router plain objects a single source of truth for three different purposes.