Skip to content

Instantly share code, notes, and snippets.

@rugk
Created June 16, 2016 20:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rugk/d242a539979942f98db11bf7a8ab1bde to your computer and use it in GitHub Desktop.
Save rugk/d242a539979942f98db11bf7a8ab1bde to your computer and use it in GitHub Desktop.
Create Link headers for HTTP/2 Server push

This script creates headers of all files in a given directory to use them for HTTP/2 server push.

Usage: linkheader.sh /mydir

#!/bin/sh
# Creates Link headers of all asset files in a directory.
#
# LICENSE: CC0/Public Domain - To the extent possible under law, rugk has waived all copyright and related or neighboring rights to this work. This work is published from: Deutschland.
getfiletype() {
fileext=${1##*.}
case $fileext in
js)
echo "script"
;;
css)
echo "stylesheet"
;;
jpg|jpeg|png|gif)
echo "image"
;;
esac
}
for asset in $1/*; do
assetfile=$( basename "${asset}" )
filetype=$( getfiletype "${assetfile}" )
if [ "${filetype}" != "" ]; then
echo "Link <$1/${assetfile}>; rel=preload; as=${filetype}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment