@prerenderer/webpack-plugin
This package is part of the @prerenderer
monorepo, for the rest of the documentation head over to https://github.com/Tofandel/prerenderer#prerendererwebpack-plugin-options
Requirements
This plugin is for webpack 5 and requires the html-webpack-plugin to be setup
Installation
npm i -D @prerenderer/webpack-plugin @prerenderer/renderer-puppeteer
or
npm i -D @prerenderer/webpack-plugin @prerenderer/renderer-jsdom
Basic Usage (webpack.config.js
)
const path = require('path')
const PrerendererWebpackPlugin = require('@prerenderer/webpack-plugin')
module.exports = {
plugins: [
...
new PrerendererWebpackPlugin({
routes: [ '/', '/about', '/some/deep/nested/route' ],
})
]
}
Advanced Usage (webpack.config.js
)
const path = require('path')
const PrerendererWebpackPlugin = require('@prerenderer/webpack-plugin')
module.exports = {
plugins: [
...
new PrerendererWebpackPlugin({
indexPath: 'index.html',
routes: [ '/', '/about', '/some/deep/nested/route' ],
postProcess (renderedRoute) {
renderedRoute.route = renderedRoute.originalRoute
renderedRoute.html = renderedRoute.html.split(/>[\s]+</gmi).join('><')
if (renderedRoute.route.endsWith('.html')) {
renderedRoute.outputPath = path.join(__dirname, 'dist', renderedRoute.route)
}
renderedRoute.html = renderedRoute.html.replace(
/http:/i,
'https:',
).replace(
/(https:\/\/)?(localhost|127\.0\.0\.1):\d*/i,
(process.env.CI_ENVIRONMENT_URL || ''),
);
},
server: {
port: 8001
},
renderer: '@prerenderer/renderer-puppeteer',
rendererOptions: {
injectProperty: '__PRERENDER_INJECTED',
inject: {
foo: 'bar'
},
maxConcurrentRoutes: 4,
renderAfterDocumentEvent: 'custom-render-trigger',
renderAfterElementExists: 'my-app-element',
renderAfterTime: 5000,
timeout: 20000,
headless: false
}
})
]
}
const path = require('path')
const PrerendererWebpackPlugin = require('@prerenderer/webpack-plugin')
module.exports = {
plugins: [
new PrerendererWebpackPlugin({
routes: [ '/', '/about', '/contact' ],
rendererOptions: {
renderAfterDocumentEvent: 'render-event',
inject: {},
timeout: 10000,
},
postProcess: function (context) {
var titles = {
'/': 'Home',
'/about': 'Our Story',
'/contact': 'Contact Us'
}
context.html = context.html.replace(
/<title>[^<]*<\/title>/i,
'<title>' + titles[context.route] + '</title>'
)
}
}
)
]
}