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

@ampproject/toolbox-optimizer-express

v2.10.1

Published

Express middleware for @ampproject/toolbox-optimizer

Downloads

4,033

Readme

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.