Home

Awesome

<h1 align="center"><img width="42" style="vertical-align:middle" alt="" src="assets/logo.png?raw=true"> Prerenderer</h1> <p align="center"> <em>Fast, flexible, framework-agnostic prerendering for sites and SPAs.</em> </p>
<div align="center">

test npm downloads js-standard-style license

</div>
<div align="left">

Monorepo for the following NPM packages

</div>

About prerenderer

The goal of this package is to provide a simple, framework-agnostic prerendering solution that is easily extensible and usable for any site or single-page-app.

Now, if you're not familiar with the concept of prerendering, you might predictably ask...

What is Prerendering?

Recently, SSR (Server Side Rendering) has taken the JavaScript front-end world by storm. The fact that you can now render your sites and apps on the server before sending them to your clients is an absolutely revolutionary idea (and totally not what everyone was doing before JS client-side apps got popular in the first place...)

However, the same criticisms that were valid for PHP, ASP, JSP, (and such) sites are valid for server-side rendering today. It's slow, breaks fairly easily, and is difficult to implement properly.

Thing is, despite what everyone might be telling you, you probably don't need SSR. You can get almost all the advantages of it (without the disadvantages) by using prerendering. Prerendering is basically firing up a headless browser, loading your app's routes, and saving the results to a static HTML file. You can then serve it with whatever static-file-serving solution you were using previously. It just works with HTML5 navigation and the likes. No need to change your code or add server-side rendering workarounds.

In the interest of transparency, there are some use-cases where prerendering might not be a great idea.

Example prerenderer Usage

(It's much simpler if you use prerenderer with webpack or another build system.)

Input

app/
├── index.html
└── index.js // Whatever JS controls the SPA, loaded by index.html

Output

app/
├── about
│   └── index.html // Static rendered /about route.
├── index.html // Static rendered / route.
├── index.js // Whatever JS controls the SPA, loaded by index.html
└── some
    └── deep
        └── nested
            └── route
                └── index.html // Static rendered nested route.
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const Prerenderer = require('@prerenderer/prerenderer')
// Make sure you install a renderer as well!
const JSDOMRenderer = require('@prerenderer/renderer-jsdom')

const prerenderer = new Prerenderer({
  // Required - The path to the app to prerender. Should have an index.html and any other needed assets.
  staticDir: path.join(__dirname, 'app'),
  // The plugin that actually renders the page.
  renderer: new JSDOMRenderer(),
  postProcess (renderedRoute) {
    // Replace all http with https urls and localhost to your site url
    renderedRoute.html = renderedRoute.html.replace(
      /http:/ig,
      'https:',
    ).replace(
      /(https:\/\/)?(localhost|127\.0\.0\.1):\d*/ig,
      (process.env.CI_ENVIRONMENT_URL || ''),
    );
  },
})

// Initialize is separate from the constructor for flexibility of integration with build systems.
prerenderer.initialize()
  .then(() => {
    // List of routes to render.
    return prerenderer.renderRoutes(['/', '/about', '/some/deep/nested/route'])
  })
  .then(renderedRoutes => {
    // renderedRoutes is an array of objects in the format:
    // {
    //   route: String (The route rendered)
    //   html: String (The resulting HTML)
    // }
    renderedRoutes.forEach(renderedRoute => {
      try {
        // A smarter implementation would be required, but this does okay for an example.
        // Don't copy this directly!!!
        const outputDir = path.join(__dirname, 'app', renderedRoute.route)
        const outputFile = `${outputDir}/index.html`

        mkdirp.sync(outputDir)
        fs.writeFileSync(outputFile, renderedRoute.html.trim())
      } catch (e) {
        // Handle errors.
      }
    })

    // Shut down the file server and renderer.
    return prerenderer.destroy()
  })
  .catch(err => {
    // Shut down the server and renderer.
    return prerenderer.destroy()
    // Handle errors.
  })

Available Renderers

Which renderer should I use?

Use @prerenderer/renderer-puppeteer if: You're prerendering up to a couple hundred pages.

Use @prerenderer/renderer-jsdom if: You need to prerender thousands upon thousands of pages, but quality isn't all that important, and you're willing to work around issues for more advanced cases. (Programmatic SVG support, etc.)

An alternative faster dom renderer using linkedom is being considered

Documentation

All of the packages are strongly typed using typescript, if some documentation is missing or when in doubt, we recommend referring to the types which are self documenting

Installation

Install your selected assortment of packages with your package manager

Example for Rollup or vite and the Puppeteer renderer:

npm install --save-dev @prerenderer/renderer-puppeteer puppeteer @prerenderer/rollup-plugin

If your package manager does not install peer dependencies automatically, make sure to install puppeteer in your dependencies if you wish to use @prerenderer/renderer-puppeteer, there is different methods of installation, in the documentation we only cover the simple package installation, which will also install the necessary browsers binary

npm install --save-dev puppeteer

Then follow the installation instruction of your package of choice, that you can find in the README of that specific package

Prerenderer Options

OptionTypeRequired?DefaultDescription
staticDirStringYesNoneThe root path to serve your app from. (If you are using a plugin, you don't need to set this, it will be taken from the configuration of webpack or rollup)
indexPathStringNostaticDir/index.htmlThe index file to fall back on for SPAs.
serverObjectNoNoneApp server configuration options (See below)
rendererIRenderer Instance, constructor or String to requireNonew 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.
rendererOptionsObjectNoNoneThe options to pass to the renderer if it was not given as an instance, see below for a list of options
postProcess(renderedRoute: Route, routes: Route[]) => voidNoNoneAllows you to customize the HTML and output path before writing the rendered contents to a file, you can also add your own routes by pushing to the routes parameter

Server Options

OptionTypeRequired?DefaultDescription
portIntegerNoFirst free port after 8000The port for the app server to run on.
proxyObjectNoNo proxyingProxy configuration. Has the same signature as webpack-dev-server
hostStringNo127.0.0.1The host to send requests to. Use with caution, as changing this could result in external urls being rendered instead of your local server, use postProcess if you just want to change urls in the resulting .html
listenHostStringNo127.0.0.1The ip address the server will listen to. (0.0.0.0 would allow external access, use with caution)
beforeFunctionNoNo operationDeprecated: Use hookServer() instead. Function for adding custom server middleware.

Prerenderer Methods

[
  {
    originalRoute: '/route/path', // The requested route path.
    route: '/route/redirected-path', // The final route path after redirection or history change.
    html: '<!DOCTYPE html><html>...</html>' // The prerendered HTML for the route
  },
  // ...
]

@prerenderer/renderer-jsdom Options

None of the options are required, by default the page will render on DOMContentLoaded

OptionTypeDefaultDescription
maxConcurrentRoutesNumber0 (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.
injectObjectNoneAn 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.
injectPropertyString__PRERENDER_INJECTEDThe property to mount inject to during rendering. Does nothing if inject isn't set.
renderAfterDocumentEventStringDOMContentLoadedWait 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'))
renderAfterElementExistsString (Selector)NoneWait to render until the specified element is detected using document.querySelector
renderAfterTimeInteger (Milliseconds)NoneWait to render until a certain amount of time has passed.
timeoutInteger (Milliseconds)30000If this timeout triggers while waiting for an event or an element, the rendering will abort with an error.
JSDOMOptionsBaseOptions{ runScripts: 'dangerously', resources: 'usable', pretendToBeVisual: true }Additional options for JSDOM.fromUrl

@prerenderer/renderer-puppeteer Options

None of the options are required, by default the page will render when puppeteer is ready which is when DOMContentLoaded fires

OptionTypeDefaultDescription
maxConcurrentRoutesNumber0 (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.
injectObjectNoneAn 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.
injectPropertyString__PRERENDER_INJECTEDThe property to mount inject to during rendering. Does nothing if inject isn't set.
renderAfterDocumentEventStringDOMContentLoadedWait 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'))
renderAfterTimeInteger (Milliseconds)NoneWait to render until a certain amount of time has passed.
renderAfterElementExistsString (Selector)NoneWait to render until the specified element is detected using document.querySelector
elementVisibleBooleanNoneIf we should wait until the renderAfterElementExists is visible
elementHiddenBooleanNoneIf we should wait until the renderAfterElementExists is hidden
timeoutInteger (Milliseconds)30000If this timeout triggers while waiting for an event or an element, the rendering will abort with an error.
skipThirdPartyRequestsBooleanfalseAutomatically block any third-party requests. (This can make your pages load faster by not loading non-essential scripts, styles, or fonts.)
headlessBooleantrueWhether to run the browser in headless mode
consoleHandlerfunction(route: String, message: ConsoleMessage)NoneAllows 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.
viewportViewportNoneThose options will be passed to puppeteer.launch().
launchOptionsLaunchOptionsNoneThose options will be passed to puppeteer.launch().
navigationOptionsWaitForOptionsNoneThose options will be passed to page.goto(), such as timeout: 30000ms.

@prerenderer/webpack-plugin Options

Implementation details

The @prerenderer/webpack-plugin requires HtmlWebpackPlugin to be configured and serve your html as this plugin is hooked into it.

None of the options are required, by default the renderer-puppeteer will be used and render only the entry file

OptionTypeDefaultDescription
routesArray<string>['/']The list of routes to prerender
fallbackBooleanStringfalse
rendererstring or instance of a renderer'@prerenderer/renderer-puppeteer'The instance of the renderer or the name of the renderer to require
rendererOptionsObjectNoneThe options to pass to the renderer if it was not given as an instance, see above for a list of options
postProcess(renderedRoute: Route) => voidNoneAllows you to customize the HTML and output path before writing the rendered contents to a file
urlModifier(url: string) => stringNoneHook to be able to modify the url to retrieve the compiled asset
entryPathStringindexPath optionThe entry html file to use
...Additional Prerenderer Options

@prerenderer/rollup-plugin Options

Implementation details

The @prerenderer/rollup-plugin and @prerenderer/webpack-plugin aims to have the same feature set and api for an easy migration

As such the options are the same as the webpack-plugin

Caveats

Contributing

This is a monorepo, which uses lerna, so you'll need to clone the repository, then run npm install inside the directory

Run npm run test to make sure that everything is working correctly

Maintainers

<table> <tbody> <tr> <td align="center"> <a href="https://github.com/tofandel"> <img width="150" height="150" src="https://github.com/tofandel.png?v=3&s=150"> </br> Adrien Foulon </a> </td> <td align="center"> <a href="https://github.com/tribex"> <img width="150" height="150" src="https://github.com/JoshTheDerf.png?v=3&s=150"> </br> Joshua Bemenderfer </a> </td> </tr> <tbody> </table>