Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created March 15, 2020 21:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garybernhardt/225b671cf7800435e38c46600bf866ed to your computer and use it in GitHub Desktop.
Save garybernhardt/225b671cf7800435e38c46600bf866ed to your computer and use it in GitHub Desktop.
commit 9f676b29d4a49356e756048ff249d17e552b5faa (HEAD -> master, origin/master)
Author: Gary Bernhardt <gary.bernhardt@gmail.com>
Date: Sun Mar 15 13:36:55 2020 -0700
cache public dir between deploys (note)
Problem:
1. A deploy starts.
2. A browser starts loading a page.
3. The deploy finishes.
4. The browser requests assets referenced by the page.
5. Those assets no longer exist due to the deploy.
Solution:
Use Heroku's build cache to cache the entire public/dist directory. The
public/dist directory doesn't exist in our git repo, so Heroku is happy
to restore it from the cache at the beginning of every deploy. (If it
did exist in the repo, Heroku would refuse to restore it.) We delete old
files from the cache after 30 days to be sure that it doesn't grow
unbounded.
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
added: bin/remove-old-build-artifacts
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
@ bin/remove-old-build-artifacts:4 @
+#!/usr/bin/env bash
+
+set -e -o pipefail
+
+echo "Removing old build artifacts:"
+# "-mtime +30" means "files modified more than 30 days ago"
+find public/dist -type f -mtime +30 | while read -r p; do
+ echo "- ${p}"
+ rm "$p"
+done
+echo "Done removing old build artifacts."
+
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
modified: package.json
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
@ package.json:56 @
},
"scripts": {
"dev": "bin/dev",
- "heroku-postbuild": "tsc && webpack -p",
+ "heroku-postbuild": "bin/remove-old-build-artifacts && tsc && webpack -p",
"server:test": "node --trace-deprecation ./build/server/run.js",
"stop-only": "stop-only --folder cypress/integration",
"ci": "npm-run-all --print-label stop-only test:unit test:courses test:cypress lint:tslint lint:eslint",
@ package.json:114 @
"tsc-watch": "^4.0.0",
"webpack-manifest-plugin": "^2.2.0"
},
+ "//": "Heroku cache directories: we cache our public/dist directory so assets from previous builds still exist on new deploys. That ensures that out-of-date clients can still see old assets until they reload the latest version of the client code.",
+ "cacheDirectories": ["node_modules", "public/dist"],
"//": "Chrome's iOS sim dev tools uses a user agent that matches iOS 9",
"browserslist": [
"defaults",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment