Skip to main content
If you want to serve the HTML using a traditional backend (e.g. Rails, Laravel) but use Vite for serving assets, check for existing integrations listed in Awesome Vite.If you need a custom integration, you can follow the steps in this guide to configure it manually.

Configuration Steps

1

Configure Vite entry and manifest

In your Vite config, configure the entry and enable build manifest:
vite.config.js
If you haven’t disabled the module preload polyfill, you also need to import the polyfill in your entry:
2

Set up development scripts

For development, inject the following in your server’s HTML template (substitute http://localhost:5173 with the local URL Vite is running at):
In order to properly serve assets, you have two options:
  • Make sure the server is configured to proxy static assets requests to the Vite server
  • Set server.origin so that generated asset URLs will be resolved using the back-end server URL instead of a relative path
This is needed for assets such as images to load properly.
If you are using React with @vitejs/plugin-react, you’ll also need to add this before the above scripts, since the plugin is not able to modify the HTML you are serving (substitute http://localhost:5173 with the local URL Vite is running at):
3

Generate manifest for production

For production, after running vite build, a .vite/manifest.json file will be generated alongside other asset files. An example manifest file looks like this:
.vite/manifest.json
4

Use the manifest to render links

You can use this file to render links or preload directives with hashed filenames.Here is an example HTML template to render the proper links. The syntax here is for explanation only, substitute with your server templating language:

Manifest Structure

The manifest has a Record<name, chunk> structure where each chunk follows the ManifestChunk interface:

Manifest Entry Types

Each entry in the manifest represents one of the following:
Generated from files specified in build.rollupOptions.input. These chunks have isEntry: true and their key is the relative src path from project root.
Generated from dynamic imports. These chunks have isDynamicEntry: true and their key is the relative src path from project root.
Their key is the base name of the generated file prefixed with _.
Generated from imported assets like images, fonts. Their key is the relative src path from project root.
When build.cssCodeSplit is false, a single CSS file is generated with the key style.css. When build.cssCodeSplit is not false, the key is generated similar to JS chunks (i.e. entry chunks will not have _ prefix and non-entry chunks will have _ prefix).

Rendering Tags in Production

Specifically, a backend generating HTML should include the following tags given a manifest file and an entry point. Note that following this order is recommended for optimal performance:
  1. A <link rel="stylesheet"> tag for each file in the entry point chunk’s css list (if it exists)
  2. Recursively follow all chunks in the entry point’s imports list and include a <link rel="stylesheet"> tag for each CSS file of each imported chunk’s css list (if it exists)
  3. A tag for the file key of the entry point chunk. This can be <script type="module"> for JavaScript, <link rel="stylesheet"> for CSS
  4. Optionally, <link rel="modulepreload"> tag for the file of each imported JavaScript chunk, again recursively following the imports starting from the entry point chunk

Example: Entry Point views/foo.js

Following the example manifest above, for the entry point views/foo.js the following tags should be included in production:

Example: Entry Point views/bar.js

For the entry point views/bar.js:
An example pseudo implementation of importedChunks in TypeScript (This will need to be adapted for your programming language and templating language):