Skip to content

feat(preview): block link navigation in live-preview routes - #49

Merged
Ardnived merged 2 commits into
mainfrom
feat/preview-block-link-navigation
Jun 30, 2026
Merged

Ardnived merged 2 commits into
mainfrom
feat/preview-block-link-navigation

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

  • Makes every link in the live-preview routes (/preview and /preview/embed) inert so editors don't navigate the SahajCloud preview iframe away from the document they're editing.
  • Same-page anchor jumps (#heading) still scroll; <button> and media controls (play/pause, captions, embed dropdown) and the message-based live-preview/seek sync are untouched.
  • Zero impact on the normal site — the guard is mounted only while <Preview> renders.

Changes

  • pages/preview/_components/previewNavigation.ts (new) — shouldBlockPreviewLink(href) predicate (block everything except #hash/href-less anchors) + usePreviewLinkGuard() hook that installs capture-phase click/auxclick listeners on window and calls preventDefault() + stopPropagation() on blocked anchors.
  • pages/preview/_components/Preview.tsx — calls usePreviewLinkGuard() once at the top of the shared Preview component (rendered by both preview routes).
  • pages/preview/_components/previewNavigation.test.ts (new) — unit tests for the decision logic.

Why a window-capture listener + stopPropagation()

Vike's client router listens for link clicks on document in the bubble phase and does not check event.defaultPrevented (initOnLinkClick.js). So preventDefault() alone wouldn't stop Vike's client-side navigation. A capture-phase listener on window is the first node in the event path, so stopPropagation() there halts all further propagation and the event never reaches Vike's document handler; preventDefault() additionally blocks native navigation and middle-click/modifier "open in new tab" (via auxclick). A single document-level interceptor also covers the Header/Footer nav that LayoutChrome renders outside <Preview>, without threading a disabled prop through every link-rendering component.

Test Results

  • Lint: ✓ No errors (pnpm lint)
  • Types: ✓ Clean (pnpm exec tsc --noEmit)
  • Tests: ✓ 272 passed, incl. 4 new (pnpm test:run)
  • Build: ✓ Success (pnpm build)

Manual verification

Open the SahajCloud live preview for a page, a meditation, and a lecture, in both /preview and /preview/embed:

  1. Click a rich-text link, a nav/header link, a card, and a breadcrumb → URL/iframe stays put, no navigation.
  2. Cmd/Ctrl-click and middle-click a navigating link → no new tab opens.
  3. Click a table-of-contents #heading link → still scrolls within the document.
  4. Use play/pause, the captions menu, and the Embed dropdown (open + copy) → all still work.
  5. Confirm live edits in the admin still update the preview and frame-thumbnail seek sync still works.

Notes for reviewer

  • Tests cover the pure shouldBlockPreviewLink decision logic. The DOM wiring (capture phase, auxclick, Vike interaction) isn't unit-tested because the project runs Vitest in node with no jsdom — it's covered by the manual steps above.
  • Considered and deferred — altitude: a /simplify pass suggested moving the hook into a dedicated preview layout. Kept it inline in Preview because this repo's layouts are config-driven (Layout: in +config.ts) with a deliberate (full) vs embed chrome split; a guard layout would add routing machinery/risk for a one-line effect, and Preview is the single shared mount point both routes already render.
  • Known minor limitation: because the guard calls stopPropagation(), clicking an <a> inside an open dropdown won't auto-close that dropdown in preview (Floating UI's outside-press dismiss never sees the click). Navigation is still correctly blocked, the EmbedButton dropdown has no anchors so it's unaffected, and clicking elsewhere still closes it. Not worth complicating the guard.

Closes #46

🤖 Generated with Claude Code

Ardnived and others added 2 commits June 30, 2026 11:18
Editors working in the SahajCloud live-preview iframe lost their session
whenever they clicked a link, because preview routes render the real site
components (rich-text links, nav, cards, breadcrumbs) and every link is
genuine navigation. Add a capture-phase click/auxclick guard, mounted only
while <Preview> renders, that blocks anchor navigation across the whole
preview tree — including the Header/Footer chrome LayoutChrome renders
outside <Preview>.

Same-page hash links (#heading) still scroll; buttons and media controls
(play/pause, captions, embed dropdown) and the message-based live-preview /
seek sync are untouched. Because Vike's client router listens in the bubble
phase and ignores defaultPrevented, the guard runs in the capture phase and
calls both preventDefault() and stopPropagation().

Refs #46

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Refs #46

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying wemeditate-design with  Cloudflare Pages  Cloudflare Pages

Latest commit: e4d3dff
Status: ✅  Deploy successful!
Preview URL: https://fd1dfbe8.wm-design.pages.dev
Branch Preview URL: https://feat-preview-block-link-navi.wm-design.pages.dev

View logs

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
wemeditate-web e4d3dff Commit Preview URL

Branch Preview URL
Jun 30 2026, 10:29 AM

@Ardnived
Ardnived merged commit dac4bb8 into main Jun 30, 2026
5 checks passed
@Ardnived
Ardnived deleted the feat/preview-block-link-navigation branch June 30, 2026 10:46
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.

feat(preview): block link navigation in live-preview routes

1 participant