1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-19 04:55:30 -04:00

cleaning up the netlify plugin

didn't actually try just passing an array of dirs to Netlify plugin, and now only saving cache if build successful

[skip ci]
This commit is contained in:
2020-06-05 19:47:27 -04:00
parent eafbf87f49
commit fd06f71df4
4 changed files with 32 additions and 29 deletions

View File

@@ -39,14 +39,13 @@
# Cache resoures between builds, see plugins/netlify-plugin-cache/index.js
[[plugins]]
package = "/plugins/netlify-plugin-cache"
package = "./plugins/netlify-plugin-cache"
[plugins.inputs]
# TODO: make this better.
paths = "[\"resources\", \"_vendor\"]"
[[plugins]]
package = "netlify-plugin-debug-cache"
[[plugins]]
package = "@netlify/plugin-local-install-core"
paths = ["resources", "_vendor"]
# [[plugins]]
# package = "netlify-plugin-debug-cache"
# [[plugins]]
# package = "@netlify/plugin-local-install-core"
# The most important headers and redirects are specified in the _headers and
# _redirects files generated by Hugo. These are additional custom rules.

View File

@@ -14,9 +14,9 @@
"url": "git+https://github.com/jakejarvis/jarv.is.git"
},
"scripts": {
"clean": "rimraf public/ builds/",
"build": "run-s clean hugo minify",
"build:preview": "run-s clean hugo:dev",
"clean": "rimraf public/ resources/ builds/ && hugo mod clean",
"build": "run-s hugo minify",
"build:preview": "run-s hugo:dev",
"hugo": "hugo --gc --cleanDestinationDir --verbose",
"hugo:dev": "hugo --environment development --baseURL ${DEPLOY_PRIME_URL:-/} --buildDrafts --buildFuture --gc --cleanDestinationDir --verbose",
"start": "hugo server --disableFastRender --buildDrafts --buildFuture --port 1337 --bind 0.0.0.0 --verbose",

View File

@@ -1,27 +1,30 @@
// Based on https://github.com/cdeleeuwe/netlify-plugin-hugo-cache-resources/blob/master/index.js
// https://github.com/netlify/build/blob/master/packages/cache-utils/README.md
module.exports = {
async onPreBuild({ utils, inputs }) {
const paths = JSON.parse(inputs.paths);
const success = await utils.cache.restore(paths);
if (success) {
const cachedFiles = await utils.cache.list(paths);
console.log(`Restored cache of '${paths}', totaling ${cachedFiles.length} files.`);
// 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 '${paths}' doesn't exist (yet).`);
console.log(`A cache of ${inputs.paths.join(', ')} doesn't exist (yet).`)
}
},
async onPostBuild({ utils, inputs }) {
const paths = JSON.parse(inputs.paths);
const success = await utils.cache.save(paths);
// 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.`)
if (success) {
const cachedFiles = await utils.cache.list(paths);
console.log(`Successfully cached '${paths}', totaling ${cachedFiles.length} files.`);
// 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(`Couldn't cache '${paths}'.`);
console.log(`Failed caching ${inputs.paths.join(', ')}. :(`)
}
}
};
},
}

View File

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