Skip to content

Instantly share code, notes, and snippets.

@mackenziestarr
Last active August 29, 2015 14:18
Show Gist options
  • Save mackenziestarr/c001dc06c5f479ecb6c4 to your computer and use it in GitHub Desktop.
Save mackenziestarr/c001dc06c5f479ecb6c4 to your computer and use it in GitHub Desktop.
GNU/BSD compatible cache busting with sed
#!/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