1
mirror of https://github.com/jakejarvis/netlify-plugin-cache.git synced 2025-06-27 16:25:40 -04:00

commit the plugin!

This commit is contained in:
2020-06-06 10:15:46 -04:00
parent 3bf03f1c20
commit e2ee0ba0e3
4 changed files with 107 additions and 1 deletions

View File

@ -1,2 +1,47 @@
# netlify-plugin-cache
Generic cache plugin for saving and restoring files and/or folders between Netlify builds
[![npm](https://img.shields.io/npm/v/netlify-plugin-cache?logo=npm&color=red)](https://www.npmjs.com/package/netlify-plugin-cache)
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! ⚡
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
) — it isn't tied to any specific static site generator (on purpose).
## Install
Add the following to your `netlify.toml` configuration:
```toml
[[plugins]]
package = "netlify-plugin-cache"
[plugins.inputs]
paths = ["resources", "_vendor", "folder/file.md"]
```
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**.
Read more about plugin configuration at [the official Netlify Plugin docs](https://docs.netlify.com/configure-builds/build-plugins/#install-a-plugin).
## Usage examples
- **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)
- **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/)
- **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)
- **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!
## Debugging
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!
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.)
```toml
[[plugins]]
package = "netlify-plugin-debug-cache"
```
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)
## Licenses
This project is distributed under the [MIT License](LICENSE).

31
index.js Normal file
View File

@ -0,0 +1,31 @@
// https://github.com/netlify/build/blob/master/packages/cache-utils/README.md
module.exports = {
// try to restore cache before build begins, if it exists
onPreBuild: async ({ utils: { cache }, inputs }) => {
if (await cache.restore(inputs.paths)) {
const files = await cache.list(inputs.paths)
console.log(`Successfully restored: ${inputs.paths.join(', ')} ... ${files.length} files in total.`)
} else {
console.log(`A cache of ${inputs.paths.join(', ')} doesn't exist (yet).`)
}
},
// only save/update cache if build was successful
onSuccess: async ({ utils: { cache, status }, inputs }) => {
if (await cache.save(inputs.paths)) {
const files = await cache.list(inputs.paths)
console.log(`Successfully cached: ${inputs.paths.join(', ')} ... ${files.length} files in total.`)
// show success & more detail in deploy summary
status.show({
title: `${files.length} files cached`,
summary: 'These will be restored on the next build! ⚡',
text: `${inputs.paths.join(', ')}`,
})
} else {
console.log(`Failed caching ${inputs.paths.join(', ')}. :(`)
}
},
}

5
manifest.yml Normal file
View File

@ -0,0 +1,5 @@
name: netlify-plugin-cache
inputs:
- name: paths
description: Array of files and/or directories to cache between builds.
required: true

25
package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "netlify-plugin-cache",
"version": "1.0.0",
"description": "Generic cache plugin for saving and restoring files and/or folders between Netlify builds",
"author": "Jake Jarvis <jake@jarv.is>",
"license": "MIT",
"main": "index.js",
"homepage": "https://github.com/jakejarvis/netlify-plugin-cache#readme",
"bugs": "https://github.com/jakejarvis/netlify-plugin-cache/issues",
"repository": {
"type": "git",
"url": "git+https://github.com/jakejarvis/netlify-plugin-cache.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"netlify",
"netlify-plugin",
"cache",
"ci",
"build",
"plugin"
]
}