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

spa-prerender-cli

v0.1.0

Published

A CLI program to prerender single page app with Chromium, like VueJs, React, Angular.

Downloads

22

Readme

spa-prerender-cli

A CLI program to prerender single page app with Chromium, like VueJs, React, Angular. Also generate sitemap.xml

js 命令行程序, 使用 Chromium 浏览器预渲染 SPA(单页面应用)网站. 支持 VueJs, React, Angular. 同时生成 sitemap.xml.

Install / 安装

npm install spa-prerender-cli

Usage / 使用

Create config / 创建配置文件

Create prerender.config.ts.

import { defineConfig } from "spa-prerender-cli";

export default defineConfig({
  origin: "https://example.com",
  staticDir: "dist",
  outDir: "dist-pre",
});

Add script in package.json / 在 package.json 添加执行脚本

"scripts": {
  "prerender": "prerender"
}

Run script / 运行脚本

npm run prerender

How it works / 工作原理

It creates a server for SPA applications, then use Chromium browser to visit home page, find links, and scrape its rendered HTML code. It finds all internal links from element a and link[rel="alternate"]. If a link can't be found, add it to addtionalUrl in config. It injects variable window.__IS_GENERATED__ = true, you can use this to detect if prerendered.

它创建临时服务器运行 SPA 程序, 然后使用 Chromium 浏览器访问首页, 查找链接, 并抓取渲染后的 HTML 代码. 它通过a标签和link[rel="alternate"]查找站内链接. 如果某链接不能被找到, 把它添加到配置的addtionalUrl中. 会注入变量window.__IS_GENERATED__ = true, 你可以使用此变量判断是否是预渲染后的 HTML.

Config / 配置

// 默认配置
const defaultConfig = {
  staticDir: "dist", // SPA bundle. SPA程序的构建结果文件夹路径.
  outDir: "dist-pre", // pre-rendered bundle. 预渲染的结果路径.
  retry: 3, // retry to scrape a page. 抓取页面失败时的重试次数.
  workers: 5, // scrape several pages at the same time. 同时抓取多个页面.
  urlAmountLimit: 1000, // maximum number of url. 最大链接数量.
  pageTimeout: 1000 * 20, // timeout to scrape a page, unit ms. 抓取页面超时, 单位毫秒.
  addtionalUrl: [] as string[], // 其他入口链接.
  viewport: { width: 1773, height: 887 }, // browser viewport. 抓取时浏览器的大小.
};
// required config. 必填配置
{
  // site origin, e.g.: https://example.com
  origin: string;
}
// optional config. 可选配置
{
  // replace, e.g.: replace: {'hello world': 'world hello'}
  // 文字替换, 是一个对象, 将会把键替换成值. 见上一行的例子
  replace: Record<string, string>;
}

Examples / 例子

Feel free to add examples here if your project works well with spa-prerender-cli.

如果 spa-prerender-cli 在你的项目工作良好, 欢迎添加到此处.