BattleWords / inject-pwa-head.sh
Surn's picture
0.2.29
ffe26fc
#!/bin/bash
# Inject PWA meta tags into Streamlit's index.html head section
# This script modifies the Streamlit index.html during Docker build
set -e
echo "[PWA] Injecting PWA meta tags into Streamlit's index.html..."
# Find Streamlit's index.html
STREAMLIT_INDEX=$(python3 -c "import streamlit; import os; print(os.path.join(os.path.dirname(streamlit.__file__), 'static', 'index.html'))")
if [ ! -f "$STREAMLIT_INDEX" ]; then
echo "[PWA] ERROR: Streamlit index.html not found at: $STREAMLIT_INDEX"
exit 1
fi
echo "[PWA] Found Streamlit index.html at: $STREAMLIT_INDEX"
# Check if already injected (to make script idempotent)
if grep -q "PWA (Progressive Web App) Meta Tags" "$STREAMLIT_INDEX"; then
echo "[PWA] PWA tags already injected, skipping..."
exit 0
fi
# Read the injection content
INJECT_FILE="/app/pwa-head-inject.html"
if [ ! -f "$INJECT_FILE" ]; then
echo "[PWA] ERROR: Injection file not found at: $INJECT_FILE"
exit 1
fi
# Create backup
cp "$STREAMLIT_INDEX" "${STREAMLIT_INDEX}.backup"
# Use awk to inject after <head> tag
awk -v inject_file="$INJECT_FILE" '
/<head>/ {
print
while ((getline line < inject_file) > 0) {
print line
}
close(inject_file)
next
}
{ print }
' "${STREAMLIT_INDEX}.backup" > "$STREAMLIT_INDEX"
echo "[PWA] PWA meta tags successfully injected!"
echo "[PWA] Backup saved as: ${STREAMLIT_INDEX}.backup"