vite-plugin-prerender-k

Flexible, framework-agnostic static site generation for sites and SPAs built with Vite.
It is inspired by prerender-spa-plugin
✅ Support any framework including Vue, React
✅ Flexible customised configuration
updates
Fixed warning issue in vite5
Install (yarn or npm)
node version: >=12.0.0
vite version: >=2.0.0
yarn add vite-plugin-prerender-k -D
or
npm i vite-plugin-prerender-k -D
Basic Usage (vite.config.js
)
import vitePrerender from 'vite-plugin-prerender-k'
import path from 'path'
export default () => {
return {
plugins: [
vitePrerender({
staticDir: path.join(__dirname, 'dist'),
routes: ['/', '/about', '/some/deep/nested/route'],
}),
],
}
}
Advanced Usage (vite.config.js
)
import vitePrerender from 'vite-plugin-prerender-k'
import path from 'path'
const Renderer = vitePrerender.PuppeteerRenderer
export default () => {
return {
plugins: [
vitePrerender({
staticDir: path.join(__dirname, 'dist'),
outputDir: path.join(__dirname, 'prerendered'),
indexPath: path.join(__dirname, 'dist', 'index.html'),
routes: ['/', '/about', '/some/deep/nested/route'],
postProcess(renderedRoute) {
renderedRoute.route = renderedRoute.originalRoute
renderedRoute.html = renderedRoute.html.split(/>[\s]+</gim).join('><')
if (renderedRoute.route.endsWith('.html')) {
renderedRoute.outputPath = path.join(
__dirname,
'dist',
renderedRoute.route,
)
}
return renderedRoute
},
minify: {
collapseBooleanAttributes: true,
collapseWhitespace: true,
decodeEntities: true,
keepClosingSlash: true,
sortAttributes: true,
},
server: {
port: 8001,
},
renderer: new Renderer({
injectProperty: '__PRERENDER_INJECTED',
inject: {
foo: 'bar',
},
maxConcurrentRoutes: 4,
renderAfterDocumentEvent: 'custom-render-trigger',
renderAfterElementExists: 'my-app-element',
renderAfterTime: 5000,
headless: false,
}),
}),
],
}
}
Documentation
Plugin Options
Option | Type | Required? | Default | Description |
---|
staticDir | String | Yes | None | The root path to serve your app from. |
outputDir | String | No | None | Where the prerendered pages should be output. If not set, defaults to staticDir. |
indexPath | String | No | staticDir/index.html | The index file to fall back on for SPAs. |
postProcess | Function(Object context): [Object | Promise] | No | None | See the Using the postProcess Option section. |
minify | Object | No | None | Minifies the resulting HTML using html-minifier. Full list of options available here. |
server | Object | No | None | App server configuration options (See below) |
renderer | Renderer Instance or Configuration Object | No | new PuppeteerRenderer() | The renderer you'd like to use to prerender the app. It's recommended that you specify this, but if not it will default to @prerenderer/renderer-puppeteer . |
Server Options
Option | Type | Required? | Default | Description |
---|
port | Integer | No | First free port after 8000 | The port for the app server to run on. |
proxy | Object | No | No proxying | Proxy configuration. |
Using The postProcess Option
The postProcess(Object context): Object | Promise
function in your renderer configuration allows you to adjust the output of prerender-spa-plugin
before writing it to a file. It is called once per rendered route and is passed a context
object in the form of:
{
route: String,
originalRoute: String,
html: String,
outputPath: String || null
}
You can modify context.html
to change what gets written to the prerendered files and/or modify context.route
or context.outputPath
to change the output location.
You are expected to adjust those properties as needed, then return the context object, or a promise that resolves to it like so:
postProcess(context) {
if (context.route.endsWith('.html')) {
context.outputPath = path.join(__dirname, 'dist', context.route)
}
return context
}
postProcess(context) {
return someAsyncProcessing(context.html)
.then((html) => {
context.html = html;
return context;
});
}
@prerenderer/renderer-puppeteer
options
Option | Type | Required? | Default | Description |
---|
maxConcurrentRoutes | Number | No | 0 (No limit) | The number of routes allowed to be rendered at the same time. Useful for breaking down massive batches of routes into smaller chunks. |
inject | Object | No | None | An object to inject into the global scope of the rendered page before it finishes loading. Must be JSON.stringifiy -able. The property injected to is window['__PRERENDER_INJECTED'] by default. |
injectProperty | String | No | __PRERENDER_INJECTED | The property to mount inject to during rendering. |
renderAfterDocumentEvent | String | No | None | Wait to render until the specified event is fired on the document. (You can fire an event like so: document.dispatchEvent(new Event('custom-render-trigger')) |
renderAfterElementExists | String (Selector) | No | None | Wait to render until the specified element is detected using document.querySelector |
renderAfterTime | Integer (Milliseconds) | No | None | Wait to render until a certain amount of time has passed. |
skipThirdPartyRequests | Boolean | No | false | Automatically block any third-party requests. (This can make your pages load faster by not loading non-essential scripts, styles, or fonts.) |
consoleHandler | function(route: String, message: ConsoleMessage) | No | None | Allows you to provide a custom console.* handler for pages. Argument one to your function is the route being rendered, argument two is the Puppeteer ConsoleMessage object. |
[Puppeteer Launch Options] | ? | No | None | Any additional options will be passed to puppeteer.launch() , such as headless: false . |
@prerenderer/renderer-jsdom
options
Option | Type | Required? | Default | Description |
---|
maxConcurrentRoutes | Number | No | 0 (No limit) | The number of routes allowed to be rendered at the same time. Useful for breaking down massive batches of routes into smaller chunks. |
inject | Object | No | None | An object to inject into the global scope of the rendered page before it finishes loading. Must be JSON.stringifiy -able. The property injected to is window['__PRERENDER_INJECTED'] by default. |
injectProperty | String | No | __PRERENDER_INJECTED | The property to mount inject to during rendering. |
renderAfterDocumentEvent | String | No | None | Wait to render until the specified event is fired on the document. (You can fire an event like so: document.dispatchEvent(new Event('custom-render-trigger')) |
renderAfterElementExists | String (Selector) | No | None | Wait to render until the specified element is detected using document.querySelector |
renderAfterTime | Integer (Milliseconds) | No | None | Wait to render until a certain amount of time has passed. |
License
MIT