vite-prerender-plugin is a Vite plugin designed to perform server-side rendering (prerendering) of web applications to static HTML during the build process. This is particularly useful for improving initial page load performance and SEO for sites that don't require full server-side rendering on every request. The current stable version is `0.5.13`, with minor updates released somewhat regularly to address bug fixes and ensure compatibility with newer Vite versions, currently supporting Vite 5.x through 8.x. A key differentiator of this plugin is its highly flexible approach, being an extraction of the prerendering functionality from the Preact ecosystem's `@preact/preset-vite` and WMR. It delegates the actual rendering logic to the user, who must provide an exported `prerender()` function within a specified script. This function can be synchronous or asynchronous, allowing for data fetching and file system access, and is expected to return an object containing an `html` property with the rendered string. The plugin then injects this HTML into the main document at a configurable `renderTarget` selector, and can automatically discover and prerender additional routes.
npm install vite-prerender-pluginVerified import paths — ran on the pinned version, not inferred.
Shows how to configure `vite-prerender-plugin` in `vite.config.ts`, define a `prerender.ts` script for generating HTML, and prerender basic and dynamically added routes. Includes a minimal `index.html` and client-side `main.ts` for context.
Ensure your `vite.config.ts` includes the `prerenderScript` option pointing to an absolute path, or that your `index.html` contains a `<script prerender src="...">` tag, and that the specified script exports an `async function prerender(url: string): Promise<{html: string}>`.Verify that `renderTarget` (e.g., `'#app'`) matches the selector used in your client-side entrypoint (e.g., `document.querySelector('#app')`).Ensure your installed Vite version matches the peer dependency range specified by `vite-prerender-plugin`, currently `5.x || 6.x || 7.x || 8.x`. Upgrade or downgrade Vite as needed.
If experiencing issues with `magic-string` and Yarn on `0.5.9`, upgrade `vite-prerender-plugin` to `0.5.10` or newer to resolve the dependency resolution problem.
Upgrade `vite-prerender-plugin` to version `0.5.10` or newer. This issue was resolved by reverting `magic-string` to a regular dependency.
This issue was addressed in version `0.5.12`. Upgrade `vite-prerender-plugin` to `0.5.12` or later to apply the fix.
Verify that the `prerenderScript` path in `vite.config.ts` is absolute and correct. Confirm that the specified script explicitly exports the `prerender` function and that it returns an object with a non-empty `html` string. Also, ensure your `renderTarget` option matches your client-side app's root element ID/class.