#!/bin/bash USAGE=< Options: --force : overwrite existing files (default skips) --clean : remove all files before starting --only : restrict rendering to the given pattern --output : output directory (default: ./static) --sitemap : sitemap file (default: ./static/sitemap.txt END CLEAN="yes" FORCE="no" OUTPUT_DIR="./static" SITEMAP_FILE="./static/sitemap.txt" while [[ "$1" != "" ]]; do case "$1" in "--force") FORCE="yes";; "--help") echo $USAGE; exit 0;; "--clean") CLEAN="yes";; "--only") shift; ONLY="$1";; "--output") shift; OUTPUT_DIR="$1";; "--sitemap") shift; SITEMAP_FILE="$1";; esac shift done SCRIPT='/tmp/prerender_script.coffee' cat > $SCRIPT < if status isnt 'success' console.error "Could not load #{url}" phantom.exit(1) setTimeout (-> console.log page.content phantom.exit(0) ), 5000 END if [[ "$CLEAN" == "yes" ]]; then find "$OUTPUT_DIR/browse" -name "*.html" | xargs rm fi cat $SITEMAP_FILE | while read URL; do OUTPUT_FILE=$(sed 's/http:\/\/[^/]*\///' <<< $URL) OUTPUT_FILE="$OUTPUT_DIR/$OUTPUT_FILE/index.html" OUTPUT_FILE=$(sed 's/\/\//\//' <<< $OUTPUT_FILE) if [[ ! -e "$OUTPUT_FILE" || "$FORCE" == "yes" ]]; then mkdir -p $(dirname "$OUTPUT_FILE") echo "prerendering $URL into $OUTPUT_FILE" phantomjs --web-security=false --load-images=false $SCRIPT "$URL" >"$OUTPUT_FILE" 2>/dev/null if [[ "$?" != "0" ]]; then echo "An error occured rendering $URL" rm "$OUTPUT_FILE" 2>/dev/null fi else echo "Skipping $URL..." fi done rm $SCRIPT