#!/usr/bin/env bash # Compare pages on the VM with the same pages on Netlify. # From https://exe.dev/deploy/migrate-netlify # # netlify/check [PATHS_FILE] paths one per line (default ~/netlify/paths.txt) # # For each path: status, redirect target, content type, and a hash of the # body (of its text, for HTML: Netlify rewrites form markup). Pages with # live data or dates may differ; look at those by hand. set -uo pipefail kit=~/netlify list=${1:-$kit/paths.txt} netlify=${NETLIFY_URL:-$(jq -r '.ssl_url // .url' $kit/site.json)} [ -f "$list" ] || { echo "list the pages to compare in $list, one path per line" >&2; exit 2; } fetch() { # URL -> "status location type hash" local h b h=$(mktemp) b=$(mktemp) curl -s -o "$b" -D "$h" --max-time 30 "$1" local s l t s=$(head -1 "$h" | cut -d' ' -f2) l=$(grep -i '^location:' "$h" | tr -d '\r' | cut -d' ' -f2- | sed "s#^$netlify##; s#^http://localhost:8000##") t=$(grep -i '^content-type:' "$h" | tr -d '\r' | cut -d' ' -f2- | cut -d';' -f1) local sum case $s:$t in 3*) sum=- ;; # redirects: the target is what counts *:text/html) sum=$(sed 's/<[^>]*>/ /g' "$b" | tr -s ' \t\n' ' ' | sha256sum | cut -c1-12) ;; # the text, not the markup *) sum=$(sha256sum < "$b" | cut -c1-12) ;; esac echo "$s ${l:--} ${t:--} $sum" rm -f "$h" "$b" } same=0 diff=0 while read -r p; do case $p in ''|'#'*) continue ;; esac a=$(fetch "$netlify$p") b=$(fetch "http://localhost:8000$p") if [ "$a" = "$b" ]; then echo "same $p"; same=$((same+1)) else echo "DIFF $p"; echo " netlify: $a"; echo " vm: $b"; diff=$((diff+1)); fi done < "$list" echo "$same same, $diff different"