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

hypercharged

v0.4.0

Published

Generates a prerendered site from your original website.

Downloads

6

Readme

Generates a prerendered site from your original website.

npm npm bundle size npm dev dependency version Build Status codecov Stryker mutation score Maintainability Known Vulnerabilities NPM

Summary

About

I created this library because I used to exploit a home made tool to generate a prerendered website from my original single page application. This prerendered version would help me pass through the SEO issue caused by bots not being able to execute Javascript. The result let bots parse an HTML generated result that help them understand the content of my website without executing Javascript. To do so, I configure my .htaccess Apache file rule to redirect bots to the prerendered version of my website.

Installation

Before starting

puppeteer will install a supported Chrome executable. It can use additional space so you should be aware of it as it can be a problem on operating system running with a low storage.

If you need, you can use puppeteer-core and instruct it to use an existing Chrome instance. The only downside is if the version of your existing Chrome instance is not the same as the one puppeteer is tested, the behavior of this library will not be guaranteed anymore.

Learn more about the difference here.

Install the dependencies

With npm:

npm install --save-dev puppeteer@2.* hypercharged

Or with Yarn:

yarn add --dev puppeteer@2.* hypercharged

Usage

1. Simple usage

In this example, we will render the home page of http://example.com

const Hypercharged = require("hypercharged").default;
// import Hypercharged from "hypercharged";

const hypercharged = new Hypercharged({
    input: {
        url: "http://example.com",
    },
    output: {
        folder: {
            path: __dirname, // means, in directory containing this file
        },
    },
});

hypercharged.addUrl("/");

(async () => {
    await hypercharged.render();
})();

Result:

your-project
├── index.js
├── index.html <-- the generated file
├── package.json
└── package-lock.json

2. Create the output folder if it does not exist

Hypercharged does not create the output folder if you do not tell it do to so. In this example, we will instruct it to create it if it does not exist.

const Hypercharged = require("hypercharged").default;
// import Hypercharged from "hypercharged";

const hypercharged = new Hypercharged({
    input: {
        url: "http://example.com",
    },
    output: {
        folder: {
            path: __dirname + "/prerendered",
            createIfNotExist: true,
        },
    },
});

hypercharged.addUrl("/");

(async () => {
    await hypercharged.render();
})();

Result:

your-project
├── prerendered
│	└── index.html
├── index.js
├── package.json
└── package-lock.json

3. Generating multiple files

In this example, we will generated multiple pages in a given folder.

const Hypercharged = require("hypercharged").default;
// import Hypercharged from "hypercharged";

const hypercharged = new Hypercharged({
    input: {
        url: "http://example.com",
    },
    output: {
        folder: {
            path: __dirname + "/prerenderd",
            createIfNotExist: true,
        },
    },
});

hypercharged.addUrls(["/", "/about", "/contact-us"]);

(async () => {
    await hypercharged.render();
})();

Result:

your-project
├── prerendered
│	├── about
│	│	└── index.html
│	├── contact-us
│	│	└── index.html
│	└── index.html
├── index.js
├── package.json
└── package-lock.json

4. Using custom puppeteer command before starting to copy the HTML content

If, like me, you are building a single page application, or any other website that relies on Javascript to generate content dynamically, you might be annoyed by the fact that Hypercharged will not wait that your page has finished to execute Javascript before copying the HTML content.

In this example, we will use custom Puppeteer commands to overpass this limitation.

const Hypercharged = require("hypercharged").default;
// import Hypercharged from "hypercharged";

const hypercharged = new Hypercharged({
    input: {
        url: "http://example.com",
    },
    output: {
        folder: {
            path: __dirname + "/prerendered",
            createIfNotExist: true,
        },
    },
});

hypercharged.addUrl("/", async function(page) {
    await page.waitForNavigation({
        waitUntil: "networkidle0",
    });
});

(async () => {
    await hypercharged.render();
})();

For more information which features you can use with the page object, go to the Puppeteer API, then scroll to the Page section.

Result:

your-project
├── prerendered
│	└── index.html
├── index.js
├── package.json
└── package-lock.json

5. Passing options before rendering

Hypercharged runs a Chrome instance in headless mode using puppeteer. You can add custom options to pass to this library before it launches a healess driven Chrome instance.

In this example we will tell puppeteer to show the Chrome window while it runs. This is very interesting to troubleshoot issues and understand why something is not going like expected.

const Hypercharged = require("hypercharged").default;
// import Hypercharged from "hypercharged";

const hypercharged = new Hypercharged({
    input: {
        url: "http://example.com",
    },
    output: {
        folder: {
            path: __dirname,
        },
    },
});

hypercharged.addUrl("/");

(async () => {
    await hypercharged.render({
        headless: false,
    });
})();

You can learn more about which options you can use by reading the documentation on puppeteer.launch() options. puppeteer.launch() is the method Hypercharged uses to start a Chrome instance to get your pages content.

6. Enable the debug mode to print what is Hypercharged doing

In this example, we will activate the debug mode to see in the console what is happening with your urls.

const Hypercharged = require("hypercharged").default;
// import Hypercharged from "hypercharged";

const hypercharged = new Hypercharged({
    input: {
        url: "http://example.com",
    },
    output: {
        folder: {
            path: __dirname,
        },
    },
});

hypercharged.addUrls(["/", "/about"]);

(async () => {
    await hypercharged.render();
})();

Result in console:

rendering /...
rendered
rendering /about...
rendered

7. Use an identical page hold function to a bunch of urls

In this example, we have a lots of urls that needs a same "put the web page on hold" function before copying the content.

const Hypercharged = require("hypercharged").default;
// import Hypercharged from "hypercharged";

const hypercharged = new Hypercharged({
    input: {
        url: "http://example.com",
    },
    output: {
        folder: {
            path: __dirname,
        },
    },
});

const callback = async page => {
    await page.waitFor(".card");
};

hypercharged.addUrls(["/", "/about", "/contact-us"], callback);

(async () => {
    await hypercharged.render();
})();

You can check the list of all the available method on the puppeteer's page object in the dedicated documentation.