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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-plugin-preload-assets

v1.2.3

Published

Optimizes performance by automatically injecting preload and preconnect tags for critical resources with Vite (JS, CSS, fonts, images).

Readme

EN | FR

vite-plugin-preload-assets

vite-plugin-preload-assets is a Vite plugin designed to improve performance by automatically injecting <link rel="preload"> and <link rel="preconnect"> tags for critical resources (images, fonts, JS, and CSS) at build time.

npm version vite compatibility

Features

  • 📷 Automatically preloads images marked with data-preload
  • 🌗 Handles dark mode variants (when using browserux-theme-switcher) and preloads them as well (e.g. logo-dark.png)
  • 🔤 Preloads fonts via configuration (fontsToPreload)
  • 🧠 Preloads critical JS/CSS files by matching configured names dynamically in the build output
  • 🌐 Automatically injects <link rel="preconnect"> tags for Google Fonts (fonts.googleapis.com, fonts.gstatic.com)
  • 🚀 Optimizes initial load performance with zero manual effort
  • 🧼 No manual HTML changes required

Installation

npm install vite-plugin-preload-assets --save-dev

Utilisation

In the vite.config.ts, vite.config.js, or vite.config.mjs file:

import preloadAssetsPlugin from 'vite-plugin-preload-assets'

export default {
    plugins: [
        preloadAssetsPlugin({
            preloadGoogleFonts: true,
            fontsToPreload: [
                {
                    href: '/fonts/Inter.woff2',
                    type: 'font/woff2',
                    crossorigin: true
                }
            ],
            criticalJs: ['main'],
            criticalCss: ['main']
        })
    ]
}

Image Preloading

Simply add the data-preload attribute to an image in your HTML:

<img src="/img/logo.png" data-preload width="100" height="100" alt="Logo">

The plugin will automatically inject:

<link rel="preload" href="/img/logo.png" as="image">

Support for dark mode variants

If your image also supports dark mode and is used with the browserux-theme-switcher, you can add the has-dark class:

<img src="/img/logo.png" class="has-dark" data-preload alt="Logo">

This will inject both versions:

<link rel="preload" href="/img/logo.png" as="image">
<link rel="preload" href="/img/logo-dark.png" as="image">

This ensures both light and dark versions are loaded early and switch instantly on theme change.

Available options

imagesToPreload

Explicitly preload images that are not in index.html (e.g. used in React components):

imagesToPreload: [
  '/img/logo.png',
  '/img/hero.jpg'
]

This will generate:

<link rel="preload" href="/img/logo.png" as="image">
<link rel="preload" href="/img/hero.jpg" as="image">

fontsToPreload

List of fonts to preload manually.

fontsToPreload: [
  {
    href: '/fonts/Inter.woff2',  // URL (relative or absolute)
    type: 'font/woff2',          // MIME type (default : 'font/woff2')
    crossorigin: true            // add the `crossorigin` attribute
  },
  {
    href: 'https://fonts.googleapis.com/css2?family=Dosis:[email protected]&display=swap',
    as: 'style',                 // Override preload type (default: 'font')
    crossorigin: true
  }
]
  • You can override the as attribute ('font' by default) to support custom cases like Google Fonts CSS.
  • If you're preloading a CSS file from Google Fonts, use as: 'style'.

If a is not set to 'font', the plugin will automatically skip the type attribute to avoid preload warnings.

preloadGoogleFonts

Automatically injects the preconnect tags needed to optimize the loading of Google Fonts:

preloadGoogleFonts: true

This will automatically inject the following into the <head>:

<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

criticalJs

List of JS file names to preload (without hash), or a function to dynamically resolve them based on the current HTML page path.

Basic usage (array):

criticalJs: ['main', 'app']

This will match files like:

  • /assets/main-abc123.js
  • /assets/app-def456.js

Advanced usage (per page):

criticalJs: (path) => {
  if (path === '/index.html') return ['main']
  if (path.includes('/blog/')) return ['blog']
  return []
}

This allows per-page targeting in multi-page apps.

criticalCss

Same as criticalJs, but for generated CSS files.

Basic usage (array):

criticalCss: ['main', 'style']

Matches:

  • /assets/main-abc123.css
  • /assets/style-xyz789.css

Advanced usage (per page):

criticalCss: (path) => {
  if (path === '/index.html') return ['main']
  if (path.includes('/blog/')) return ['blog']
  return []
}

How it works

  • The plugin runs during the build phase (using the transformIndexHtml hook)
  • It scans the HTML and the Vite bundle
  • It injects <link rel="preload"> tags at the very beginning of the <head> (head-prepend)

Best Practices

Using preload can improve perceived performance — but only when used wisely.

Here are some tips:

  • Only preload what’s essential
  • Don’t use preload on all images — this can unnecessarily overload the network.
  • Preload only:
    • Critical fonts used immediately
    • Images visible above the fold
    • JS or CSS files required for the initial render
  • Avoid preloading secondary assets
  • Do not preload decorative background images or non-critical assets.
  • Don’t preload all JS chunks — use criticalJs only for main entry files.
  • Don’t replace prefetch or lazy loading
  • preload is not a substitute for loading="lazy" on images.

Goal: help the browser prioritize the loading of truly critical resources — not to load everything upfront, which can have the opposite effect.

License

MIT License — Free to use, modify, and distribute.