add cache busting
Build and Push Container / build-and-push (push) Successful in 16s

This commit is contained in:
2026-05-28 16:01:42 -04:00
parent 6a30145171
commit 54746f4636
5 changed files with 65 additions and 9 deletions
+3
View File
@@ -1,3 +1,6 @@
# Build output
dist/
# Build artifacts
*.tar
*.tar.gz
+6 -8
View File
@@ -1,16 +1,14 @@
FROM docker.io/library/nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/conf.d/default.conf && \
apk add --no-cache coreutils
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY src/index.html /usr/share/nginx/html/
COPY src/style.css /usr/share/nginx/html/
COPY src/script.js /usr/share/nginx/html/
COPY src/profile.jpeg /usr/share/nginx/html/
COPY src/ /src/
COPY build.sh /build.sh
RUN chmod +x /build.sh && SRC=/src DIST=/dist /bin/sh /build.sh && cp -r /dist/* /usr/share/nginx/html/ && rm -rf /src /build.sh /dist
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD wget -qO- http://localhost:8080/ || exit 1
HEALTHCHECK --interval=30s --timeout=3s --retries=3 CMD wget -qO- http://localhost:8080/ || exit 1
+1 -1
View File
@@ -13,7 +13,7 @@ podman build -t homepage:latest .
### Run the container
```bash
podman run --rm -p 8080:8080 -v ./src:/usr/share/nginx/html:z homepage:latest
podman run --rm -p 0.0.0.0:8080:8080 -v ./src:/usr/share/nginx/html:z homepage:latest
```
Visit `http://localhost:8080` in your browser.
Executable
+41
View File
@@ -0,0 +1,41 @@
#!/bin/sh
set -e
SRC="${SRC:-src}"
DIST="${DIST:-dist}"
# Clean and create dist
rm -rf "$DIST"
mkdir -p "$DIST"
# Copy index.html
cp "$SRC/index.html" "$DIST/index.html"
# Cache-bust assets by appending a content hash to filenames
for ext in css js jpeg png svg; do
for file in "$SRC"/*."$ext"; do
[ -f "$file" ] || continue
name=$(basename "$file" | sed "s/\.$ext$//")
hash=$(md5sum "$file" | cut -d' ' -f1)
newname="${name}.${hash}.${ext}"
cp "$file" "$DIST/$newname"
# Update references in HTML
case "$ext" in
css)
sed -i "s|href=\"${name}.${ext}\"|href=\"${newname}\"|g" "$DIST/index.html"
;;
js)
sed -i "s|src=\"${name}.${ext}\"|src=\"${newname}\"|g" "$DIST/index.html"
;;
jpeg|png|svg)
sed -i "s|\"${name}.${ext}\"|\"${newname}\"|g" "$DIST/index.html"
;;
esac
done
done
echo "Build complete: $DIST/"
echo "Assets with cache-busting hashes:"
ls -1 "$DIST"
+14
View File
@@ -17,6 +17,20 @@ server {
gzip_min_length 256;
gzip_vary on;
# Never cache index.html so the latest HTML is always served
location = /index.html {
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
try_files $uri =404;
}
# Cache hashed assets forever (filename contains content hash)
location ~* \.(css|js|jpeg|jpg|png|gif|ico|svg|woff2?)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}
location / {
try_files $uri $uri/ /index.html;
}