Socket
Book a DemoInstallSign in
Socket

vite-content-preload

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-content-preload

[![CI](https://github.com/ErikGS/vite-content-preload/actions/workflows/ci.yml/badge.svg)](https://github.com/ErikGS/vite-content-preload/actions/workflows/ci.yml) [![CD](https://github.com/ErikGS/vite-content-preload/actions/workflows/cd.yml/badge.svg)](

1.0.5
latest
npmnpm
Version published
Weekly downloads
1
-96.77%
Maintainers
1
Weekly downloads
 
Created
Source

vite-content-preload

CI CD

Vite plugin to automatically inject <link rel="preload"> tags for bundled assets (e.g., those imported via relative paths or modules) referenced in initial JS chunks and CSS, optimizing page load performance. Please note that it does not handle assets in the public directory — if you want to know the difference between bundled assets and assets in the public directory, see Vite's asset handling docs for details.

If you have any questions, try checking the F.A.Q.

Features

  • Automatically detects assets referenced in initial JS chunks and CSS
  • Injects <link rel="preload"> for fonts, images, videos, and more
  • Configurable asset size and file extensions
  • Works out-of-the-box with Vite build

See How it works.

Installation

npm install vite-content-preload --save-dev

Usage

Add the plugin to your vite.config.ts:

import autoPreload from 'vite-content-preload'

export default {
  plugins: [
    autoPreload({
      maxSizeKB: 200, // Only preload assets up to 200KB (default)
      extensions: /\.(woff2?|ttf|otf|png|jpe?g|gif|svg|webp|mp4|webm)$/i // File types to preload (default)
    })
  ]
}

Options

  • maxSizeKB

    • Type: number
    • Default: 200
    • Description: Maximum asset size (in KB) to preload.
  • extensions

    • Type: RegExp
    • Default: /\.(woff2?|ttf|otf|png|jpe?g|gif|svg|webp|mp4|webm)$/i
    • Description: RegExp to match asset file extensions.

How it works

  • During the Vite build, the plugin scans the final HTML for referenced JS and CSS files.
  • It finds bundled assets (e.g., those imported via relative paths or modules) referenced in those initial JS chunks and CSS (fonts, images, videos, etc.).
  • For each asset matching the configured extensions and under the size limit, it injects a <link rel="preload"> tag into the HTML <head>.
  • This helps browsers start fetching critical assets earlier, improving page load performance.
  • Note: This plugin targets bundled assets for optimal performance and does not handle assets in the public directory, as they are not part of Vite's optimized build process. For best results, use relative imports to enable bundling, hashing, and optimization — see Vite's asset handling docs for details.

Example

Given this HTML:

<html>
  <head>
    <script src="main.js"></script>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body></body>
</html>

If main.js imports font.woff2 and styles.css references image.png, the plugin will inject before </head>:

<link rel="preload" href="/font.woff2" as="font" crossorigin>
<link rel="preload" href="/image.png" as="image">

FAQ

Q: Why isn't my image in the public folder being preloaded?

A: The plugin targets bundled assets for optimal performance. Public assets are copied as-is without hashing or optimization. To preload your image, move it to an assets folder and reference it with a relative path (e.g., import img from './assets/my-image.png'; or <img src="./assets/my-image.png" /> in templates). This enables Vite to process it fully—see Vite's asset handling docs for details.

Q: I'm using absolute paths like url('/images/bg.png') — how do I switch to bundled?

A: Refactor to relative paths (e.g., url('../images/bg.png')) so Vite bundles it. If the asset is in the public directory, consider relocaing it outside public and switching to import-based handling. Benefits of using import-based assets includes automatic filename hashing for better caching and potential size reductions — see Vite's asset handling docs for details.

Q: Can I configure the plugin to include assets in public directory?

A: Not currently, to keep the plugin lightweight and focused on best practices.

License

MIT

Made with ❤️ and ☕ by Erik GS

💖 Support my work → Sponsor me on GitHub

Keywords

vite

FAQs

Package last updated on 15 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.