Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Explains the Starchild iframe-based Browser preview panel, reverse-proxy URL routing, and relative-path asset rules.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: browser-preview3version: 1.1.04description: "Preview panel knowledge — the iframe-based Preview tab in the right panel. Use when the user mentions browser, preview, tab disappeared, page broken, blank screen, white screen, 白屏, 页面挂了, tab 不见了, preview not loading, or asks about running services."56metadata:7starchild:8emoji: "🌐"9skillKey: browser-preview1011user-invocable: true12disable-model-invocation: false13---1415# Browser Preview1617You already know `preview_serve` and `preview_stop`. This skill fills the gap: what happens **after** preview_serve returns a URL — how the user actually sees it.1819## What is the Preview Panel2021The frontend has a right-side panel with three tabs: **Files**, **Preview**, and **Jobs**. The Preview tab renders preview URLs inside an iframe. When you call `preview_serve`, the frontend automatically opens a Preview tab loading that URL.2223Key facts:24- Each `preview_serve` call creates one Preview tab25- URL format: `https://<host>/preview/{id}/`26- Preview panel has a **⋮ menu** (top-right) showing "RUNNING SERVICES" list27- Preview tab can be **closed by the user** without stopping the backend service28- Backend service stopping → Preview tab shows an error page2930## ⚠️ CRITICAL: Never Tell Users to Access localhost3132**The user's browser CANNOT access `localhost` or `127.0.0.1`.** These addresses point to the server container, not the user's machine. The preview architecture uses a **reverse proxy**:3334```35User's Browser → https://<host>/preview/{id}/path → (reverse proxy) → 127.0.0.1:{port}/path36```3738Rules:39- **NEVER** tell the user to visit `http://localhost:{port}` or `http://127.0.0.1:{port}` — they cannot reach it40- **ALWAYS** direct users to the preview URL: `/preview/{id}/` (or the full URL `https://<host>/preview/{id}/`)41- `curl http://localhost:{port}` is for **your own server-side diagnostics only** — never suggest it to the user as a way to "test" the preview42- When a preview is running, tell the user: "Check the Preview panel, or refresh the Preview panel"43- If you need to give the user a URL, use the `url` field returned by `preview_serve` (format: `/preview/{id}/`)4445## ⚠️ Static Assets Must Use Relative Paths4647Because previews are served under `/preview/{id}/`, **absolute paths in HTML/JS/CSS will break**. The reverse proxy strips the `/preview/{id}` prefix before forwarding to the backend, but the browser resolves absolute paths from the domain root.4849Example of the problem:50```html51<!-- ❌ BROKEN: browser requests https://host/static/app.js → 404 (bypasses preview proxy) -->52<script src="/static/app.js"></script>5354<!-- ✅ WORKS: browser requests https://host/preview/{id}/static/app.js → proxied correctly -->55<script src="static/app.js"></script>56<script src="./static/app.js"></script>57```5859Common patterns to fix:60| Broken (absolute) | Fixed (relative) |61|---|---|62| `"/static/app.js"` | `"static/app.js"` or `"./static/app.js"` |63| `"/api/users"` | `"api/users"` or `"./api/users"` |64| `"/images/logo.png"` | `"images/logo.png"` or `"./images/logo.png"` |65| `url('/fonts/x.woff')` | `url('./fonts/x.woff')` |66| `fetch('/data.json')` | `fetch('data.json')` |6768**Check ALL places** where paths appear:691. HTML `src`, `href` attributes702. JavaScript `fetch()`, `XMLHttpRequest`, dynamic imports713. CSS `url()` references724. JavaScript string literals (e.g., `'/static/'` in template strings or concatenation)735. Framework config files (e.g., `publicPath`, `base`, `assetPrefix`)7475⚠️ Be thorough — it's common to fix CSS `url()` but miss JS string literals like `'/static/'` (with single quotes). Search for ALL occurrences of absolute paths across all file types.7677## ⚠️ Do NOT Browse Filesystem to Debug Previews7879**Never** look at workspace directories like `preview/`, `output/`, or random folders to understand preview state. Those are user data, not preview service state.8081The **only** sources of truth:821. Registry file: `/data/previews.json` (running services)832. History file: `/data/preview_history.json` (all past services)843. `preview_serve` / `preview_stop` tools854. Port checks via `curl` (server-side only, for your diagnostics)8687Do NOT use `ls`/`find` on workspace directories to diagnose preview issues. Do NOT call unrelated tools like `list_scheduled_tasks`. Stay focused.8889## Step-by-Step: Diagnosing Preview Issues9091When a user reports any Preview panel problem, follow this exact sequence:9293### Step 1: Read the registry (running services)9495```bash96cat /data/previews.json 2>/dev/null || echo "NO_REGISTRY"97```9899⚠️ Your bash CWD is `/data/workspace/`. The registry is at `/data/previews.json` (absolute path, one level up). Always use the absolute path.100101JSON structure:102```json103{104"previews": [105{"id": "f343befc", "title": "My App", "dir": "/data/workspace/my-project", "command": "npm run dev", "port": 9080, "is_builtin": false}106]107}108```109110### Step 2: Branch based on registry state111112**If registry has entries** → Go to Step 3 (verify services)113**If registry is empty or missing** → Go to Step 4 (check history)114115### Step 3: Registry has entries — verify and fix116117For each preview in the registry, check if the port is responding **server-side** (this is your diagnostic, not for the user):118```bash119curl -s -o /dev/null -w "%{http_code}" http://localhost:{port}120```121122**If port responds (200):**123- The service IS running. Tell the user:124- "You have a running service: {title}"125- "Click the ⋮ menu at the top-right of the Preview panel, then click it in the RUNNING SERVICES list to reopen"126- Preview URL: `/preview/{id}/`127- **If user says the ⋮ menu is empty or doesn't show the service** → frontend lost sync. Fix by recreating: `preview_stop(id)` then `preview_serve(dir, title, command)` using the info from the registry. This forces the frontend to re-register the tab.128129**If port does NOT respond:**130- Process crashed but registry entry remains. Recreate:131```132preview_stop(id="{id}")133preview_serve(dir="{dir}", title="{title}", command="{command}")134```135136### Step 4: No running services — check history first, then scan workspace137138When there are no running services, use a **two-tier lookup** to find projects the user can preview:139140#### Tier 1: Read preview history (preferred — fast and accurate)141142```bash143cat /data/preview_history.json 2>/dev/null || echo "NO_HISTORY"144```145146JSON structure:147```json148{149"history": [150{151"id": "f343befc",152"title": "Trading System",153"dir": "/data/workspace/my-project",154"command": "python main.py",155"port": 8000,156"is_builtin": false,157"created_at": 1709100000.0,158"last_started_at": 1709200000.0159}160]161}162```163164History entries are **never removed by `preview_stop`** — they persist across restarts. Entries are automatically pruned only when the project directory no longer exists.165166**If history has entries:**167- List all history entries to the user with title, directory, and last started time168- Ask which one they want to restart169- Call `preview_serve` with the `dir`, `title`, and `command` from the history entry170171**If user says a project is missing from history** → fall through to Tier 2.172173#### Tier 2: Scan workspace (fallback — when history is empty or incomplete)174175```bash176find /data/workspace -maxdepth 2 \( -name "package.json" -o -name "index.html" -o -name "*.html" -o -name "app.py" -o -name "main.py" -o -name "vite.config.*" \) -not -path "*/node_modules/*" -not -path "*/skills/*" -not -path "*/memory/*" -not -path "*/prompt/*" -not -path "*/.git/*" 2>/dev/null177```178179Then:1801. List discovered projects with brief descriptions1812. Ask the user which one to preview1823. Call `preview_serve` with the appropriate directory183184**Don't just say "no services running" and stop.** Always check history first, then scan, and offer options.185186## Quick Reference187188| User says | You do |189|-----------|--------|190| "tab disappeared" / "tab 不见了" | Step 1 → 2 → 3 or 4 |191| "blank page" / "白屏" | Check port (server-side), if dead → recreate; if alive → check for absolute path issues |192| "not updating" / "内容没更新" | Suggest refresh button in Preview tab, or recreate preview |193| "port conflict" / "端口冲突" | `preview_stop` old → `preview_serve` new |194| "can't see service" / "⋮ menu empty" | `preview_stop` + `preview_serve` to force re-register |195| "where's my project" / "what did I build" | Read `/data/preview_history.json` and list entries |196| "resource load failed" / "JS/CSS 404" | Check for absolute paths (`/static/`, `/api/`), fix to relative paths |197198## What You Cannot Do199200- Cannot directly open/close/refresh Preview tabs (frontend UI)201- Cannot force-refresh the iframe202- Cannot read what the iframe displays203204When you can't do something, tell the user the manual action (e.g., "click refresh in Preview tab"). If manual action doesn't work, recreate the preview with `preview_stop` + `preview_serve`.205206## Common Mistakes to Avoid2072081. ❌ Telling user to "visit http://localhost:18791/" — user cannot access localhost2092. ❌ Saying "refresh the page at localhost" — meaningless to the user2103. ❌ Only fixing CSS `url()` paths but missing JS string literals with absolute paths2114. ❌ Forgetting to check ALL file types (HTML, JS, CSS, config) for absolute paths2125. ✅ Always use `/preview/{id}/` as the user-facing URL2136. ✅ Always use `curl localhost:{port}` only for your own server-side diagnostics2147. ✅ After fixing paths, call `preview_stop` + `preview_serve` to restart, then tell user to check Preview panel215