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

cohan-presite

v2.0.10

Published

CLI app for pre-rendering SPA websites.

Downloads

2

Readme

presite

NPM version NPM downloads donate

Why Presite?

Presite is an alternative to static site generators like Gatsby, Next.js and Nuxt.js etc, the difference is that it uses Puppeteer to prerender websites instead of relying on server-side rendering.

Install

npm i -g presite

Note that Presite relies on Chrome (or Chromium) browser on your machine, so you need to ensure it's installed before running Presite.

Usage

presite ./path/to/your/site

Presite is supposed to work with existing single-page applications, first you use something like Create React App, Vue CLI, Parcel or Vite to create a production build of your app, then use Presite to pre-render the website to static HTML files.

Pre-rendered website will be generated into .presite folder.

Examples

{
  "scripts": {
-    "build": "react-scripts build"
+    "build": "react-scripts build && presite ./build"
  }
}
{
  "scripts": {
-    "build": "vue-cli-service build"
+    "build": "vue-cli-service build && presite ./dist"
  }
}
{
  "scripts": {
-    "build": "poi build"
+    "build": "poi build && presite ./dist"
  }
}
{
  "scripts": {
-    "build": "vite build"
+    "build": "vite build && presite ./dist"
  }
}

That's it, Presite prerender all pages of your website without any configuration!

Run presite --help for all CLI flags.

Non-HTML pages

Presite also supports rendering non-HTML pages like XML or JSON pages, simply create files ending with .xml.js or .json.js, let's say you have a feed.json.js:

import { createJSONFeed } from './somewhere/create-json-feed'

export default async () => {
  const posts = await fetch('/api/my-posts').then((res) => res.json())
  return createJSONFeed(posts)
}

You can export a function that resolves to a string or JSON object, then Presite will output this page as feed.json.

These pages are evaluated in browser in a <script type="module"> tag, so you can use the import keyword.

Using presite.config.js

Many CLI flags can be stored in a configuration file, it's totaly optional but if you need one, it's there for you.

Besides presite.config.js, you can also use presite.config.json or the presite key in package.json.

Set routes that needs prerender

If some of your pages are not referenced by other pages, you can manually specify them here:

module.exports = {
  routes: ['/', '/about'],
}

Note that in most cases you won't need this option, Presite automatically find all same-site <a> elements on the pages and prerender all of them.

If you want to fetch routes asynchronously, use async/await:

module.exports = {
  async routes() {
    const routes = await fetchRoutesFromSomeWhere()
    return routes
  },
}

Wait

Wait specific ms or dom element to appear:

module.exports = {
  wait: 3000,
  // Or wait for an element to appear
  // wait: '#comments'
}

Maunally set ready state

Instead of using wait you can manually tell when the app is ready:

module.exports = {
  manually: true,
}

Then you can call window.snapshot in your app when its contents are ready:

window.snapshot && window.snapshot()

To use a custom global variable name, set it to a string instead:

module.exports = {
  manually: `__my_snapshot__`,
}

Now you should call window.__my_snapshot__() instead.

Access Puppeteer browser page

Access the page instance, for example, to expose some functions from Node.js to browser:

module.exports = {
  async onBrowserPage(page) {
    await page.exposeFunction('md5', (content) => md5(content))
  },
}

Filter out link to be crawled

To prevent link (from <a> elements) to be crawled, you could use the linkFilter option:

module.exports = {
  // Returns `true` to keep, `false` otherwise
  linkFilter(url) {
    // Ignore URLs ending with .xml
    return !url.endsWith('.xml')
  },
}

Source directory

This is the same as using CLI presite ./path/to/your/spa:

module.exports = {
  baseDir: './path/to/your/spa',
}

Output directory

By default it outputs to .presite folder in current directory.

module.exports = {
  outDir: '.presite',
}

CLI options

Run presite --help.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

presite © egoist, Released under the MIT License. Authored and maintained by egoist with help from contributors (list).

Website · GitHub @egoist · Twitter @_egoistlily