Last active
August 29, 2015 14:18
-
-
Save mackenziestarr/c001dc06c5f479ecb6c4 to your computer and use it in GitHub Desktop.
GNU/BSD compatible cache busting with sed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# update cache busting strings in html file, supports GNU and BSD | |
function size_of () { wc -c $1 | awk '{print $1}'; } | |
before=`cat $1 | grep -Eo 'css\?rel=[0-9]+' | grep -Eo '([0-9]+)'` | |
now=`date +%s` | |
sed -i.bak "s/main.*css?rel=[0-9]*/main.min.css?rel=$now/" $1 | |
sed -i.bak "s/build.*js?rel=[0-9]*/build.js?rel=$now/" $1 | |
sed -i.bak "s/urlArgs.*$/urlArgs: \"v=$now\"/" $1 | |
if [[ $(size_of $1) -eq $(size_of "$1.bak") ]]; | |
then | |
echo "broke cache in $1 [$before] => [$now]" | |
rm "$1.bak" | |
echo "removed $1.bak" | |
else | |
echo 'error writing to $1' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment