The reload container (docker/web-dev/live.js, browser-sync) now publishes the site's port itself, so the page's socket can connect
(the old proxy ws option moved it to an unpublished port: the tab never followed). The IDE tells it each saved file (POST /__jumphp/notify),
so it works without file events (Docker Desktop also polls); CSS is injected without a reload, anything else reloads once.
Status bar item Live: N (browser tabs connected), open the site at the live-reload address, turn it off ('live': 'off' in web.json).
bin/jumphp web:live, LiveReload (IDE) + tests, end-to-end test with headless Chrome on a Symfony site, docs, 14 languages.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
62 lines
3.2 KiB
Shell
62 lines
3.2 KiB
Shell
#!/usr/bin/env bash
|
|
# Live reload of a web site, for real (Docker, network, a few minutes): a Symfony site made by `bin/jumphp web:new`, served by
|
|
# `web:up`, a headless Chrome (Puppeteer) on its live-reload address; files are edited like the IDE's save does (the file written,
|
|
# then POST /__jumphp/notify), and once more with the watcher alone:
|
|
# - a CSS rule changed: the computed style changes without a reload (a mark on the window survives, the scroll stays),
|
|
# - a Twig template changed: the page reloads with the new text,
|
|
# - a controller changed: the page reloads with the new text.
|
|
# bash tools/tests/live-reload.test.sh [folder] (default: a temporary folder, removed at the end)
|
|
set -uo pipefail
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
ROOT="$(cd "$HERE/../.." && pwd)"
|
|
site="${1:-$(mktemp -d /tmp/sf-live-XXXXXX)}"
|
|
made=""; [ -z "${1:-}" ] && made=1
|
|
fails=0
|
|
cleanup() { "$ROOT/bin/jumphp" web:stop "$site" >/dev/null 2>&1; [ -n "$made" ] && docker run --rm -v "$site:/s" alpine rm -rf /s/. 2>/dev/null; [ -n "$made" ] && rm -rf "$site"; }
|
|
trap cleanup EXIT
|
|
|
|
if [ ! -f "$site/composer.json" ]; then
|
|
rm -rf "$site"
|
|
"$ROOT/bin/jumphp" web:new symfony "$site" --db=sqlite >/dev/null || { echo "FAIL web:new"; exit 1; }
|
|
fi
|
|
mkdir -p "$site/src/Controller" "$site/templates" "$site/public/css"
|
|
cat > "$site/src/Controller/HomeController.php" <<'PHP'
|
|
<?php
|
|
namespace App\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
final class HomeController extends AbstractController
|
|
{
|
|
#[Route('/', name: 'home')]
|
|
public function index(): Response
|
|
{
|
|
return $this->render('home.html.twig', ['who' => 'controller-v1']);
|
|
}
|
|
}
|
|
PHP
|
|
cat > "$site/templates/home.html.twig" <<'TWIG'
|
|
<!doctype html>
|
|
<html><head><meta charset="utf-8"><title>live</title><link rel="stylesheet" href="/css/app.css"></head>
|
|
<body><h1 id="t">template-v1</h1><p id="w">{{ who }}</p><div style="height:3000px"></div></body></html>
|
|
TWIG
|
|
echo 'h1 { color: rgb(255, 0, 0); }' > "$site/public/css/app.css"
|
|
|
|
"$ROOT/bin/jumphp" web:up "$site" >/dev/null 2>&1 || { echo "FAIL web:up"; exit 1; }
|
|
url="$("$ROOT/bin/jumphp" web:info "$site" | sed -n 's|^site (live reload): ||p')/"
|
|
for _ in $(seq 1 120); do curl -sf -o /dev/null --max-time 2 "${url}__jumphp/status" && break; sleep 2; done
|
|
curl -sf "${url}__jumphp/status" || { echo "FAIL the live-reload server does not answer at $url"; exit 1; }
|
|
echo
|
|
|
|
img="${JUMPHP_PUPPETEER_IMAGE:-ghcr.io/puppeteer/puppeteer:24.10.0}"
|
|
for mode in notify watch; do
|
|
echo "== $mode"
|
|
docker run --rm --network host --user 0:0 -e PUPPETEER_CACHE_DIR=/home/pptruser/.cache/puppeteer -v "$ROOT/tools/webshot:/scripts:ro" -v "$site:/site" \
|
|
-w /home/pptruser "$img" sh -c "cp /scripts/livecheck.cjs . && node livecheck.cjs $url /site $mode" || fails=$((fails + 1))
|
|
# back to the first state for the next run
|
|
sed -i 's/0, 0, 255/255, 0, 0/' "$site/public/css/app.css"; sed -i 's/template-v2/template-v1/' "$site/templates/home.html.twig"
|
|
sed -i 's/controller-v2/controller-v1/' "$site/src/Controller/HomeController.php"; sleep 3
|
|
done
|
|
[ "$fails" = 0 ] && echo "all passed" || { echo "$fails failed"; exit 1; }
|