Skip to content

feat(sandbox): show production site while the preview dev server wakes - #5084

Merged
guitavano merged 11 commits into
mainfrom
guitavano/sandbox-prod-preview-fallback
Jul 23, 2026
Merged

guitavano merged 11 commits into
mainfrom
guitavano/sandbox-prod-preview-fallback

Conversation

@guitavano

@guitavano guitavano commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Lovable-style boot for projects linked to a live site. While the sandbox dev server is still waking, the preview canvas paints the linked site's live production URL inside the iframe with a non-blocking "waking" pill, instead of the full blocking booting overlay. The user sees their site immediately and can open Blocks / edit the CMS right away; the iframe swaps to the sandbox preview the moment the dev server is up.

Product decisions (confirmed with @guitavano)

  1. Source of the production URL: persisted, not guessed. We do not assume {siteSlug}.deco.site — a site's real production URL can be a custom domain. The actual URL deco.cx reports is stored on the agent's metadata.productionUrl.
  2. Where: agent metadata (branch-independent), alongside siteSlug/githubRepo. No DB migration (metadata is JSON).
  3. Editable, not just import-time. Set automatically at deco.cx import, and editable afterwards via a "Production URL" field in the agent settings' Sandbox card — so users can set it for projects imported before the field existed, or when a custom domain changes.

What changed

  • Schema: productionUrl added to the virtual-mcp metadata (entity / create / update) in packages/mesh-sdk.
  • Shared helper (shared/deco-site-production-url.ts): productionUrlFromDomain (bare host → https://…, validated) and sanitizeProductionUrl (validate stored value to a canonical http(s) href — rejects javascript:/garbage).
  • Import: import-from-deco-dialog.tsx computes the production URL from the site's domains (d.production first, else the first domain) and persists it on create.
  • Editable field: ProductionUrlField in the settings Sandbox card, bound to metadata.productionUrl, autosaved via the existing form; normalizes on blur.
  • Preview: reads metadata.productionUrl (sanitized) — no siteSlug derivation. New pure, unit-tested resolvePreviewDisplay decides the canvas surface: sandbox iframe, production fallback + waking pill, or (no productionUrl) today's blocking overlay. Gated on progress.status (boot no longer in progress), so a failed boot correctly shows the sandbox crash page, not a false "waking" pill.
  • The production frame is view-only: visual/CMS editor injection, path sync, and vm_preview_loaded analytics are skipped for the cross-origin fallback (injection already targets the sandbox origin, so it's a no-op there anyway). The Blocks panel keeps working from the committed .deco/*.gen.json snapshot.
  • Unified the toolbar/Blocks-button gate on a derived previewSurfaceActive (the deferred follow-up from feat: Genesis #1) so Blocks is reachable during the production fallback. iframe key tracks the base URL so it remounts cleanly on the production→sandbox swap.

Behavior when there's no productionUrl

GitHub-only projects, or sites imported before this field existed and never set → productionUrl is null → exact current behavior (blocking SandboxStateCard until the dev server is ready). Suspended/errored states are untouched.

Testing

  • bun test shared/deco-site-production-url.test.ts — URL normalization/validation (bare host, existing scheme, whitespace, javascript:/garbage/empty/nullish).
  • bun test .../preview-display.test.ts — every display-mode branch (running → sandbox; failed → sandbox crash page, no pill; fresh/warming boot → production + pill; no productionUrl → blocking overlay; suspended/errored passthrough).
  • Full workspace bun run check clean; bun run lint 0 errors on changed files; bun run fmt applied.

🤖 Generated with Claude Code


Also in this PR: daemon regenerates blocks.gen.json when absent

Related, but independent of the production-preview fallback above — both make the editing surface usable earlier in the boot.

Problem: Many repos gitignore .deco/blocks.gen.json (a multi-MB single-line merge of .deco/blocks/*.json, regenerated on dev cold-start — see casaevideo-tanstack). So a fresh sandbox has the block sources but not the merged artifact, and the CMS/Blocks panel (which reads the committed snapshot via the daemon when the dev server is down) couldn't render until the runtime came up — even with the FS ready.

Fix: The daemon's /read handler regenerates it on the fly when absent — merging each .deco/blocks/<stem>.json into { [decodeURIComponent(stem)]: <contents> }, exactly what the runtime emits (the stem is the encodeURIComponent of the block id).

  • Non-blocking (daemon = single-threaded event loop): async readdir/readFile + raw-text concatenation — no per-value JSON.parse/JSON.stringify of the multi-MB payload; only the small keys are JSON-encoded.
  • No client change: returned un-numbered. Pretty JSON never matches the ^\d+\t line-number prefix, so the existing stripLineNumbers is a no-op and readCommittedJson's JSON.parse works as-is.
  • Inert otherwise: when the file exists the normal read wins; when there's no blocks dir it falls through to the existing 404. A malformed block isn't caught here (parsing would block the loop) — the client JSON.parse fails → 'no snapshot', same as today; write/edit handlers already gate block validity.

Tests: 7 new cases in routes/fs.test.ts (decoded-key merge incl. %2F→/; no-dir / empty-dir → null; ignores non-json + empty files; on-the-fly generation via the handler; committed file preferred; still-404 with no blocks dir). Full bun run check clean.

@guitavano
guitavano force-pushed the guitavano/sandbox-prod-preview-fallback branch from 417e697 to e59e0be Compare July 23, 2026 03:21
guitavano and others added 10 commits July 23, 2026 08:54
Lovable-style boot for deco.cx projects: while the sandbox dev server is
still waking, paint the linked site's live production URL in the preview
iframe with a non-blocking "waking" pill, instead of the full blocking
overlay. The user sees their site and can open Blocks immediately; the
iframe swaps to the sandbox preview once the dev server is up.

- Production URL is derived (no migration) from the agent's
  `metadata.siteSlug` via the public `{slug}.deco.site` convention.
- New pure, unit-tested `resolvePreviewDisplay` decides the canvas surface
  (sandbox / production fallback / blocking overlay); GitHub-only projects
  with no siteSlug keep the original blocking-overlay behavior.
- The production frame is view-only: visual/CMS editor injection, path
  sync and analytics are skipped for the cross-origin fallback; the Blocks
  panel still reads the committed snapshot.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ing it

Don't guess `{siteSlug}.deco.site` — a site's real production URL can be a
custom domain. Persist the actual URL deco.cx reports.

- Add `productionUrl` to the virtual-mcp metadata schema (entity/create/update).
- Populate it at deco.cx import time from the site's production domain (the
  value was already fetched to render the import cards), via a new shared
  `productionUrlFromDomain` helper.
- Preview reads `metadata.productionUrl` (sanitized to a valid http(s) URL)
  instead of deriving from siteSlug. Sites imported before this field exists
  simply fall back to the original blocking overlay.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Add a "Production URL" field to the Sandbox card in agent settings, bound
to `metadata.productionUrl` (autosaved via the existing form). Normalizes
on blur (bare host → https, invalid → null) with the shared
`productionUrlFromDomain` helper. This lets users set/change the preview
fallback URL — including for projects imported before the field existed,
or when a site's custom domain changes — not just at import time.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Repos commonly gitignore `.deco/blocks.gen.json` (a multi-MB single-line
merge of `.deco/blocks/*.json`, regenerated on dev cold-start), so a fresh
sandbox has the block sources but not the merged artifact — the CMS/Blocks
couldn't be read until the dev runtime came up, even with the FS ready.

The daemon's /read handler now regenerates it on the fly when the file is
absent: merge each `.deco/blocks/<stem>.json` into
`{ [decodeURIComponent(stem)]: <contents> }` — exactly what the runtime
emits. Async reads + raw-text concatenation (no per-value JSON.parse/
stringify) keep the single-threaded event loop free. Returned un-numbered:
pretty JSON never matches the `^\d+\t` line-number prefix, so the client's
stripLineNumbers is a no-op and JSON.parse still works — no client change.

Inert when the file exists (normal read wins) or when there's no blocks
dir (falls through to the existing 404).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Enlarge the preview "starting" pill (bigger padding/text/spinner, stronger
contrast) and simplify the copy to just "Starting your preview" — no mention
of showing the production site. Renames the i18n key accordingly.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The URL bar / device toggles / Blocks toolbar was gated on `daemonReady`,
so during the production-fallback boot window (daemon still provisioning)
the canvas showed only the "starting" pill with no topbar. Show the toolbar
whenever an iframe surface is visible: the production fallback gets it right
away; sandbox mode still waits for the daemon. Page/Blocks controls fill in
once the daemon is up (they read the committed snapshot, empty until then).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Give the waking toast an opaque grey (bg-muted) card with border/shadow so it
contrasts against both the grey boot canvas and a white production page while
it loads, and add a description line clarifying that edits made now will show
in the preview once it's ready.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Fresh/Deno deco sites never persist meta.gen.json to the FS (the schema only
exists at runtime), so the Blocks panel stayed stuck "loading" until the dev
server booted — even though the blocks (decofile) were already readable from
the committed .deco/blocks/ sources.

Pull the schema from the linked production site's /live/_meta as a last-resort
source: production is a live deco runtime serving the same route (and sends
`access-control-allow-origin: *`), so the browser can read it directly. Priority
is live (dev server) → committed snapshot (branch-accurate) → production, so a
committed meta.gen.json still wins; production only fills the gap for sites that
don't commit one. Content (decofile) intentionally stays FS/branch-sourced.

Reuses the persisted metadata.productionUrl; the ordering is a pure,
unit-tested helper.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Clarify that metadata.productionUrl ideally isn't set by hand — it's
auto-populated at import and should be sourced from the site's `domains`
array once the domain-setup capability lands.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@guitavano
guitavano force-pushed the guitavano/sandbox-prod-preview-fallback branch from 968b98a to f98896f Compare July 23, 2026 11:56
…light

Review remediation:
- Blocker: resolvePreviewDisplay now yields the canvas for the `othersThread`
  state too, so a teammate's-branch confirmation gate no longer leaks the
  toolbar / production iframe / waking pill behind it (+ test).
- Important: single-flight the daemon's blocks.gen.json regeneration so
  concurrent cold reads on a large shared sandbox share one rebuild instead of
  piling ~11MB serialization bursts on the event loop (health-probe safety) (+ test).
- Tighten the daemon comment: the merge avoids per-value JSON work, but the
  response's full-payload stringify is downstream (no worse than the committed
  read path).
- Extract pickProductionDomain helper (dedup the import-dialog domain pick) (+ test).
- Document why useDecofile has no production fallback (content stays
  branch-accurate; schema in useLiveMeta can't).
- Add the missing metaSourceOrder "live + no production" case.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@guitavano
guitavano merged commit 9b02b72 into main Jul 23, 2026
15 checks passed
@guitavano
guitavano deleted the guitavano/sandbox-prod-preview-fallback branch July 23, 2026 12:41
decocms Bot pushed a commit that referenced this pull request Jul 23, 2026
PR: #5084 feat(sandbox): show production site while the preview dev server wakes
Bump type: minor

- decocms (apps/mesh/package.json): 4.105.0 -> 4.106.0
- @decocms/mesh-sdk (packages/mesh-sdk/package.json): 1.31.0 -> 1.32.0
- @decocms/sandbox (packages/sandbox/package.json): 1.19.0 -> 1.20.0
- deploy/helm/sandbox-env (chart 0.9.12) (deploy/helm/sandbox-env/values.yaml deploy/helm/sandbox-env/Chart.yaml): image.tag/appVersion -> 1.20.0

Deploy-Scope: both
rafavalls added a commit that referenced this pull request Jul 23, 2026
Only conflict was apps/mesh/src/web/components/sandbox/preview/preview.tsx,
where main's new preview "display model" (#5084 production fallback while the
dev server wakes, #5089 resilient path-param picker, #5086 confirm-before-
auto-start) collided with this branch's header-portal redesign of the same
toolbar.

Resolution: keep the redesign's structure (urlControls / moreMenu extracted and
portaled into the panel header via headerSlot) and adopt main's display model:
- showPreviewToolbar now follows main's visibility — shown once an iframe
  surface is active (sandbox when the daemon is up, or the production fallback
  while it wakes) instead of `daemonReady && previewState.kind === "iframe"`.
- canVisualEdit uses `display.mode === "sandbox"`.
- "Open in new tab" resolves the URL via `iframeSrc ?? display.iframeBase`
  (previewState.previewUrl no longer exists under the display model).
Main's toolbar-internal changes were pure re-indentation (verified against the
base diff), so the extracted urlControls/moreMenu already carry them; main's
other features (waking pill, confirm dialog) live outside the toolbar and
auto-merged.

Verified: tsc across all workspaces, oxlint (0), knip (clean), format.
guitavano added a commit that referenced this pull request Aug 4, 2026
…en absent (#5687)

* feat(sandbox): regenerate blocks.gen.json in the Go daemon's /read when absent

`.deco/blocks.gen.json` is the runtime's merge of every `.deco/blocks/*.json`;
most repos gitignore it (a multi-MB single line that conflicts on every content
PR and is regenerated on dev cold-start), so a fresh sandbox has the block
sources but not the merged artifact. The web CMS reads it via the daemon's
/read proxy (readCommittedJson -> use-decofile) so the Blocks UI is editable
before the dev server boots.

The TypeScript daemon synthesized this merge on the fly in /read (added in
#5084) but the behavior was dropped when that daemon was deleted (#5575) and
never ported to Go — so on main today /read 400s and the CMS shows "decofile
unavailable" until the dev server is up. Restore it in Go:

- decofile.GenerateFromBlocks / GenerateFromBlocksDeduped: deterministic,
  byte-spliced merge keyed by the decode-until-stable filename stem (fixes the
  double-encoded-stem bug the old decode-once path had), single-flight
  coalesced so concurrent cold reads share one multi-MB build.
- fs.Read falls back to the merge only when the absent path's basename is
  blocks.gen.json; every other absent file still 400s.
- Go unit tests for the merge + a black-box daemon-e2e spec.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* review: harden the decofile /read fallback

Addresses a multi-perspective review of the fallback:

- security: gate the fallback on a relative path. An absolute body.Path
  bypasses SafePath, so allowing it would turn the merge into an arbitrary
  sibling-blocks/ directory glob; the CMS only ever sends relative paths.
  Proven by a routes unit test that points an absolute path at the real repo's
  gen artifact and asserts it still 400s while the relative form merges.
- robustness: defer the singleflight cleanup so a panic in the merge can't
  leave a stale in-flight entry that wedges every future reader of that dir.
- testing: cover the riskiest path — pretty-printed multi-line blocks round-
  tripping through the consumer's stripLineNumbers (inlined in the e2e, unit-
  asserted no merged line starts with a line-number prefix) — plus a concurrent
  -race test for the coalescer.
- cleanup: unexport generateFromBlocks (only the deduped wrapper calls it),
  splice via bytes.TrimSpace to drop a per-block string copy, and broaden the
  package doc to cover generation alongside validation.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant