Backend Integration (original) (raw)

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'  
  1. 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:

<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>  
  1. 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
{  
  "_shared-Dbge_PUz.js": {  
    "file": "assets/shared-ChJ_j-JJ.css",  
    "src": "_shared-Dbge_PUz.js"  
  },  
  "_shared-B7PI925R.js": {  
    "file": "assets/shared-B7PI925R.js",  
    "name": "shared",  
    "css": ["assets/shared-ChJ_j-JJ.css"]  
  },  
  "baz.js": {  
    "file": "assets/baz-B2H3sXNv.js",  
    "name": "baz",  
    "src": "baz.js",  
    "isDynamicEntry": true  
  },  
  "views/bar.js": {  
    "file": "assets/bar-gkvgaI9m.js",  
    "name": "bar",  
    "src": "views/bar.js",  
    "isEntry": true,  
    "imports": ["_shared-B7PI925R.js"],  
    "dynamicImports": ["baz.js"]  
  },  
  "views/foo.js": {  
    "file": "assets/foo-BRBmoGS9.js",  
    "name": "foo",  
    "src": "views/foo.js",  
    "isEntry": true,  
    "imports": ["_shared-B7PI925R.js"],  
    "css": ["assets/foo-5UjPuW-k.css"]  
  }  
}  
  1. 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" href="/{{ chunk.file }}" />  

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

<link rel="stylesheet" href="assets/foo-5UjPuW-k.css" />  
<link rel="stylesheet" href="assets/shared-ChJ_j-JJ.css" />  
<script type="module" src="assets/foo-BRBmoGS9.js"></script>  
<!-- optional -->  
<link rel="modulepreload" href="assets/shared-B7PI925R.js" />  

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

<link rel="stylesheet" href="assets/shared-ChJ_j-JJ.css" />  
<script type="module" src="assets/bar-gkvgaI9m.js"></script>  
<!-- optional -->  
<link rel="modulepreload" href="assets/shared-B7PI925R.js" />