npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zippybee/prerender

v2.0.3

Published

Fast, flexible, framework-agnostic prerendering for sites and SPAs.

Downloads

19

Readme


test npm downloads js-standard-style license


Monorepo for the following NPM packages

  • npm version

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.

  • Tons of routes - If your site has hundreds or thousands of routes, prerendering will be really slow. Sure you only have to do it once per update, but it could take ages. Most people don't end up with thousands of static routes, but just in-case...
  • Dynamic Content - If your render routes that have content that's specific to the user viewing it or other dynamic sources, you should make sure you have placeholder components that can display until the dynamic content loads on the client-side. Otherwise, it might be a tad weird.

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('@zippybee/prerender');

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.
  postProcess(renderedRoute) {
    // Replace all http with https urls and localhost to your site url
    renderedRoute.html = renderedRoute.html
      .replace(/http:/i, 'https:')
      .replace(/(https:\/\/)?(localhost|127\.0\.0\.1):\d*/i, 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.
  });

Which renderer should I use?

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

Prerenderer Options

| Option | Type | Required? | Default | Description | | --------------- | ----------------------------------------------- | --------- | ---------------------- | ------------------------------------------------------------ | | staticDir | String | Yes | None | The 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) | | indexPath | String | No | staticDir/index.html | The index file to fall back on for SPAs. | | server | Object | No | None | App server configuration options (See below) | | rendererOptions | Object | No | None | The 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[]) => void | No | None | Allows 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

| 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. Has the same signature as webpack-dev-server | | host | String | No | 127.0.0.1 | The 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 | | listenHost | String | No | 127.0.0.1 | The ip address the server will listen to. (0.0.0.0 would allow external access, use with caution) | | ~~before~~ | Function | No | No operation | Deprecated: Use hookServer() instead. Function for adding custom server middleware. |

Prerenderer Methods

  • constructor(options: Object) - Creates a Prerenderer instance and sets up the renderer and server objects.
  • hookServer(cb: (server: Express) => void, stage: Stage = 'pre-fallback') - Use this method to hook into the express server to add middlewares, routes etc
  • initialize(): Promise<void> - Starts the static file server and renderer instance (where appropriate).
  • getOptions(): PrerenderFinalOptions - Returns the options used to configure prerenderer
  • getServer(): Server - Returns the Server class holding the express server
  • destroy(): Promise<void> - Destroys the static file server and renderer, freeing the resources.
  • renderRoutes(routes: Array<String>): Promise<Array<RenderedRoute>> - Renders set of routes. Returns a promise resolving to an array of rendered routes in the form of:
[
  {
    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
  },
  // ...
];

renderOptions Options

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

| Option | Type | Default | Description | |--------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | maxConcurrentRoutes | Number | 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 | 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 | __PRERENDER_INJECTED | The property to mount inject to during rendering. Does nothing if inject isn't set. | | renderAfterDocumentEvent | String | DOMContentLoaded | 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')) | | renderAfterTime | Integer (Milliseconds) | None | Wait to render until a certain amount of time has passed. | | renderAfterElementExists | String (Selector) | None | Wait to render until the specified element is detected using document.querySelector | | elementVisible | Boolean | None | If we should wait until the renderAfterElementExists is visible | | elementHidden | Boolean | None | If we should wait until the renderAfterElementExists is hidden | | timeout | Integer (Milliseconds) | 30000 | If this timeout triggers while waiting for an event or an element, the rendering will abort with an error. | | skipThirdPartyRequests | Boolean | false | Automatically block any third-party requests. (This can make your pages load faster by not loading non-essential scripts, styles, or fonts.) | | headless | Boolean | true | Whether to run the browser in headless mode | | consoleHandler | function(route: String, message: ConsoleMessage) | 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. | | viewport | Viewport | None | Those options will be passed to puppeteer.launch(). | | launchOptions | LaunchOptions | None | Those options will be passed to puppeteer.launch(). | | navigationOptions | WaitForOptions | None | Those options will be passed to page.goto(), such as timeout: 30000ms. |


Caveats

  • For obvious reasons, prerenderer only works for SPAs that route using the HTML5 history API. index.html#/hash/route URLs will unfortunately not work.
  • Whatever client-side rendering library you're using should be able to at least replace any server-rendered content or diff with it.
    • For Vue.js 1 use replace: false on root components.
    • For Vue.js 2 and 3 Ensure your root component has the same id as the prerendered element it's replacing. Otherwise you'll end up with duplicated content.