├── .gitignore
├── README.md
├── index.js
├── manifest.yml
├── package-lock.json
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Netlify Build Plugin: Persist the Gridsome Cache Between Builds
2 |
3 | Persist the Gridsome cache between Netlify builds for huge build speed improvements! ⚡️
4 |
5 | What are Netlify build plugins? Check out [the announcement page on Netlify](https://www.netlify.com/products/build/plugins/).
6 |
7 | ### [One-click install to add this to your Gridsome site](https://app.netlify.com/plugins/netlify-plugin-gridsome-cache/install)
8 |
9 | ## Usage
10 |
11 | If you don’t want to use the [UI-based installation](https://app.netlify.com/plugins/netlify-plugin-gridsome-cache/install), you can install manually using `netlify.toml`.
12 |
13 | Add the following lines to your `netlify.toml` file:
14 |
15 | ```toml
16 | [build]
17 | publish = "dist"
18 |
19 | [[plugins]]
20 | package = "netlify-plugin-gridsome-cache"
21 | ```
22 |
23 | Note: The `[[plugins]]` line is required for each plugin, even if you have other plugins in your `netlify.toml` file already.
24 |
25 | This plugin determines the location of your `.cache` folder by looking at the `publish` folder configured for Netlify deployment (this _must_ be set in your `netlify.toml` in the `[build]` section). This means that if your Gridsome site successfully deploys, it will be cached as well with no config required! 🎉
26 |
27 | ## How much of a difference does this plugin make in build times?
28 |
29 | Each Gridsome site is different, so build times vary widely between them, but one common slowdown in Gridsome builds is processing and transforming images. Gridsome 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 `dist` and `.cache` directories need to be preserved between builds. That’s what this plugin does!
30 |
31 | | | No Cache | Cache | Savings |
32 | | ----------------------------------------------------- | -------- | ----- | ------- |
33 | | _ 231 GraphQL queries
_ 295 images
\* 295 pages | 27.5s | 22.4s | 22% |
34 |
35 | 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.
36 |
37 | \*note: the above differences are cited from [my blog's repo](https://github.com/edm00se/blog); if you would like to submit a PR to add additional application size/savings data, I will be happy to accept it
38 |
39 | ## Want to learn how to create your own Netlify Build Plugins?
40 |
41 | 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)!
42 |
43 | ## Credits
44 |
45 | Thank you to @jlengstorf whose work this repo is forked from; [jlengstorf/netlify-plugin-gatsby-cache](https://github.com/jlengstorf/netlify-plugin-gatsby-cache).
46 |
47 | ## License
48 |
49 | MIT
50 |
--------------------------------------------------------------------------------
/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 | `Gridsome sites must publish the dist directory, but your site’s publish directory is set to “${constants.PUBLISH_DIR}”. Please set your publish directory to your Gridsome site’s dist directory.`,
14 | );
15 | }
16 |
17 | const cacheDirs = getCacheDirs(constants);
18 |
19 | if (await utils.cache.restore(cacheDirs)) {
20 | console.log('Found a Gridsome cache. We’re about to go FAST. ⚡️');
21 | } else {
22 | console.log('No Gridsome 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 Gridsome cache to speed up future builds.');
30 | } else {
31 | console.log('No Gridsome build found.');
32 | }
33 | },
34 | };
35 |
--------------------------------------------------------------------------------
/manifest.yml:
--------------------------------------------------------------------------------
1 | name: netlify-plugin-gridsome-cache
2 | inputs: []
3 |
--------------------------------------------------------------------------------
/package-lock.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "netlify-plugin-gridsome-cache",
3 | "version": "1.0.3",
4 | "lockfileVersion": 1
5 | }
6 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "netlify-plugin-gridsome-cache",
3 | "version": "1.0.3",
4 | "description": "Persist the Gridsome cache between Netlify builds for huge build speed improvements!",
5 | "main": "index.js",
6 | "repository": "https://github.com/edm00se/netlify-plugin-gridsome-cache.git",
7 | "bugs": {
8 | "url": "https://github.com/edm00se/netlify-plugin-gridsome-cache/issues"
9 | },
10 | "author": "Eric McCormick (https://edm00se.codes/)",
11 | "license": "MIT",
12 | "keywords": [
13 | "netlify",
14 | "netlify-plugin"
15 | ],
16 | "dependencies": {}
17 | }
18 |
--------------------------------------------------------------------------------