• Stars
    star
    220
  • Rank 180,422 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 1 year ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Warm up Vite's transform cache

vite-plugin-warmup

Warm up Vite's transform cache as soon as the server initializes.

Requires Vite >=4.3. Does not work with middlewareMode.

Why

On-demand nature

Vite at it's core is an on-demand file server. When a request comes in, it will transform the file and serve it. This means we only do the work that is requested, keeping the dev server fast.

However, sometimes we know in advance which files will be requested when we start our development cycle. Instead of Vite idling before we open up the page, we can start transforming the files beforehand so when it gets requested, it's cached and can be served immediately.

Deep module graph

Take this module graph where the left file would import the right file:

foo.js -> bar.vue -> baz.js -> qux.json

The imported ids can only be known after the file is transformed, so if bar.vue takes some time to transform, baz.js has to wait for it's turn, and so on. This causes an internal waterfall the deeper the imports are.

By warming up baz.js, or any files that you know is hot path in your app, they'll be cached and can be served immediately.

Usage

Setup

Install vite-plugin-warmup:

npm install vite-plugin-warmup

Use the Vite plugin:

// vite.config.js
import { defineConfig } from 'vite'
import { warmup } from 'vite-plugin-warmup'

export default defineConfig({
  plugins: [
    warmup({
      // warm up the files and its imported JS modules recursively
      clientFiles: ['./**/*.html', './src/components/*.jsx']
    })
  ]
})

The plugin accepts clientFiles and ssrFiles options. As the name suggests, the files specified would be warmed up for the client and server transforms respectively.

The files can be direct file names or glob patterns using fast-glob.

NOTE: It's recommended to not turn off server.preTransformRequests in the Vite config, as that prevents transforming the module graph recursively.

Which files should I warm up?

If you're using Vite SPA with a index.html file, add that to the clientFiles option and you're good to go!

warmup({ clientFiles: ['./**/*.html'] })

You can also run DEBUG="vite:transform" vite to see the files that are being transformed. With vite-plugin-warmup, you should see these logs before you load the page.

Logs that appear after the page load are URLs that didn't get warmed up, if so, you can add more of them into the clientFiles option. Note that only actual files in the file system are supported.

vite:transform 28.72ms /@vite/client +1ms
vite:transform 62.95ms /src/components/BigComponent.jsx +1ms
warmup({ clientFiles: ['./**/*.html', './src/components/BigComponent.jsx'] })

If you're using SSR, you can use the ssrFiles option instead. As the Vite logs doesn't differentiate between client and server transforms, make sure the files added are safe to be transformed in the client or server respectively.

NOTE: Make sure to read the Why section to understand which files to add to not overload the Vite server on startup.

Metaframeworks

Some metaframeworks don't load the files through Vite directly, so this plugin might not work for them. To be sure which files are loaded by Vite, you can start the metaframework with the DEBUG="vite:transform" flag and follow the steps above.

vite-plugin-warmup also exports a warmupFile function you can use to warm up specific files (absolute paths only). If you need more control, you can reuse the warmupFile implementation in index.js.

Attribution

Sponsors

Sponsors

License

MIT

More Repositories

1

publint

Lint packaging errors
JavaScript
953
star
2

whyframe

Develop components in isolation with just an iframe
JavaScript
536
star
3

create-vite-extra

Extra Vite templates
JavaScript
370
star
4

svelte-preprocess-import-assets

Import assets directly in your markup
JavaScript
91
star
5

ihimnm

JavaScript
80
star
6

vite-plugin-iso-import

Import modules isomorphically in the client or server
JavaScript
74
star
7

bun-vite-ts-test

How far can we get with bun 👀
CSS
62
star
8

renoma

A recursive node modules analyzer with opinionated package health checks
JavaScript
55
star
9

svelte-fast-dimension

Fast dimension bindings using ResizeObserver
JavaScript
40
star
10

svelte-router

An easy-to-use SPA router for Svelte
TypeScript
32
star
11

svelte-url

Barebones routing with reactive URLs
JavaScript
27
star
12

LuaTween-for-Rainmeter

Animate stuff in Rainmeter easily
Lua
24
star
13

refined-github-comments

JavaScript
24
star
14

fmu

A collection of JS module utilities written in Rust
Rust
21
star
15

pick-palette

A color palette manager made with Svelte
Svelte
19
star
16

package-library

Vue
16
star
17

colorblind

A zero-dependencies color blindness simulation library
TypeScript
15
star
18

svelte-motion

Experimental web animations support for Svelte transitions
Svelte
14
star
19

substitute-string-action

🚀️ Easily substitute or replace strings in GitHub Actions using YAML
JavaScript
14
star
20

release-for-reddit-action

🚀️ Submit a Reddit post about your release in GitHub Actions
JavaScript
14
star
21

website

Svelte
11
star
22

svelte-js-in-css

Svelte
7
star
23

svelte-parse-markup

Parse Svelte markup without parsing the script or style tags
JavaScript
6
star
24

svelte-view-source

Svelte
5
star
25

tailwind-color-gen

Yet another Tailwind color palette generator
Vue
5
star
26

astro-pages-hmr

Support HMR in pages in Astro
Astro
5
star
27

sponsors

A zero-dependencies script to generate sponsors SVG from Patreon
JavaScript
4
star
28

cloudflare-lit-ssr-repro

JavaScript
3
star
29

material-shadow-preview

Visualize Material Design's elevations and generate CSS!
Vue
3
star
30

svelte-as-markup-preprocessor

Runs script and style preprocessors in the markup phase.
TypeScript
3
star
31

vite-plugin-cloudflare-redirect

Vite plugin to handle Cloudflare's _redirects file
JavaScript
3
star
32

cleanup

JavaScript
2
star
33

timetable

A timetable for personal use
Vue
1
star
34

astro-cdn-test

Astro
1
star
35

penn-todo

A todo app built on the PENN stack
JavaScript
1
star
36

svelte-preprocess-inline-logic

JavaScript
1
star
37

Rainmeter-Tron-Suite

A Tron-inspired Rainmeter suite
Lua
1
star
38

kit-iso-import

Svelte
1
star
39

trailing-slash-servers

See how different servers handle serving HTML files and trailing slashes
JavaScript
1
star