Skip to content

Instantly share code, notes, and snippets.

@searls
Last active January 11, 2025 20:33
Show Gist options
  • Save searls/ce657bb421198fa80653563e16d46324 to your computer and use it in GitHub Desktop.
Save searls/ce657bb421198fa80653563e16d46324 to your computer and use it in GitHub Desktop.
Use fswatch instead of the built-in hugo server, which keeps giving me too many file errors
#!/bin/bash
# Ensure fswatch is installed
if ! command -v fswatch &> /dev/null; then
echo "Installing fswatch with Homebrew..."
brew install fswatch
exit 1
fi
# Ensure Caddy is installed for serving files
if ! command -v caddy &> /dev/null; then
echo "Installing Caddy with Homebrew..."
brew install caddy
fi
# Function to clean up background processes when the script exits
cleanup() {
echo "Stopping fswatch..."
kill $CADDY_PID 2>/dev/null
exit 0
}
# Trap Ctrl-C (SIGINT) to clean up processes
trap cleanup SIGINT
echo "Starting Caddy static file server on port 1313..."
caddy file-server --root ./public --listen :1313 &
CADDY_PID=$!
while true; do
fswatch -o . -e '\.git' -e '_gen' -e 'resources' -e 'public' -e 'static/pagefind' | while read -r event; do
echo "Detected change, rebuilding site..."
hugo --buildFuture --buildDrafts
npm run pagefind:dev
done
sleep 1 # Avoid rapid restarts if fswatch fails
done
# Clean up background processes at exit
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment