@ampproject/toolbox-optimizer-express

中文文档
2.10.1发布于 2024/06/14Apache-2.0CommonJS近 30 天下载 4471

Express middleware for @ampproject/toolbox-optimizer

原始使用文档
文档目录5 节
  1. Introduction
  2. How it works
  3. Usage
  4. Options
  5. Best Practice: Cache server-side-rendered AMPs

Introduction

npm version

amp-optimizer-express is an express middleware that optimizes page load times for websites using AMP for their canonical pages. The middleware uses the same server-side-rendering optimizations as the Google AMP Cache.

The middleware uses the amp-optimizer component to apply server-side-rendering on the fly.

How it works

amp-optimizer-express intercepts the responses and replaces their content with a version that has been transformed by amp-optimizer.

As the server-side-rendered version of the content is not valid AMP, the component also provides the original content on an alternative URL. Server-side-rendering transformations are not applied to this URL, and the original valid AMP is served.

An AMPHTML link tag is added to the server-side-rendered version, linking it to the original valid AMP version hosted on the alternative URL.

Example:

A valid AMP page is served on https://example.com/index.html.

When the amp-optimizer-express middleware is used, that URL will serve the server-side-rendered version of the content.

The original, valid AMP will then become available at https://example.com/index.html?amp.

An amphtml link will be added to the server-side-rendered version:

<link rel="amphtml" href="https://example.com/index.html?amp">

Usage

Install via:

$ npm install @ampproject/toolbox-optimizer-express

The AMP Optimizer Middleware can be used like any other express middleware.

It is important that the middleware is used before the middleware or route that renders the page.

The example bellow will transform HTML being loaded by express-static:

const express = require('express');
const path = require('path');
const app = express();
const AmpOptimizerMiddleware = require('@ampproject/toolbox-optimizer-express');

// It's important that the AmpOptimizerMiddleware is added *before* the static middleware.
// This allows us to replace the parts needed before static handles the request.
app.use(AmpOptimizerMiddleware.create());

const staticMiddleware = express.static(path.join(__dirname, '/public'));
app.use(staticMiddleware);

Options

The following options are supported:

  • runtimeVersion: true if the optimizer should use versioned runtime imports (default is false).
  • ampOnly: true if the optimizer should only be applied to AMP files (indicated by the lightning bolt in the header).

Example:

app.use(AmpOptimizerMiddleware.create({
  runtimeVersion: true
});

Best Practice: Cache server-side-rendered AMPs

To achieve best performance, those transformations shouldn't be applied for every request. Instead, transformations should only be applied the first time a page is requested, and the results then cached. Caching can happen on the CDN level, on the site's internal infrastructure (e.g.: Memcached), or even on the server itself, if the set of pages is small enough to fit in memory.

版本与发布记录

@ampproject/toolbox-optimizer-express 共发布 88 个版本,首次发布于 2019/06/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.10.1latest2024/06/142 年前
2.10.02024/06/142 年前
2.9.02022/06/244 年前
2.8.102022/03/174 年前
2.8.92022/03/114 年前
2.8.82022/02/124 年前

其他发布通道:beta → 1.1.0-beta.0,alpha → 2.7.0-alpha.1,canary → 3.0.0-canary.2。

关于 @ampproject/toolbox-optimizer-express 的常见问题

@ampproject/toolbox-optimizer-express 是什么?

Express middleware for @ampproject/toolbox-optimizer

如何安装 @ampproject/toolbox-optimizer-express?

在项目目录运行 npm install @ampproject/toolbox-optimizer-express,或使用 pnpm add / yarn add / bun add。

@ampproject/toolbox-optimizer-express 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ampproject__toolbox-optimizer-express 是否存在。

@ampproject/toolbox-optimizer-express 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ampproject/toolbox-optimizer-express 可以商用吗?

@ampproject/toolbox-optimizer-express 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ampproject/toolbox-optimizer-express 的下载量有多少?

根据 npm 官方下载统计,@ampproject/toolbox-optimizer-express 在 2026/10/06 – 2026/11/04 这 30 天内被下载 4,471 次,日均约 149 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ampproject/toolbox-optimizer-express 还在维护吗?

最新版本 2.10.1 发布于 2024/06/14(2 年前);至今共发布 88 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。