Skip to content

feat(widget): send the embed report and publish a readiness marker #153

Closed

Description

@Ardnived

Summary

#149 shipped the detection half of embed reporting but not the send: boot() takes the payload
and drops it (void report — src/Widget.tsx:377), with a comment noting the receiving endpoint
was a separate SahajCloud ticket. That endpoint now exists and is deployed —
POST /api/clients/report, sydevs/SahajCloud#633 (PR #636) — so the send can be switched on.

Switching it on as-is would fail, though: two payload mismatches mean every report 400s, and the
mount key silently collapses on WordPress. Separately, SahajCloud's follow-up verifies embeds
server-side and needs one thing #149 never specced — a readiness marker in the DOM.

Verified against the shipped code at 8e6c4ce and a SahajCloud instance running the new schema.

1. Payload shape — every report currently 400s

Widget sends (src/loader/report.ts) Endpoint expects
mount: "https://host/path" (one field) origin and pathname as separate fields
mode: 'component' | 'iframe' mode: 'inline' | 'iframe'

The split is not cosmetic — the endpoint rejects a pathname carrying a query string or fragment,
and it can only enforce that if the path arrives on its own. mount is still the storage key; the
server rebuilds it as origin + pathname.

On mode: component is arguably the better name, but the CMS contract is deployed and published
at /api/docs, so the cheaper fix is for the widget to send inline. Say if you'd rather the CMS
rename — its column is JSON, so that side is a validator change with no migration.

canonicalViable is extra as far as the endpoint is concerned; unknown keys are stripped, so it is
harmless to keep sending.

2. mountKey() collapses every WordPress page onto one mount

mountKey() returns origin + pathname, discarding the query. On a WordPress site using default
permalinks every page is /?p=<id>, so every post reports as https://site.com/ — all mounts
collapse into one record, and the page an operator needs to designate as canonical cannot be named
at all. That is precisely the audience this program targets.

The endpoint was changed for this: a path may now carry ?p=<digits> and nothing else. Every
other query string is still refused, for the reason hostPageUrl() already documents — a host query
can carry a reset token or an OAuth code, and none of that may leave the page. A numeric post id is
not seeker input.

So mountKey() should preserve ?p=<digits> and continue discarding everything else, including
?p=123&utm_source=….

3. Readiness marker — new, not something #149 missed

SahajCloud only lets a server-verified embed yield a canonical URL: the CMS loads the page itself
via Cloudflare Browser Rendering and confirms the widget really booted. This closes a hole where a
forged report could otherwise reshape a live canonical URL — reports nominate a candidate, the
server decides.

A postMessage cannot serve this. The Browser Rendering REST API renders a page and returns DOM,
with no message channel, and an injected listener races the widget's own boot — which would
false-FAIL healthy sites. A DOM attribute is raceless and carries the state the verifier needs:

document.documentElement.setAttribute(
  'data-sahaj-atlas-ready',
  JSON.stringify({ v: 2, routing: 'query', topLevel: true, urlWritable: true }),
)

Two requirements:

  • Set it only after the widget has genuinely mounted and rendered, never on script load. A
    marker set on load attests to nothing and makes verification theatre.
  • Its JSON is a cross-repo contract, hence the v field. The verifier reads routing from it, which
    is what makes routing server-attested rather than self-reported.

No postMessage is wanted — the verifier is the only consumer, so one mechanism, not two.

4. Report on every mount, not only on change

#149 gates the POST on the fingerprint differing from /clients/me. That makes lastSeen useless
as a liveness signal: a healthy, unchanged embed reports once, ever, and the admin panel would
show "last seen 8 months ago" for a working site.

The server already suppresses unchanged reports for an hour, so reporting on every mount costs at
most one write per mount per hour. The differs-from-/clients/me gate then becomes unnecessary —
which also removes a round trip from the load path.

5. Two things worth knowing about the endpoint

  • allowedDomains must be non-empty, and the reported origin must match it. Unlike the read
    surface, an empty allowlist is refused (403) rather than allowing everything — with no allowlist
    there is nothing to attribute a mount to. An unconfigured service simply cannot report.
  • A client may accumulate at most 50 distinct mounts; a new one past that is 429. Known mounts keep
    reporting normally.

6. Cleanup: legacyConfig is gone

SahajCloud removed the field. ClientSchema (src/types/client.ts:18) still declares it and the
/clients/me query still selects it. It is .nullish() and never read for behaviour, and I
confirmed a stale select[legacyConfig]=true still returns 200 — so nothing breaks. It is dead
weight to drop when convenient, along with the regenerated payload-types.ts.

Acceptance criteria

  • The report POSTs to POST /api/clients/report and a successful send is asserted in the node lane.
  • Payload sends origin + pathname separately, and mode uses the endpoint's enum.
  • mountKey() preserves ?p=<digits> and discards every other query string and all fragments — asserted both directions.
  • data-sahaj-atlas-ready is set only after a genuine mount, carrying v, routing, topLevel, urlWritable.
  • The report fires on every mount; the differs-from-/clients/me gate is removed.
  • A 403 or 429 from the endpoint is a console diagnostic, never a thrown error in the host's page.
  • legacyConfig dropped from ClientSchema and the /clients/me select.

References

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