Skip to content

May 9th, 2024

Vite Meetup SF

Backend Integration

Note

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

  1. In your Vite config, configure the entry and enable build manifest:

    js
    // vite.config.js
    export default defineConfig({
      build: {
        // generate .vite/manifest.json in outDir
        manifest: true,
        rollupOptions: {
          // overwrite default .html entry
          input: '/path/to/main.js',
        },
      },
    })

    If you haven't disabled the module preload polyfill, you also need to import the polyfill in your entry

    js
    // add the beginning of your app entry
    import 'vite/modulepreload-polyfill'
  2. For development, inject the following in your server's HTML template (substitute http://localhost:5173 with the local URL Vite is running at):

    html
    <!-- if development -->
    <script type="module" src="http://localhost:5173/@vite/client"></script>
    <script type="module" src="http://localhost:5173/main.js"></script>

    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.

    Note 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):

    html
    <script type="module">
      import RefreshRuntime from 'http://localhost:5173/@react-refresh'
      RefreshRuntime.injectIntoGlobalHook(window)
      window.$RefreshReg$ = () => {}
      window.$RefreshSig$ = () => (type) => type
      window.__vite_plugin_react_preamble_installed__ = true
    </script>
  3. 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:

    json
    {
      "main.js": {
        "file": "assets/main.4889e940.js",
        "src": "main.js",
        "isEntry": true,
        "dynamicImports": ["views/foo.js"],
        "css": ["assets/main.b82dbe22.css"],
        "assets": ["assets/asset.0ab0f9cd.png"],
        "imports": ["_shared.83069a53.js"]
      },
      "views/foo.js": {
        "file": "assets/foo.869aea0d.js",
        "src": "views/foo.js",
        "isDynamicEntry": true,
        "imports": ["_shared.83069a53.js"]
      },
      "_shared.83069a53.js": {
        "file": "assets/shared.83069a53.js",
        "css": ["assets/shared.a834bfc3.css"]
      }
    }
    • The manifest has a Record<name, chunk> structure
    • For entry or dynamic entry chunks, the key is the relative src path from project root.
    • For non entry chunks, the key is the base name of the generated file prefixed with _.
    • Chunks will contain information on its static and dynamic imports (both are keys that map to the corresponding chunk in the manifest), and also its corresponding CSS and asset files (if any).
  4. 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. The importedChunks function is for illustration and isn't provided by Vite.

    html
    <!-- if production -->
    
    <!-- for cssFile of manifest[name].css -->
    <link rel="stylesheet" href="/{{ cssFile }}" />
    
    <!-- for chunk of importedChunks(manifest, name) -->
    <!-- for cssFile of chunk.css -->
    <link rel="stylesheet" href="/{{ cssFile }}" />
    
    <script type="module" src="/{{ manifest[name].file }}"></script>
    
    <!-- for chunk of importedChunks(manifest, name) -->
    <link rel="modulepreload" src="/{{ chunk.file }}" />

    Specifically, a backend generating HTML should include the following tags given a manifest file and an entry point:

    • A <link rel="stylesheet"> tag for each file in the entry point chunk's css list
    • 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.
    • A tag for the file key of the entry point chunk (<script type="moudle"> for Javascript, or <link rel="stylesheet"> for css)
    • Optionally, <link rel="modulepreload"> tag for the file of each imported Javascript chunk, again recursively following the imports starting from the entry point chunk.

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

    html
    <link rel="stylesheet" href="assets/main.b82dbe22.css" />
    <link rel="stylesheet" href="assets/shared.a834bfc3.css" />
    <script type="module" src="assets/main.4889e940.js"></script>
    <!-- optional -->
    <link rel="modulepreload" src="assets/shared.83069a53.js" />

    While the following should be included for the entry point views/foo.js:

    html
    <link rel="stylesheet" href="assets/shared.a834bfc3.css" />
    <script type="module" src="assets/foo.869aea0d.js"></script>
    <!-- optional -->
    <link rel="modulepreload" src="assets/shared.83069a53.js" />

Released under the MIT License. (53b21d88)