From c597c208a819e42d05f9e07c9b336790156c5756 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Thu, 17 Sep 2020 22:21:14 +0200 Subject: [PATCH 1/3] ci(netlify): add resources cache Modified https://github.com/cdeleeuwe/netlify-plugin-hugo-cache-resources, set cache dir to `exampleSite/resources` --- .../README.md | 27 +++++++++++++ .../index.js | 39 +++++++++++++++++++ .../manifest.yml | 5 +++ netlify.toml | 5 ++- 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .netlify/plugins/netlify-plugin-hugo-cache-resources/README.md create mode 100644 .netlify/plugins/netlify-plugin-hugo-cache-resources/index.js create mode 100644 .netlify/plugins/netlify-plugin-hugo-cache-resources/manifest.yml diff --git a/.netlify/plugins/netlify-plugin-hugo-cache-resources/README.md b/.netlify/plugins/netlify-plugin-hugo-cache-resources/README.md new file mode 100644 index 0000000..a7c1ef2 --- /dev/null +++ b/.netlify/plugins/netlify-plugin-hugo-cache-resources/README.md @@ -0,0 +1,27 @@ +> Original Repo: https://github.com/cdeleeuwe/netlify-plugin-hugo-cache-resources +> +# Netlify Build Plugin: Persist Hugo resources Between Builds + +Persist [Hugo](https://gohugo.io/) resources folder between Netlify builds for huge build speed improvements! ⚡️ + +This plugin caches the `resources` folder after build. If you are processing many images, this would improve build duration significantly. + +Note: Restoring cache only comes from the production branch. So once cache is saved on the production branch, the next preview build would use the cache. For example, when pushing to the same preview build, the latest preview build will not get the cache from the previous preview build, but will get it from master. + +## Usage + +To install, add the following lines to your `netlify.toml` file: + +```toml +[build] + publish = "public" + +[[plugins]] + package = "netlify-plugin-hugo-cache-resources" + + [plugins.inputs] + # If it should show more verbose logs (optional, default = true) + debug = true +``` + +Note: The `[[plugins]]` line is required for each plugin, even if you have other plugins in your `netlify.toml` file already. \ No newline at end of file diff --git a/.netlify/plugins/netlify-plugin-hugo-cache-resources/index.js b/.netlify/plugins/netlify-plugin-hugo-cache-resources/index.js new file mode 100644 index 0000000..fd8ffb6 --- /dev/null +++ b/.netlify/plugins/netlify-plugin-hugo-cache-resources/index.js @@ -0,0 +1,39 @@ +const getResourcesDir = () => { + return 'exampleSite/resources'; +} + +const printList = (items) => { + console.log('---'); + items.forEach((item, index) => { + console.log(`${index + 1}. ${item}`); + }); +} + +module.exports = { + + async onPreBuild({ utils, inputs }) { + const path = getResourcesDir(); + const success = await utils.cache.restore(path); + + if (success) { + const cachedFiles = await utils.cache.list(path); + console.log(`Restored cached resources folder. Total files: ${cachedFiles.length}`); + if (inputs.debug) printList(cachedFiles); + } else { + console.log(`No cache found for resources folder`); + } + }, + + async onPostBuild({ utils, inputs }) { + const path = getResourcesDir(); + const success = await utils.cache.save(path); + + if (success) { + const cachedFiles = await utils.cache.list(path); + console.log(`Saved resources folder to cache. Total files: ${cachedFiles.length}`); + if (inputs.debug) printList(cachedFiles); + } else { + console.log(`No resources folder cached`); + } + } +}; \ No newline at end of file diff --git a/.netlify/plugins/netlify-plugin-hugo-cache-resources/manifest.yml b/.netlify/plugins/netlify-plugin-hugo-cache-resources/manifest.yml new file mode 100644 index 0000000..d6a208f --- /dev/null +++ b/.netlify/plugins/netlify-plugin-hugo-cache-resources/manifest.yml @@ -0,0 +1,5 @@ +name: netlify-plugin-hugo-cache +inputs: + - name: debug + description: Show more verbose logs + default: true \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index 55ebd33..89f501b 100644 --- a/netlify.toml +++ b/netlify.toml @@ -14,4 +14,7 @@ command = "cd exampleSite && hugo --gc --themesDir ../.. -b ${DEPLOY_PRIME_URL}" [context.deploy-preview] - command = "cd exampleSite && hugo --gc --themesDir ../.. -b ${DEPLOY_PRIME_URL}" \ No newline at end of file + command = "cd exampleSite && hugo --gc --themesDir ../.. -b ${DEPLOY_PRIME_URL}" + +[[plugins]] + package = "/.netlify/plugins/netlify-plugin-hugo-cache-resources" \ No newline at end of file From 3496001c2635678e41dc983cacef746ebcc3b77c Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Thu, 17 Sep 2020 22:26:08 +0200 Subject: [PATCH 2/3] fix(netlify): add . to plugin dir --- netlify.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlify.toml b/netlify.toml index 89f501b..65f9abb 100644 --- a/netlify.toml +++ b/netlify.toml @@ -17,4 +17,4 @@ command = "cd exampleSite && hugo --gc --themesDir ../.. -b ${DEPLOY_PRIME_URL}" [[plugins]] - package = "/.netlify/plugins/netlify-plugin-hugo-cache-resources" \ No newline at end of file + package = "./.netlify/plugins/netlify-plugin-hugo-cache-resources" \ No newline at end of file From 69e41b1c0d961ec4c51aab672399f0c8f33e1a7a Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Thu, 17 Sep 2020 22:30:21 +0200 Subject: [PATCH 3/3] ci(netlify): move `plugins` folder to exampleSite --- .../plugins/netlify-plugin-hugo-cache-resources/README.md | 0 .../plugins/netlify-plugin-hugo-cache-resources/index.js | 0 .../plugins/netlify-plugin-hugo-cache-resources/manifest.yml | 0 netlify.toml | 2 +- 4 files changed, 1 insertion(+), 1 deletion(-) rename {.netlify => exampleSite}/plugins/netlify-plugin-hugo-cache-resources/README.md (100%) rename {.netlify => exampleSite}/plugins/netlify-plugin-hugo-cache-resources/index.js (100%) rename {.netlify => exampleSite}/plugins/netlify-plugin-hugo-cache-resources/manifest.yml (100%) diff --git a/.netlify/plugins/netlify-plugin-hugo-cache-resources/README.md b/exampleSite/plugins/netlify-plugin-hugo-cache-resources/README.md similarity index 100% rename from .netlify/plugins/netlify-plugin-hugo-cache-resources/README.md rename to exampleSite/plugins/netlify-plugin-hugo-cache-resources/README.md diff --git a/.netlify/plugins/netlify-plugin-hugo-cache-resources/index.js b/exampleSite/plugins/netlify-plugin-hugo-cache-resources/index.js similarity index 100% rename from .netlify/plugins/netlify-plugin-hugo-cache-resources/index.js rename to exampleSite/plugins/netlify-plugin-hugo-cache-resources/index.js diff --git a/.netlify/plugins/netlify-plugin-hugo-cache-resources/manifest.yml b/exampleSite/plugins/netlify-plugin-hugo-cache-resources/manifest.yml similarity index 100% rename from .netlify/plugins/netlify-plugin-hugo-cache-resources/manifest.yml rename to exampleSite/plugins/netlify-plugin-hugo-cache-resources/manifest.yml diff --git a/netlify.toml b/netlify.toml index 65f9abb..08c3dca 100644 --- a/netlify.toml +++ b/netlify.toml @@ -17,4 +17,4 @@ command = "cd exampleSite && hugo --gc --themesDir ../.. -b ${DEPLOY_PRIME_URL}" [[plugins]] - package = "./.netlify/plugins/netlify-plugin-hugo-cache-resources" \ No newline at end of file + package = "/exampleSite/plugins/netlify-plugin-hugo-cache-resources" \ No newline at end of file