Production Builds
In production there is no Vite dev server. Vite compiles your SPA into hashed, optimized assets
inside wwwroot, and <prod-vite-scripts> renders the tags that load them.
Building the SPA
Section titled “Building the SPA”-
Run the Vite build from the SPA folder:
Terminal window cd ReactAppnpm run build -
Switch the page to the production helper:
Pages/Index.cshtml <prod-vite-scripts app-name="ReactApp" />
What the build produces
Section titled “What the build produces”The plugin points Vite’s output at ../wwwroot/{AppFolder}, so everything the build produces lives
under the app’s own folder. A build of ReactApp produces roughly:
Directorywwwroot/
DirectoryReactApp/
- main.[hash].js the hashed JS bundle
- main.[hash].css the hashed CSS (if any)
- manifest.json Vite’s manifest (source → hashed output)
- manifest.prod.json the plugin’s integration metadata
manifest.jsonis Vite’s standard build manifest — it maps your entrypoint to its hashed output files and lists the CSS that belongs to the entry chunk.manifest.prod.jsonis the plugin’s integration metadata — the entrypoint, the container element id, and whether the app is React.
<prod-vite-scripts> reads both: the hashed paths from manifest.json, and the container id from
manifest.prod.json.
What gets rendered
Section titled “What gets rendered”For a built app, the helper emits:
<link rel="stylesheet" href="/ReactApp/main.[hash].css" /><script type="module" src="/ReactApp/main.[hash].js"></script><div id="root"></div>Because the assets live in wwwroot, they’re served by ASP.NET Core’s static-files middleware —
make sure app.UseStaticFiles() is in your pipeline.
Automating the build in publish
Section titled “Automating the build in publish”To build the SPA as part of dotnet publish, add an MSBuild target that runs the Vite build. For
example:
<Target Name="BuildReactApp" BeforeTargets="Publish"> <Exec Command="npm install" WorkingDirectory="ReactApp" /> <Exec Command="npm run build" WorkingDirectory="ReactApp" /></Target>This ensures the hashed assets and manifest.prod.json exist in wwwroot before your app is
packaged.
Switching helpers per environment
Section titled “Switching helpers per environment”See Rendering a SPA for the pattern of branching
between <dev-vite-scripts> and <prod-vite-scripts> based on IWebHostEnvironment.