Skip to content

fix(widget): survive foreign hashes and harden the mount path #92

Closed

Description

@Ardnived

Summary

Several mount-path defects make the embed fragile on real host pages: a host URL carrying any pre-existing #anchor renders the widget blank; on a clean URL the mount-time hash write pushes a host history entry (first Back press appears to do nothing); a second copy of the embed script throws on customElements.define; a second <sahaj-atlas> instance silently reuses the first's API key and theme singletons; and a throw from Providers/BrandTheme — above the app's error boundary — blanks the widget with no fallback. Two adjacent correctness items ride along: the widget root sets no lang (WCAG 3.1.2 failure for any non-host-language embed) and an un-gated og:locale Helmet write mutates the host <head> in embed mode. Ship-blocker #2 + the §2 reliability gaps of the release-readiness review.

Evidence (verified against the working tree, 2026-08-06)

  1. Foreign hash → blank widget. The boot guard at src/Widget.tsx:56–67 writes the widget hash only when the hash is empty or exactly #! / #!/. A host URL already carrying #respond, #comment-123, #contact is left untouched; react-router's hash location then strips the ! basename, gets null, and renders nothing. No fallback, no test.
  2. Mount pushes history. The same guard assigns window.location.hash = ... (src/Widget.tsx:65) on a clean URL — a hash assignment adds a history entry, so the visitor's first Back press appears to do nothing. This contradicts the "never pop/pollute host history" contract the dismissal logic (dismissAction) otherwise carefully honours.
  3. Unguarded define. customElements.define('sahaj-atlas', …) at src/Widget.tsx:101 has no customElements.get() guard — a second copy of the script (plausible: the docs name two different bundle URLs) throws NotSupportedError in the host console.
  4. Multi-instance fails silently. src/Widget.tsx:40–42 keeps the FIRST apiKey for every later instance, and src/config/theme/BrandTheme.tsx:69–79 documents the theme-root + system-watcher singletons ("Assumes one widget per page; a second concurrent embed would share these singletons").
  5. No boundary above Providers/BrandTheme. In src/App.tsx:68–78 the app's ResetErrorBoundary sits inside BrandTheme, so a throw from Providers or BrandTheme unmounts the widget silently (it does not take down the host page — but nothing renders and nothing reports).
  6. No lang on the embedded subtree. The theme-root wrapper (src/Widget.tsx:83–88) sets only className + dir — a French widget on an English host announces with English pronunciation (WCAG 3.1.2). There is also no landmark on the widget root.
  7. Un-gated head write. src/App.tsx:163–165 emits <meta property="og:locale"> via Helmet unconditionally — in embed mode that mutates the host page's <head>.

Proposed changes

  • Treat a hash that doesn't start with #! as "no route" and boot at basePath — without destroying the host's anchor. Options: render off basePath in-memory until the first in-widget navigation, or defer the #!… hash write to that first navigation. Hard requirement either way: the widget renders AND the host's #anchor still scroll-works on load.
  • Never push history at mount: use history.replaceState (not a hash assignment) for any initial hash write on a clean URL.
  • Guard the definition: skip customElements.define when customElements.get('sahaj-atlas') already resolves.
  • Make multi-instance explicit: a second <sahaj-atlas> on the same page renders a clear "one atlas per page" notice (or console.warn + no-op) — decided and implemented, never silently running on instance A's key/theme.
  • Top-level boundary: add a minimal error boundary above Providers/BrandTheme with a static, translation-free fallback — it must not depend on i18n, the theme engine, or the query client (anything that can itself be what failed).
  • Widget root a11y: set lang={activeLocale} (reactive, like dir) on the theme root, and add a landmark — role="region" + a localized aria-label — on the widget root.
  • Gate the head write: emit og:locale only when standalone.
  • Extract the hash-classification decision ("foreign / ours / empty") as a pure helper in src/lib/shape/ so the node lane can test it (per .claude/rules/tests.md).

Acceptance criteria

  • With the host URL carrying #respond (and #comment-123), the widget renders its root view at basePath, and the host page still scrolls to that anchor on load.
  • On a clean URL, mounting the widget adds no history entry — the first Back press leaves the page.
  • A widget hash the visitor already navigated to (#!/…) still wins at mount — deep links keep working, and a Widget re-render (locale change) never teleports back to basePath (existing booted semantics preserved).
  • Loading the embed script twice does not throw NotSupportedError (guarded via customElements.get).
  • A second <sahaj-atlas> element on the same page produces the chosen explicit behavior (visible notice, or console.warn + no-op) — never a silent run on instance A's API key or theme.
  • A forced throw from Providers/BrandTheme renders the static translation-free fallback instead of a blank widget.
  • The widget root carries lang (tracks the active locale, updating on language switch), role="region", and a localized aria-label (new locale key added across all languages via pnpm i18n:add).
  • og:locale is emitted only in standalone mode — the embed no longer writes to the host <head>.
  • Hash classification is a pure helper in src/lib/shape/ with co-located node-lane tests covering: empty hash, #!, #!/, a real widget route (#!/gb/london), foreign anchors (#respond, #comment-123), and hostile input (#!//evil.example, #!javascript:… — composing with the existing safePath guard).
  • demo.html-based manual verification list executed and recorded in the implementation report: foreign-anchor URL, clean URL + Back press, double script include, two <sahaj-atlas> instances, forced provider throw, non-English locale attribute announced correctly (VoiceOver or equivalent spot check).
  • pnpm lint + pnpm typecheck + pnpm test:run + pnpm build pass.
  • Write an implementation report to .claude/reports/92-widget-mount-hardening.md — in the MAIN checkout at /Users/devindra/Documents/Projects/SahajAtlasWeb even when implementing from a worktree (the folder is gitignored) — covering what changed, what was deliberately NOT changed, and which readiness-report findings are resolved.

Files affected

Owned by this ticket:

  • src/Widget.tsx
  • src/App.tsx
  • New small pure helpers in src/lib/shape/ + their co-located *.test.ts

Parallel implementation: this ticket OWNS the files listed above; sibling release-hardening tickets are implemented concurrently, so changes outside the owned files must be avoided or kept strictly additive. src/config/theme/BrandTheme.tsx is cited as evidence only — if the multi-instance decision requires touching it, keep the change strictly additive; the new aria-label locale key is additive by nature (pnpm i18n:add across public/locales/*). Note the CSS-scoping sibling ticket may additively add a scope class to this file's theme-root wrapper — leave room for that.

References

  • .claude/reports/RELEASE_READINESS.md §2 (gaps), §3.1, ship-blocker Community Room not displayed on sahajayoga.ca website #2, Appendix B (local, gitignored — for repo-local implementers; all load-bearing evidence is inlined above)
  • react-router hash routing: createHashLocation + stripBasename return null for a location outside the basename — the mechanism behind the blank render
  • WCAG 3.1.2 (Language of Parts); WCAG landmark guidance for embedded regions
  • .claude/rules/tests.md — node-lane conventions for the pure-helper tests
  • .claude/rules/i18n-and-state.md — the history-aware dismissal contract the mount path currently contradicts
  • Sibling ticket (same batch): fix(ui): scope widget css so it cannot restyle the host page

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions