Skip to content

Commit

Permalink
chore: cache bust css
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Sep 25, 2023
1 parent 63d0653 commit 9fae14c
Show file tree
Hide file tree
Showing 5 changed files with 913 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: ./hasher.sh
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
Expand Down
33 changes: 33 additions & 0 deletions hasher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# Calculate the SHA256 hash of a file and return the first 6 characters
function calculate_hash() {
local filename="$1"
local hash=$(shasum -a 256 "$filename" | cut -c1-6)
echo "$hash"
}

echo "Running the public hasher"

# Remove the old CSS file
rm ./public/styles.*.css

# Generate the new CSS file
npx tailwindcss -i ./styles.css -o ./public/styles.css

# Add the 6 first characters of the hash to the file name
# of the generated CSS file. This is to bust the cache of the CSS file.

# Get the hash of the generated CSS file
hash=$(calculate_hash ./public/styles.css)

# Rename the generated CSS file
mv ./public/styles.css ./public/styles.$hash.css

# Replace the old CSS file with the new one
# The first argument is an empty string to skip backup in macOS
# Uses a counted range, {0,1}, to simulate a ? operator for the hash
# This matches both styles.css and styles.hash.css
sed -i "" "s/styles\(\.[a-z0-9]\{6\}\)\{0,1\}\.css/styles\.$hash\.css/g" ./views/layouts/main.html

echo "Tailwind generated and updated"
Loading

0 comments on commit 9fae14c

Please sign in to comment.