Skip to content

Commit 295da9b

Browse files
fix: auto-version css/js assets to prevent stale cache issues
1 parent c7da9db commit 295da9b

6 files changed

Lines changed: 13 additions & 6 deletions

File tree

.github/workflows/container-build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
run: mkdir -p website/data/cache
5656
- name: Build container
5757
run: |
58+
VERSION="${GITHUB_SHA::12}"
5859
RUN_KIND="${{ github.event_name }}"
5960
case "$RUN_KIND" in
6061
workflow_dispatch|schedule)
@@ -77,6 +78,7 @@ jobs:
7778
--local context=. \
7879
--local dockerfile=container \
7980
--opt build-arg:GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} \
81+
--opt build-arg:ASSET_VERSION="$VERSION" \
8082
$PARAMS \
8183
--output type=local,dest=build-output
8284
# The local exporter writes the final image filesystem; our website

container/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ WORKDIR /workspace
44

55
COPY . .
66

7-
ARG GITHUB_TOKEN WIKI_FILE
7+
ARG GITHUB_TOKEN WIKI_FILE ASSET_VERSION
88

99
RUN <<EOF
1010
set -e
@@ -13,6 +13,11 @@ git log -1
1313
export GITHUB_TOKEN="${GITHUB_TOKEN}"
1414
npm clean-install
1515
node --run all
16+
17+
# Replace cache-busting placeholder with the current commit SHA for deployed assets.
18+
ASSET_VERSION="${ASSET_VERSION:-dev}"
19+
find website -type f \( -name "*.html" -o -name "*.js" -o -name "*.css" \) \
20+
-exec sed -i "s/__ASSET_VERSION__/${ASSET_VERSION}/g" {} +
1621
EOF
1722

1823
FROM nginxinc/nginx-unprivileged:alpine-slim

website/card.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { openHintsDialog } from "./hints-dialog.js";
1+
import { openHintsDialog } from "./hints-dialog.js?v=__ASSET_VERSION__";
22

33
const cardTemplate = document.getElementById("card-template");
44

website/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>MagicMirror² 3rd Party Modules</title>
77
<link rel="icon" type="image/x-icon" href="favicon.svg" />
8-
<link rel="stylesheet" href="index.css" />
8+
<link rel="stylesheet" href="index.css?v=__ASSET_VERSION__" />
99
<link
1010
rel="stylesheet"
1111
href="fonts/line-awesome-1.3.0/css/line-awesome.min.css"
@@ -196,6 +196,6 @@ <h3 id="hints-dialog-title"></h3>
196196
</div>
197197
</dialog>
198198

199-
<script src="script.js" type="module"></script>
199+
<script src="script.js?v=__ASSET_VERSION__" type="module"></script>
200200
</body>
201201
</html>

website/result.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<title>Result of the module analysis</title>
55
<link rel="icon" type="image/x-icon" href="favicon.svg" />
6-
<link rel="stylesheet" href="result.css" />
6+
<link rel="stylesheet" href="result.css?v=__ASSET_VERSION__" />
77
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
88
</head>
99
<body>

website/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createCard } from "./card.js";
1+
import { createCard } from "./card.js?v=__ASSET_VERSION__";
22

33
let allModules = [];
44
let skippedModules = [];

0 commit comments

Comments
 (0)