Skip to content

Commit

Permalink
Merge pull request #650 from bensteinberg/add-js-cachebuster
Browse files Browse the repository at this point in the history
Add JS cache-buster
  • Loading branch information
bensteinberg authored Dec 9, 2024
2 parents 00b4890 + 3a76a4a commit 105c41b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
22 changes: 17 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,29 @@ jobs:
LIVE_SITE: ${{ secrets.LIVE_SITE }}
run: |
set -x
CSS_BUST=$(find build/assets/css -type f -exec bash -c 'if [[ `diff {} <(curl -s "https://$LIVE_SITE/$(echo "{}" | cut -c 7-)")` ]]; then echo "bust ({})"; fi' \;)
CSS_BUST=$(find build/assets/css build/*.config.js -type f -exec bash -c 'if [[ `diff {} <(curl -s "https://$LIVE_SITE/$(echo "{}" | cut -c 7-)")` ]]; then echo "bust ({})"; fi' \;)
if [[ $CSS_BUST ]] ; then
COUNTER=`grep cache-buster app/_layouts/default.html | sed -r 's/.*cache-buster=([0-9]+).*/\1/'`
COUNTER=`grep css-cache-buster app/_layouts/default.html | sed -r 's/.*css-cache-buster=([0-9]+).*/\1/'`
let NEWCOUNTER=$COUNTER+1
perl -pi -e "s/cache-buster=$COUNTER/cache-buster=$NEWCOUNTER/" app/_layouts/default.html
perl -pi -e "s/cache-buster=$COUNTER/cache-buster=$NEWCOUNTER/" app/_includes/custom-css.html
perl -pi -e "s/css-cache-buster=$COUNTER/css-cache-buster=$NEWCOUNTER/" app/_layouts/default.html
perl -pi -e "s/css-cache-buster=$COUNTER/css-cache-buster=$NEWCOUNTER/" app/_includes/custom-css.html
git config user.email "[email protected]"
git config user.name "GitHub Actions"
git add app/_layouts/default.html app/_includes/custom-css.html
git commit -m 'Increment CSS cache-buster [skip ci]'
git push origin redesign || exit 1
git push origin develop || exit 1
fi
JS_BUST=$(find build/assets/javascripts -type f -exec bash -c 'if [[ `diff {} <(curl -s "https://$LIVE_SITE/$(echo "{}" | cut -c 7-)")` ]]; then echo "bust ({})"; fi' \;)
if [[ $JS_BUST ]] ; then
COUNTER=`grep js-cache-buster app/_layouts/default.html | sed -r 's/.*js-cache-buster=([0-9]+).*/\1/'`
let NEWCOUNTER=$COUNTER+1
perl -pi -e "s/js-cache-buster=$COUNTER/js-cache-buster=$NEWCOUNTER/" app/_layouts/default.html
perl -pi -e "s/js-cache-buster=$COUNTER/js-cache-buster=$NEWCOUNTER/" app/_includes/custom_scripts.html
git config user.email "[email protected]"
git config user.name "GitHub Actions"
git add app/_layouts/default.html app/_includes/custom-scripts.html
git commit -m 'Increment JS cache-buster [skip ci]'
git push origin develop || exit 1
fi
export DEPLOY_CONTENT='{"GITHUB_RUN_NUMBER":"'$GITHUB_RUN_NUMBER'","GITHUB_SHA":"'$GITHUB_SHA'","GITHUB_REF":"'$GITHUB_REF'","GITHUB_REPOSITORY":"'$GITHUB_REPOSITORY'","GITHUB_ACTOR":"'$GITHUB_ACTOR'"}' ;
export DEPLOY_SIG="sha1=`echo -n "$DEPLOY_CONTENT" | openssl sha1 -hmac $DEPLOY_KEY | sed 's/^.* //'`" ;
Expand Down
4 changes: 2 additions & 2 deletions app/_includes/custom-css.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% if layout.custom-css %}
{% for file in layout.custom-css %}
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/{{ file | strip }}.css?cache-buster=52">
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/{{ file | strip }}.css?css-cache-buster=52">
{% endfor %}
{% endif %}
{% if page.custom-css %}
{% for file in page.custom-css %}
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/{{ file | strip }}.css?cache-buster=52">
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/{{ file | strip }}.css?css-cache-buster=52">
{% endfor %}
{% endif %}
2 changes: 1 addition & 1 deletion app/_includes/custom-scripts.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if page.custom-scripts %}
{% for file in page.custom-scripts %}
<script src="{{ site.baseurl }}/assets/javascripts/{{ file | strip }}"></script>
<script src="{{ site.baseurl }}/assets/javascripts/{{ file | strip }}?js-cache-buster=1"></script>
{% endfor %}
{% endif %}
4 changes: 2 additions & 2 deletions app/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="icon" href="{{ site.baseurl }}/assets/images/favicon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="{{ site.baseurl }}/assets/images/apple-touch-favicon.png">
<link rel="manifest" href="{{ site.baseurl }}/manifest.webmanifest">
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/main.css?cache-buster=52">
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/main.css?css-cache-buster=52">
<link rel="alternate" href="{{ site.baseurl }}/blog/feed/" type="application/rss+xml" title="RSS Feed">
<script src="{{ site.baseurl }}/assets/lib/[email protected]/gsap.min.js"></script>
<script src="{{ site.baseurl }}/assets/lib/[email protected]/Swup.umd.js"></script>
Expand All @@ -27,7 +27,7 @@
</main>
{% include footer.html %}
</div>
<script src="{{ site.baseurl }}/assets/javascripts/main.js"></script>
<script src="{{ site.baseurl }}/assets/javascripts/main.js?js-cache-buster=1"></script>
{% include custom-scripts.html %}
</body>
</html>

0 comments on commit 105c41b

Please sign in to comment.