{"bugs":{"url":"https://github.com/ampproject/amp-toolbox/issues"},"dist":{"shasum":"6249a6f3623378e930d88af94e1ed9a44a15fc39","tarball":"https://registry.npmjs.org/@ampproject/toolbox-optimizer/-/toolbox-optimizer-1.0.0-beta.7.tgz","fileCount":25,"integrity":"sha512-VFqxiyshR8yabB9OMh4dATxyRVmIXMrXw4tizW5k8Wq4Ie7aqDSpPoJh38fdjjj4xBm+yHS7L1nW1Nu44D2U2Q==","signatures":[{"sig":"MEYCIQCJ95OctqcxIhTYWpp8Y9vnOMpV5H+OIguNhoDflq1eYgIhAKevDGQy1FcyvbnvuE1Ukeukl9NKNDAv7b0RhECh31Ex","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":94339,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdOGJqCRA9TVsSAnZWagAA+f0P/if0FsTUihOigZNzMtUz\n9P5qV2W9dXvhpiNAxYfTJHxcBe97NtCcJMaYYJOpfKC8MMyNtRTR3MibjvSy\niJpSZ29KnPSRurXN4DQAqSCG/E5epAVqWvL59qwqZFv0zaVdw7dC84WTU60H\njY0A0U93iOHP1q3hnn1x7ga3u5FFeR1bUkodU+ODaF+78Hgzu4iXUwTerZt7\nnQ9IjmSTygOuxV9AwiicEbFSqqL3d0OOC3OH2ftMggmvzKG0yNdfQR+EfJic\nZAleY2U6UHBWo2dwr2xKyI4iWw7g27d0LAPEL2gHTs5gbMJc3/Kh4Sfq6yrY\nSQR8sn8jQEfqFGUbMGj0RMToOjMDGpMfFuOPWE/2K3JGTPDooZNUSuUevxqb\nKZEreRSUqkLIKvr/t+80YOfHuMOv3LKpdmLEqsUIs36jutNe6hL1QGPB4q9d\n8WMdnFnvDFNPjQgMsbMHNzZmkAwgmAyzjcRYGRUAPQgFCsn7kM53AxAxjoWg\n1BTQAkV4ZHHmUBTOS3bnBDnbZaRp+AqaLQriIs9dm3ZGS7M4mHYtEb79QBga\n5GDOJmHqmtUFKZIKRVbpL+lQYvb+vPP18dWwVC3y+E+mPH8SS7vkfgIUr/9A\nh8i7Tsb0DvCHUroW0ISvF9OrB4MlkpsRTMCkRdHLFw+vngBbxGHl72MxP9o6\nCqW9\r\n=1JRC\r\n-----END PGP SIGNATURE-----\r\n"},"main":"index.js","name":"@ampproject/toolbox-optimizer","author":{"name":"AMPHTML Team"},"readme":"# AMP Optimizer \n\n[![npm version](https://badge.fury.io/js/@ampproject/toolbox-optimizer.svg)](https://badge.fury.io/js/@ampproject/toolbox-optimizer)\n\nAMP Optimizer is a tool to server-side enhance the rendering performance of AMP pages. AMP Optimizer implements [AMP performance best practices](https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/optimize_amp?format=websites) and supports [AMP server-side-rendering](https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/server-side-rendering?format=websites). By default, it will perform the following optimizations:\n\n* Server-side render AMP layouts.\n* Remove the AMP boilerplate (when possible).\n* Inline critical CSS.\n* Speed-up AMP framework and custom font loading.\n* Move CSS keyframe animations to the bottom of the page.\n\nThe performance optimizations can improve page rendering times by up to 50%. You can read more about the potential performance gains in this [blog post](https://blog.amp.dev/2018/10/08/how-to-make-amp-even-faster/).\n\n**Important: AMP Optimizer will produce valid AMP, if the input is valid AMP.**\n\n* [Usage](#usage)\n* [Best Practices](#best-practices)\n  + [Regenerate pages at least once a week](#regenerate-pages-at-least-once-a-week)\n  + [Debugging](#debugging)\n  + [Transform AMP pages at build time if possible](#transform-amp-pages-at-build-time-if-possible)\n  + [Cache transformed AMPs at runtime](#cache-transformed-amps-at-runtime)\n* [Experimental Features](#experimental-features)\n  + [Paired AMP](#paired-amp)\n  + [Versioned AMP Runtime](#versioned-amp-runtime)\n  + [Blurry image placeholders](#blurry-image-placeholders)\n  + [Self-hosted AMP components](#self-hosted-amp-components)\n\n## Usage\n\nInstall via:\n\n```\nnpm install @ampproject/toolbox-optimizer@canary\n```\n\nMinimal usage:\n\n```js\nconst AmpOptimizer = require('@ampproject/toolbox-optimizer');\n\nconst ampOptimizer = AmpOptimizer.create();\n\nconst originalHtml = `\n<!doctype html>\n<html ⚡>\n  ...\n</html>\n`\n\nampOptimizer.transformHtml(originalHtml).then(optimizedHtml => {\n  console.log(optimizedHtml);\n});\n\n```\n\nYou can find a sample implementation [here](demo/simple/). If you're using express to serve your site, you can use the [AMP Optimizer Middleware](../optimizer-express).\n\nThere's also a [command line version](../cli/README.md) available:\n\n```shell\n$ npx @ampproject/toolbox-cli myFile.html\n```\n\n## Best Practices\n\n### Regenerate pages at least once a week\n\nAMP Optimizer inlines CSS styles required by AMP. To make sure, that the inlined CSS stays in sync with the latest AMP release, we recommend to re-generate pages at least once a week. The good news is, out-of-sync CSS will not break your page, as AMP will check the version of the inlined CSS at runtime and will automatically update it to the latest version.\n\n### Debugging\n\nEnable `verbose` mode to find out why the AMP boilerplate is not being removed. You can\nenable verbose mode either when creating a new optimizer instance:\n\n```\n// globally\nconst optimizer = ampOptimizer.create({\n  verbose: true\n});\n```\n\n... or for individual pages:\n\n```\n// per transformation\nampOptimizer.transformHtml(originalHtml, {\n  verbose: true\n})\n```\n\n### Transform AMP pages at build time if possible\n\nApplying the transformations to an AMP file consumes additional server resources. Also, since the entire file is needed to apply the transformations, it also becomes impossible to stream the response while applying it. In order to avoid server overhead, if the set of AMP files to be transformed is known in advance, transformations should be run at build time.\n\n### Cache transformed AMPs at runtime\n\nMost websites have a more dynamic nature though and are not able to apply the transformations statically. For such cases it is possible to run the transformations after AMP pages are rendered, e.g. in an Express middleware. In that case, to achieve best performance, it's best to cache transformed pages for subsequent requests. Caching can take place on the CDN level, on the site's internal infrastructure (eg: Memcached), or even on the server itself, if the set of pages is small enough to fit in memory.\n\n## Experimental Features\n\n**Warning: these features are experimental and might result in invalid AMP pages.**\n\n### Paired AMP \n\nWhen using experimental features resulting in invalid AMP it's best to setup paired AMP mode. Paired AMP mode will add `<link rel=amphtml href=${ampUrl}>` to the transformed page, were `ampUrl` needs to point to the valid version of this page.\n\nExample:\n\n```\nconst optimizer = AmpOptimizer.create({\n  transformations: AmpOptimizer.TRANSFORMATIONS_PAIRED_AMP,\n});\nconst ampFilePath = filePath.substring(1, filePath.length)\n    .replace('.html', '.amp.html');\nconst transformedHtml = await optimizer.transformHtml(html, {\n  // needed to calculate the `<link rel=amphtml href=${ampUrl}>`\n  ampUrl: ampFilePath,\n});\n```\n\n### Versioned AMP Runtime\n\nThe `ampRuntimeVersion` parameter will rewrite all AMP runtime and extension imports to the specified version. For example: \n\n```\nhttps://cdn.ampproject.org/v0.js\n```\n\nwill be replaced with:\n\n```\nhttps://cdn.ampproject.org/rtv/001515617716922/v0.js\n```\n\nVersioning the AMP runtime URLs has one main benefit: versioned AMP runtime URLs are served with a longer max-age than the unversioned ones. This means AMP pages served with versioned AMP runtime benefit from better browser caching.\n\n**Important:** when using versioned AMP runtime URLs make sure to invalidate all caches whenever a new AMP runtime is released. This is to ensure that your AMP pages always use the latest version of the AMP runtime.  \n\nYou can use [@ampproject/toolbox-runtime-version](../@ampproject/toolbox-runtime-version) to retrieve the latest version of the AMP runtime. Here is a sample to apply the optimizations including versioning the URLs:\n\n```\nconst ampOptimizer = require('@ampproject/toolbox-optimizer');\nconst ampRuntimeVersion = await runtimeVersion.currentVersion();\n\n// The input string\nconst originalHtml = `\n<!doctype html>\n<html ⚡>\n...\n`\n\n// Additional options can be passed as the second argument\nconst optimizedHtml = await ampOptimizer.transformHtml(originalHtml, {\n  ampUrl: 'canonical.amp.html',\n  ampRuntimeVersion: ampRuntimeVersion\n});\n\nconsole.log(optimizedHtml);\n```\n\n### Blurry image placeholders\n\nAdd placeholders for `amp-img` and `amp-video` posters. The placeholders are blurry versions of the corresponding original source. The blur will be displayed as the `<amp-img>` is rendering, and will fade out once the element is loaded. The current requirements of appending a blurry placeholder is for the element is to be a JPEG that is either responsive or a poster for an `amp-video`.\n\n**Important: blurry image placeholder computation is computationally expensive. Make sure to only use it for static or cached pages.**\n\nThis transformer supports the following options:\n\n* `blurredPlaceholders`: Enables blurry image placeholder generation. Default is `false`.\n* `imageBasePath`: specifies a base path used to resolve an image during build.\n* `maxBlurredPlaceholders`: Specifies the max number of blurred images. Defaults to 5.\n* `blurredPlaceholdersCacheSize`: Specifies the max number of blurred images to be cached\n  to avoid expensive recalculation. Set to 0 if caching should be disabled. Set to -1 if\n  all placeholders should be cached (good for static sites). Defaults to 30.\n\nUsage: \n\n```\nconst optimizer = AmpOptimizer.create({\n  // blurry image placeholders are currently not considered valid AMP\n  // hence it's recommended to setup paired AMP mode when enabling this feature.\n  transformations: AmpOptimizer.TRANSFORMATIONS_PAIRED_AMP,\n  blurredPlaceholders: true,\n});\n```\n\n### Self-hosted AMP components\n\nIt's possible to rewrite the AMP framework and component imports to a different domain than `cdn.ampproject.org`.\n\nExample:\n```\nconst ampOptimizer = require('@ampproject/toolbox-optimizer');\n\n// The input string\nconst originalHtml = `\n<!doctype html>\n<html ⚡>\n...\n`\n\n// Additional options can be passed as the second argument\nconst optimizedHtml = await ampOptimizer.transformHtml(originalHtml, {\n  ampUrl: 'canonical.amp.html',\n  // this will rewrite https://cdn.ampproject.org/v0.js to /amp/v0.js\n  ampUrlPrefix: '/amp'\n});\n\nconsole.log(optimizedHtml);\n```\n","gitHead":"386bd4636da2dfd92569a6f0c0ede7bd2b49580c","license":"Apache-2.0","_npmUser":{"name":"amp-toolbox","email":"amp-toolbox-eng@google.com"},"homepage":"https://github.com/ampproject/amp-toolbox/tree/master/packages/optimizer","keywords":["amp"],"repository":{"url":"git+https://github.com/ampproject/amp-toolbox.git","type":"git"},"_npmVersion":"lerna/3.16.0/node@v10.16.0+x64 (darwin)","description":"Server-side rendering for AMPs.","directories":{},"maintainers":[{"name":"amp-toolbox","email":"amp-toolbox-eng@google.com"},{"name":"ampproject-admin","email":"admin@ampproject.org"},{"name":"choumx","email":"willchou@google.com"},{"name":"jridgewell","email":"justin+npm@ridgewell.name"},{"name":"kristoferbaxter","email":"kbax@me.com"},{"name":"prateekbh","email":"prateek89born@gmail.com"},{"name":"sepandparhami","email":"sparhami@google.com"}],"_nodeVersion":"10.16.0","dependencies":{"css":"2.2.4","parse5":"5.1.0","@ampproject/toolbox-core":"^1.0.0-beta.7","parse5-htmlparser2-tree-adapter":"5.1.0","@ampproject/toolbox-runtime-version":"^1.0.0-beta.7"},"_hasShrinkwrap":false,"readmeFilename":"README.md","peerDependenciesMeta":{"jimp":{"optional":true},"lru-cache":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/toolbox-optimizer_1.0.0-beta.7_1563976298393_0.33840699362621374","host":"s3://npm-registry-packages"},"_id":"@ampproject/toolbox-optimizer@1.0.0-beta.7","version":"1.0.0-beta.7"}