Skip to content

Commit e2ee0ba

Browse files
committed
commit the plugin!
1 parent 3bf03f1 commit e2ee0ba

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
11
# netlify-plugin-cache
2-
Generic cache plugin for saving and restoring files and/or folders between Netlify builds
2+
3+
[![npm](https://img.shields.io/npm/v/netlify-plugin-cache?logo=npm&color=red)](https://www.npmjs.com/package/netlify-plugin-cache)
4+
5+
A generic cache plugin for saving and restoring files and/or folders between Netlify builds for impressive speed improvements. Worry less about running out of build minutes! ⚡
6+
7+
In other words, this plugin is a pretty wrapper around [Netlify's native cache utility](https://github.com/netlify/build/blob/master/packages/cache-utils/README.md
8+
) — it isn't tied to any specific static site generator (on purpose).
9+
10+
## Install
11+
12+
Add the following to your `netlify.toml` configuration:
13+
14+
```toml
15+
[[plugins]]
16+
package = "netlify-plugin-cache"
17+
[plugins.inputs]
18+
paths = ["resources", "_vendor", "folder/file.md"]
19+
```
20+
21+
This plugin only takes one input (which is required) named `paths`, an array of paths to files and/or folders relative to your project's root. These files/folders are restored before a build and saved in cache after a build **if it is successful**.
22+
23+
Read more about plugin configuration at [the official Netlify Plugin docs](https://docs.netlify.com/configure-builds/build-plugins/#install-a-plugin).
24+
25+
## Usage examples
26+
27+
- **Hugo:** Caching the `resources` directory can speed up your build greatly if you [process](https://gohugo.io/content-management/image-processing/) a lot of images via Hugo pipes. You can also cache the `public` directory to avoid completely rebuilding the entire site on each deploy. [More info here.](https://gohugo.io/getting-started/directory-structure/#directory-structure-explained)
28+
- **Gatsby:** By default, the `.cache` directory holds persistent data between builds. You can also cache the `dist` directory to avoid completely rebuilding the entire site on each deploy. [More info here.](https://www.gatsbyjs.org/docs/build-caching/)
29+
- **Next.js:** The `.next` directory holds the build output. [More info here.](https://nextjs.org/docs/api-reference/next.config.js/setting-a-custom-build-directory)
30+
- **Anything else:** This is the reason I kept this plugin as generic as possible! Research the caching behavior of your static site generator (and how to customize it if necessary). Feel free to make a PR and add it here as another example, too!
31+
32+
## Debugging
33+
34+
This plugin doesn't provide a way to output a list of files that were cached or restored, because Netlify already provides an official plugin named [`netlify-plugin-debug-cache`](https://github.com/netlify-labs/netlify-plugin-debug-cache) to do exactly that. No need to re-invent the wheel!
35+
36+
You can add their debug plugin **after** this plugin in your `netlify.toml`. (And yes, you need a `[[plugins]]` line for *each* plugin you add.)
37+
38+
```toml
39+
[[plugins]]
40+
package = "netlify-plugin-debug-cache"
41+
```
42+
43+
The plugin will generate a file named `cache-output.json` at the root of your project's publish directory. [Learn more about this plugin here.](https://github.com/netlify-labs/netlify-plugin-debug-cache)
44+
45+
## Licenses
46+
47+
This project is distributed under the [MIT License](LICENSE).

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://github.com/netlify/build/blob/master/packages/cache-utils/README.md
2+
3+
module.exports = {
4+
// try to restore cache before build begins, if it exists
5+
onPreBuild: async ({ utils: { cache }, inputs }) => {
6+
if (await cache.restore(inputs.paths)) {
7+
const files = await cache.list(inputs.paths)
8+
console.log(`Successfully restored: ${inputs.paths.join(', ')} ... ${files.length} files in total.`)
9+
} else {
10+
console.log(`A cache of ${inputs.paths.join(', ')} doesn't exist (yet).`)
11+
}
12+
},
13+
14+
// only save/update cache if build was successful
15+
onSuccess: async ({ utils: { cache, status }, inputs }) => {
16+
if (await cache.save(inputs.paths)) {
17+
const files = await cache.list(inputs.paths)
18+
console.log(`Successfully cached: ${inputs.paths.join(', ')} ... ${files.length} files in total.`)
19+
20+
// show success & more detail in deploy summary
21+
status.show({
22+
title: `${files.length} files cached`,
23+
summary: 'These will be restored on the next build! ⚡',
24+
text: `${inputs.paths.join(', ')}`,
25+
})
26+
} else {
27+
console.log(`Failed caching ${inputs.paths.join(', ')}. :(`)
28+
}
29+
},
30+
}
31+

manifest.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: netlify-plugin-cache
2+
inputs:
3+
- name: paths
4+
description: Array of files and/or directories to cache between builds.
5+
required: true

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "netlify-plugin-cache",
3+
"version": "1.0.0",
4+
"description": "Generic cache plugin for saving and restoring files and/or folders between Netlify builds",
5+
"author": "Jake Jarvis <[email protected]>",
6+
"license": "MIT",
7+
"main": "index.js",
8+
"homepage": "https://github.com/jakejarvis/netlify-plugin-cache#readme",
9+
"bugs": "https://github.com/jakejarvis/netlify-plugin-cache/issues",
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/jakejarvis/netlify-plugin-cache.git"
13+
},
14+
"scripts": {
15+
"test": "echo \"Error: no test specified\" && exit 1"
16+
},
17+
"keywords": [
18+
"netlify",
19+
"netlify-plugin",
20+
"cache",
21+
"ci",
22+
"build",
23+
"plugin"
24+
]
25+
}

0 commit comments

Comments
 (0)