├── .gitignore ├── README.md ├── manifest.yml ├── netlify.toml ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | package-lock.json 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AMP Server-Side Rendering Netlify Plugin 2 | This [Netlify Build Plugin](https://docs.netlify.com/configure-builds/build-plugins/) does server-side rendering of your [AMP][1]-powered website. 3 | 4 | ## Usage 5 | You can install this plugin in the Netlify UI from this [direct in-app installation link](https://app.netlify.com/plugins/netlify-plugin-amp-server-side-rendering/install) or from the [Plugins directory](https://app.netlify.com/plugins). 6 | 7 | For file-based installation, add the following lines to your **netlify.toml** file: 8 | 9 | ```toml 10 | [[plugins]] 11 | package = "netlify-plugin-amp-server-side-rendering" 12 | ``` 13 | 14 | To complete file-based installation, from your project's base directory, use npm, yarn, or any other Node.js package manager to add the plugin to `devDependencies` in `package.json`. 15 | 16 | ``` 17 | npm install -D netlify-plugin-amp-server-side-rendering 18 | ``` 19 | 20 | [1]: https://amp.dev/ 21 | -------------------------------------------------------------------------------- /manifest.yml: -------------------------------------------------------------------------------- 1 | name: netlify-plugin-amp-server-side-rendering 2 | -------------------------------------------------------------------------------- /netlify.toml: -------------------------------------------------------------------------------- 1 | [[plugins]] 2 | package = "./src/index.js" 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "netlify-plugin-amp-server-side-rendering", 3 | "version": "1.0.3", 4 | "description": "Render your AMP pages, server-side.", 5 | "keywords": [ 6 | "netlify", 7 | "netlify-plugin", 8 | "amp", 9 | "amp-html" 10 | ], 11 | "bugs": { 12 | "url": "https://github.com/martinbean/netlify-plugin-amp-server-side-rendering" 13 | }, 14 | "license": "MIT", 15 | "author": { 16 | "name": "Martin Bean", 17 | "url": "https://martinbean.dev" 18 | }, 19 | "files": [ 20 | "manifest.yml", 21 | "src/index.js" 22 | ], 23 | "main": "src/index.js", 24 | "repository": { 25 | "type": "git", 26 | "url": "git+https://github.com/martinbean/netlify-plugin-amp-server-side-rendering.git" 27 | }, 28 | "dependencies": { 29 | "@ampproject/toolbox-optimizer": "^2.5.4", 30 | "glob": "^7.1.6" 31 | }, 32 | "engines": { 33 | "node": ">=10.0" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const ampOptimizer = require('@ampproject/toolbox-optimizer').create(); 2 | const fs = require('fs'); 3 | const glob = require('glob'); 4 | 5 | module.exports = { 6 | onPostBuild: async ({ constants, utils }) => { 7 | const pattern = constants.PUBLISH_DIR + '/**/*.html'; 8 | 9 | const files = await new Promise((resolve, reject) => { 10 | glob(pattern, { nodir: true }, (err, files) => { 11 | (err) ? reject(err) : resolve(files); 12 | }); 13 | }); 14 | 15 | await Promise.all( 16 | files.map(async (file) => { 17 | const html = await fs.promises.readFile(file, 'utf-8'); 18 | const optimizedHtml = await ampOptimizer.transformHtml(html); 19 | await fs.promises.writeFile(file, optimizedHtml); 20 | }) 21 | ); 22 | 23 | utils.status.show({ 24 | title: 'AMP Server-Side Rendering', 25 | summary: `Successfully rendered ${files.length} file(s).` 26 | }); 27 | } 28 | }; 29 | --------------------------------------------------------------------------------