Home

prerenderspaplugin

prerender-spa-plugin is a Webpack plugin designed to prerender the HTML of routes in a single-page application at build time. Its primary purpose is to generate static HTML snapshots that can be served to search engines and users, providing better SEO and faster initial content for environments where JavaScript execution is limited or slow.

The plugin works by launching a lightweight server to serve the built assets and then using a

Configuration typically centers on a few key options. staticDir points to the directory containing the built

Limitations include that prerendering captures a snapshot at build time and may not reflect subsequent data

headless
browser
to
visit
each
configured
route.
For
every
route,
it
waits
for
the
page
to
finish
rendering,
captures
the
resulting
HTML,
and
writes
it
to
the
corresponding
location
in
the
output
directory.
This
process
results
in
static
HTML
files
alongside
the
usual
JavaScript
bundles,
allowing
crawlers
that
do
not
execute
JS
to
receive
meaningful
content.
assets.
routes
is
an
array
of
URL
paths
to
prerender,
such
as
'/',
'/about',
and
'/contact'.
renderer
selects
the
rendering
engine,
commonly
a
Puppeteer-based
renderer,
though
older
setups
may
use
a
PhantomJS-based
option.
Additional
timing
controls
like
renderAfterTime
or
renderAfterElementExists
help
determine
when
a
page
is
considered
ready.
postProcessHtml
and
postProcess
allow
modification
of
the
HTML
after
it
is
rendered.
The
plugin
is
usually
instantiated
in
the
webpack.config.js
with
new
PrerenderSpaPlugin({
...options...
}).
loaded
asynchronously.
It
can
increase
build
duration
and
requires
compatibility
between
the
plugin,
Webpack
version,
and
the
chosen
headless
browser
renderer.
For
dynamic
content
that
changes
after
load,
alternative
approaches
such
as
server-side
rendering
or
static
site
generation
may
be
more
appropriate.