# netlify-amp-optimizer-plugin

> A Netlify plugin to run the AMP Optimizer

Latest version **1.0.0** (published 2021-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install netlify-amp-optimizer-plugin
pnpm add netlify-amp-optimizer-plugin
yarn add netlify-amp-optimizer-plugin
bun add netlify-amp-optimizer-plugin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-02-25 |
| First published | 2021-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel Hannih |
| Maintainers | danielhannih |
| Keywords | netlify, netlify-plugin, amp, amp-html, amp-optimizer, amp-toolbox |

## Links

- npm: https://www.npmjs.com/package/netlify-amp-optimizer-plugin
- Repository: https://github.com/danielhannih/netlify-amp-optimizer-plugin
- Homepage: https://github.com/danielhannih/netlify-amp-optimizer-plugin#readme
- Issues: https://github.com/danielhannih/netlify-amp-optimizer-plugin
- npm.io page: https://npm.io/package/netlify-amp-optimizer-plugin

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2021-02-25

## README

# Netlify Plugin: AMP Optimizer
This is a modified Netlify plugin based on [martinbean's AMP Server-Side Rendering Netlify Plugin](https://github.com/martinbean/netlify-plugin-amp-server-side-rendering) to optimize AMP pages.

## Difference to the original plugin
I had issues with the original plugin as it also transformed non-AMP pages, for example the Netlify admin page.

I needed to customize the plugin for my needs, and that's why I made this local plugin.

## Install & usage
The **Netlify AMP Optimizer Plugin** is made to be a local Netlify plugin.

### How to install the Netlify AMP Optimizer plugin
Copy the **plugin** folder and it's contents into your root folder (if you haven't already created a plugin folder).

Then open your **netlify.toml** file at your root folder and add those lines:

```toml
[[plugins]]
  package = "@netlify/plugin-local-install-core"

[[plugins]]
  package = "/plugins/amp-optimizer"
    [plugins.inputs]
    siteName = "Your site name"
```

### Usage
You're all set. But you can also customize the plugin to your needs.

In my use case, AMP Optimizer ignored option that I passed into it. For example, I wanted to turn off *optimizeHeroImages* like this:

```js
const ampOptimizer = require("@ampproject/toolbox-optimizer").create({
  optimizeHeroImages: false
})
```

optimizeHeroImages is great in most use cases, in my case (I put the hero image into a div as a background image), the AMP Optimizer put a preload in the `<head>` that loaded an images that wasn't even visible on page load.

A workaround was to remove the preload tag from the optimized HTML:

```js
const ampOptimizer = require("@ampproject/toolbox-optimizer").create()
const fs = require("fs")
const glob = require("glob")

module.exports = {
  onPostBuild: async ({ constants, utils, inputs }) => {
    const pattern = constants.PUBLISH_DIR + "/**/*.html"
    const files = await new Promise((resolve, reject) => {
      glob(pattern, { nodir: true }, (err, files) => {
        (err) ? reject(err) : resolve(files)
      })
    })
    let filesLength = 0
    await Promise.all(
      files.map(async (file) => {
        const htmlFile = await fs.promises.readFile(file, "utf-8")
        if (htmlFile.toString().substring(0,30).includes("⚡")) {
          ++filesLength
          let optimizedHtml = await ampOptimizer.transformHtml(htmlFile)

          // regex to find preload tag
          const removeDataHero = /<link rel="preload" href="https:..res.cloudinary.*data-hero>/gmi
          
          await fs.promises.writeFile(file, optimizedHtml.toString().replace(removeDataHero,''))

        }
      })
    )
    utils.status.show({
      title: `${inputs.siteName} is on 🔥`,
      summary: `AMP Optimizer just made ${filesLength} out of ${files.length} HTML files smaller, faster & better! 🚀`
    })
  }
}

```

---
_Source: https://npm.io/package/netlify-amp-optimizer-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
