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

express-prerender

v1.1.5

Published

A prerender middleware for express. This middleware serves cached prerendered pages for crawlers or if no cached page exists for a requested page it will cache a page using PhantomJS and respond with that newly cached page.

Downloads

31

Readme

##express-prerender A package for prerendering pages for crawlers. This is useful when meta tags are dependent on a JavaScript framework to be loaded first e.g. Ember. The middleware will serve a cached page for any requests by crawlers to a page or cache the page if no valid cache exists. This way the middleware handles dynamic pages very well since it does not need to know of any paths on the website before hand. Providing a path to the website folder enables the middleware to determine wether a cached page is still valid or not.

PhantomJS dependant

express-prerender requires PhantomJS to be installed on the system.

Install

npm install express-prerender

Usage

In your express application above other routes, just require the plugin with your configuration like such:

var prerender = require("express-prerender")({
    cache_path      : path/where/cached/files/are/saved,
    dist_folder     : website/distribution/folder, 
    ignore          : ["list", "of", "strings"],
    protocol        : "http" | "https",
    verbose         : bool,
});
app.use(prerender.prerender);

Both cache_path and dist_folder are relative to the file where express-prerender is required or absolute.

The reason to give the path of the distribution folder in dist_folder is for the express-prerender to know when a cached page is no longer valid. This way when the website folder is modified the pages will be recached on next crawler hit.

ignore is a list of strings that, if any path includes any of the strings then the express-prerender will not try to cache the request even though it is requested by a crawler. This is useful for leaving out calls for resources.

protocol can either be "http" or "https" by default protocol is set to "https".

verbose is either true or false, by default it's set to false.

What it does

express-prerender will filter any requests with express-useragent where the user-agent corresponds to a robot. If the user-agent is not one, then next() is called to continue your express app as normal. Otherwise express-prerender will attempt to read a cached file of the requested page.

If such a cached version of the page exists, then that is served to the crawler. Otherwise PhantomJS is spawned in a child process to render the requested path on localhost and then that is served to the crawler and cached for next hit on that page.

The dist_folder is looked at to determine wether the last modified time was before or after the last modified time of a cached page. This is in order to determine wether a cached page is still valid, if not the page will be recached.

Test

You can test the middleware with curl. Issuing a normal request to your website should reveal what you would normally see:

curl yoursite

To check that robots are served with a prerendered version, then you can set the useragent to 'twitterbot' for instance:

curl yoursite -A twitterbot