├── .gitignore ├── README.md ├── index.js ├── manifest.yml ├── package.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Netlify Build Plugin: Persist the Gatsby Cache Between Builds 2 | 3 | Persist the Gatsby cache between Netlify builds for huge build speed improvements! ⚡️ 4 | 5 | > **Note:** The functionality in this plugin is now available in the [Essential Gatsby build plugin](https://github.com/netlify/netlify-plugin-gatsby). Check out Essential Gatsby for additional framework-specific functionality on Netlify, including Gatsby Functions. If you choose to install it, you should remove this plugin first. 6 | 7 | ## Usage 8 | 9 | Currently, there are **two ways to install this plugin on your application**: 10 | 11 | ### Installing from Netlify UI 12 | 13 | #### [One-click install to add this to your Gatsby site](http://app.netlify.com/plugins/netlify-plugin-gatsby-cache/install?utm_source=github&utm_medium=gatsby-cache-bp-jl&utm_campaign=devex) 14 | 15 | ### Installing from your project's code 16 | 17 | You can also install it manually using `netlify.toml`. If you want to know more about file-based configuration on Netlify, click [here](https://docs.netlify.com/configure-builds/file-based-configuration/). 18 | 19 | Add the following lines to your project's `netlify.toml` file: 20 | 21 | ```toml 22 | [build] 23 | publish = "public" 24 | 25 | [[plugins]] 26 | package = "netlify-plugin-gatsby-cache" 27 | ``` 28 | 29 | > Note: The `[[plugins]]` line is required for each plugin, even if you have other plugins in your `netlify.toml` file already. 30 | 31 | ## How does it work? 32 | 33 | This plugin determines the location of your `.cache` folder by looking at the `publish` folder configured for Netlify deployment. This means that if your Gatsby site successfully deploys, it will be cached as well with no config required! 🎉 34 | 35 | ## How much of a difference does this plugin make in build times? 36 | 37 | Each Gatsby site is different, so build times vary widely between them, but one common slowdown in Gatsby builds is processing and transforming images. Gatsby is smart enough to check if these transformations have already been done and skip them, but in order to get that benefit in a build pipeline (e.g. Netlify) the `public` and `.cache` directories need to be preserved between builds. That’s what this plugin does! 38 | 39 | | | No Cache | Cache | Savings | 40 | |------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|---------| 41 | | * 231 GraphQL queries
* 1,871 images
* 224 pages | 293207ms ([build log](https://app.netlify.com/sites/lengstorf/deploys/5dceed27d58a580008daaccc)) | 72835ms ([build log](https://app.netlify.com/sites/lengstorf/deploys/5dcef2463da4810008d48aaa)) | 75% | 42 | | * 5 GraphQL queries
* No image processing
* 32 pages | 22072ms ([build log](https://app.netlify.com/sites/build-plugin-test/deploys/5dceed49e746a200091c76fe)) | 15543ms ([build log](https://app.netlify.com/sites/build-plugin-test/deploys/5dceedbfad95d0000bcd46d1)) | 30% | 43 | 44 | tl;dr: Repeat builds with lots of images will be _much_ faster. With few or no images, the difference will be there, but it won’t be as pronounced. 45 | 46 | ## Want to learn how to create your own Netlify Build Plugins? 47 | 48 | Check out [Sarah Drasner’s excellent tutorial](https://www.netlify.com/blog/2019/10/16/creating-and-using-your-first-netlify-build-plugin/?utm_source=github&utm_medium=netlify-plugin-gatsby-cache-jl&utm_campaign=devex)! 49 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | const getCacheDirs = (constants) => [ 4 | constants.PUBLISH_DIR, 5 | path.normalize(`${constants.PUBLISH_DIR}/../.cache`), 6 | ]; 7 | 8 | module.exports = { 9 | async onPreBuild({ constants, utils }) { 10 | // print a helpful message if the publish dir is misconfigured 11 | if (process.cwd() === constants.PUBLISH_DIR) { 12 | utils.build.failBuild( 13 | `Gatsby sites must publish the public directory, but your site’s publish directory is set to “${constants.PUBLISH_DIR}”. Please set your publish directory to your Gatsby site’s public directory.`, 14 | ); 15 | } 16 | 17 | const cacheDirs = getCacheDirs(constants); 18 | 19 | if (await utils.cache.restore(cacheDirs)) { 20 | console.log('Found a Gatsby cache. We’re about to go FAST. ⚡️'); 21 | } else { 22 | console.log('No Gatsby cache found. Building fresh.'); 23 | } 24 | }, 25 | async onPostBuild({ constants, utils }) { 26 | const cacheDirs = getCacheDirs(constants); 27 | 28 | if (await utils.cache.save(cacheDirs)) { 29 | console.log('Stored the Gatsby cache to speed up future builds.'); 30 | } else { 31 | console.log('No Gatsby build found.'); 32 | } 33 | }, 34 | }; 35 | -------------------------------------------------------------------------------- /manifest.yml: -------------------------------------------------------------------------------- 1 | name: netlify-plugin-gatsby-cache 2 | inputs: [] 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "netlify-plugin-gatsby-cache", 3 | "version": "0.3.2", 4 | "description": "Persist the Gatsby cache between Netlify builds for huge build speed improvements!", 5 | "main": "index.js", 6 | "repository": "https://github.com/jlengstorf/netlify-plugin-gatsby-cache.git", 7 | "bugs": { 8 | "url": "https://github.com/jlengstorf/netlify-plugin-gatsby-cache/issues" 9 | }, 10 | "author": "Jason Lengstorf (https://lengstorf.com)", 11 | "license": "MIT", 12 | "keywords": [ 13 | "netlify", 14 | "netlify-plugin" 15 | ], 16 | "dependencies": {} 17 | } 18 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | --------------------------------------------------------------------------------