From fd06f71df454d4d6f5a18a6444069f44c3bded34 Mon Sep 17 00:00:00 2001 From: Jake Jarvis Date: Fri, 5 Jun 2020 19:47:27 -0400 Subject: [PATCH] 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] --- netlify.toml | 13 ++++---- package.json | 6 ++-- plugins/netlify-plugin-cache/index.js | 39 ++++++++++++----------- plugins/netlify-plugin-cache/manifest.yml | 3 +- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/netlify.toml b/netlify.toml index 8f5db70f..a99d88a4 100644 --- a/netlify.toml +++ b/netlify.toml @@ -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. diff --git a/package.json b/package.json index 3b3dc09c..c166921d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/plugins/netlify-plugin-cache/index.js b/plugins/netlify-plugin-cache/index.js index 81f337b3..95bf7f39 100644 --- a/plugins/netlify-plugin-cache/index.js +++ b/plugins/netlify-plugin-cache/index.js @@ -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(', ')}. :(`) } - } -}; + }, +} diff --git a/plugins/netlify-plugin-cache/manifest.yml b/plugins/netlify-plugin-cache/manifest.yml index 793f5aff..cc609f13 100644 --- a/plugins/netlify-plugin-cache/manifest.yml +++ b/plugins/netlify-plugin-cache/manifest.yml @@ -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