Skip to content

Instantly share code, notes, and snippets.

@vlasky
Last active March 21, 2018 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlasky/e41c8549dda8594bc88124bbae2eebf9 to your computer and use it in GitHub Desktop.
Save vlasky/e41c8549dda8594bc88124bbae2eebf9 to your computer and use it in GitHub Desktop.
This is a Linux shell (bash) script that gzip-compresses the static assets contained within a production build of a Meteor webapp. It has been tested under CentOS 7. This script is intended to be used when Nginx is running as the web server and the 'gzip_static' configuration option is enabled. When the client supports gzip compression, Nginx wi…
#!/bin/sh
# Files with the following extensions will be compressed with gzip
GZFILETYPES=( "*.html" "*.css" "*.js" "*.ico" "*.xml" "*.json" "*.svg" "*.otf" "*.ttf" "*.eot" )
# The base directory of the Meteor production bundle assets that are served to the client
DIRECTORIES="/var/www/meteor/production/bundle/programs/web.browser/"
# Files smaller than this size (in bytes) will NOT be compressed as the gzip overhead usually makes them larger
GZMINSIZE=150
for currentDir in $DIRECTORIES; do
for f in "${GZFILETYPES[@]}"; do
files="$(find $currentDir -iname "$f")";
if [[ -z $files ]] ; then
continue
fi
echo "$files" | while read file ; do
PLAINFILE=$file;
GZIPPEDFILE=$file.gz;
if [[ -e "$GZIPPEDFILE" ]]; then
if [[ `stat --printf=%Y "$PLAINFILE"` -gt `stat --printf=%Y "$GZIPPEDFILE"` ]]; then
echo .gz is older, updating "$GZIPPEDFILE"...;
gzip -9 -f -c "$PLAINFILE" > "$GZIPPEDFILE";
fi
if [[ `stat --printf=%s "$PLAINFILE"` -le $GZMINSIZE ]]; then
echo Uncompressed size is less than minimum "("$(stat --printf=%s "$PLAINFILE")")", removing "$GZIPPEDFILE";
rm -f "$GZIPPEDFILE";
fi
elif [[ `stat --printf=%s "$PLAINFILE"` -gt $GZMINSIZE ]]; then
echo Creating .gz "for" "$PLAINFILE"...;
gzip -9 -c "$PLAINFILE" > "$GZIPPEDFILE";
fi
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment