Skip to content

perf(api): spike — separate the region hierarchy from the event feed - #53

Merged
Ardnived merged 11 commits into
mainfrom
perf/region-feed-split
Jul 14, 2026
Merged

Ardnived merged 11 commits into
mainfrom
perf/region-feed-split

Conversation

@Ardnived

@Ardnived Ardnived commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Spike for #32 — restructure the data layer so the locale-agnostic region hierarchy caches once, decoupled from the per-locale event feed, and per-navigation /api/regions reads disappear. Prototype behind this branch; no production behaviour change to valid content. Go/no-go finding: GO with S3 — full write-up in .claude/docs/spike-32-region-feed-split.md.

  • No per-navigation /regions reads — one wholesale ['regions'] dict (513 regions, ~105 kb) replaces getRegionDoc + getChildRegions; ancestry / children / counts / the 0-event gate all derive from it client-side.
  • Agnostic feed + per-locale titles (S3) — the geojson feed drops the localized title (and breadcrumbs, ~20% of its weight); a ~28 kb ['event-titles', locale] sliver is joined by id. A language switch now refetches ~28 kb instead of the whole ~576 kb feed (~20×), at equal cold-load.
  • Latent stale-title bug fixed — locale is now in every locale-dependent query key; the agnostic ['geojson'] / ['regions'] / ['countries'] keys stay locale-free.
  • Locale handling simplified — dropped the drift-prone client-side toSahajLocale remap (+ SAHAJCLOUD_LOCALES); the interceptor sends i18n.resolvedLanguage directly and queries key by plain locale. The language sets are kept aligned by policy, not a client remap (see below).

Changes

  • src/config/api/fetch.ts — wholesale getRegions/loadRegions; getRegion/getCountries derive from the cached dict; getRegionDoc/getChildRegions dropped; agnostic feed + getEventTitles title-join; countryCode from slug (refactor(state): make four implicit lifecycles explicit state machines #556) with a transitional legacyData fallback; 0-event 404.
  • src/lib/shape/hierarchy.ts — region-tree helpers (indexRegions / childrenOf / ancestorIds); breadcrumb ancestry removed.
  • src/types/{event,geojson,region,region-ref}.ts — AgnosticFeedEventSchema / EventTitleSchema / RegionNodeSchema; breadcrumbs retired.
  • src/components/organisms/Mapbox/Map.tsx — trim feed properties to a geometry-only Mapbox source.
  • src/config/api/client.ts + views + useLocale — locale-dependent keys carry the active locale; interceptor sends i18n.resolvedLanguage directly (no toSahajLocale/sahajLocale).
  • .claude/docs/spike-32-region-feed-split.md — the written go/no-go finding.

Verification

  • Lint: ✓ (pnpm lint)
  • Types: ✓ (pnpm typecheck)
  • Tests: ✓ unit lane green (pnpm test:run)
  • Build: ✓ (pnpm build + pnpm ladle:build)

Manual / visual verification

Driven in-browser (Playwright MCP) against the local :3000 seed (507 events / 513 regions):

  1. No per-navigation /regions — home → Belgium → Brussels fired exactly one wholesale /api/regions.
  2. Locale switch (en → fr) on a region view refetched only /api/events?select[title]&locale=fr (~28 kb) — the feed and the regions dict stayed cached.
  3. Both list surfaces render with joined titles — RegionView (region events) and SearchView / DynamicEventsList (global online list, with correct dict-derived ancestry in the nested URLs).
  4. 0-event region 404s — /luxembourg renders the error boundary, not an empty page.
  5. Flags / countryCode render on name-slugs via the transitional legacy fallback.
  6. Post-cleanup smoke — every request (/api/regions, /events/geojson, /api/events) carries the correct locale with the interceptor now sending i18n.resolvedLanguage directly; 0 console errors.

Notes for reviewer

Closes #32

🤖 Generated with Claude Code

Ardnived and others added 8 commits July 14, 2026 15:28
Replace the per-navigation getRegionDoc/getChildRegions and getCountries'
region read with one wholesale ['regions'] dict (every level, depth 0),
cached on a long stale window. Ancestry, child lists, counts, and region
routes now derive from it client-side via new region-tree helpers
(indexRegions/childrenOf/ancestorIds) — dropping the feed's breadcrumb
dependency for aggregation. countryCode derives from the country slug
first (post-SahajCloud#556), falling back to legacyData for the
pre-migration local seed (transitional).

Refs #32

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The geojson feed carried the localized event `title`, so a bare ['geojson']
key served stale titles after a language switch (and refetched the whole
feed). Split `title` into a lean id->title sliver (GET /api/events, select
title only) keyed by locale, joined back by id when building event list
items. The feed is now locale-agnostic — cached once, no locale in its key —
and also drops region `breadcrumbs` (~20% of its weight; ancestry comes from
the regions dict). A language switch now refetches only the ~5% titles sliver.

The map / search / filter surfaces read the agnostic feed directly (no title);
only getRegion + getEvents join titles.

Refs #32

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The map renders geometry and injects its own cluster properties; the click
handler needs only `id` + `webPath`. Strip every other feature property
before handing the collection to the vector source, so Mapbox holds a lean
geometry source instead of the full agnostic feed's card fields
(address/schedule/languages/region).

Demonstrates the spike finding that map-source leanness is a client-side
trim, not a reason for a separate lean feed query (S2/S4).

Refs #32

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The feed/region/event reads carry the localized event `title`, but their
query keys omitted locale — so a runtime language switch (SettingsMenu ->
i18n.changeLanguage) served stale-locale titles until the entry went stale.
Add `sahajLocale` to useLocale and thread it into every locale-dependent
key: ['region', slug, locale], ['event', id, locale], ['events', ..., locale].
The agnostic ['geojson'], ['regions'], and ['countries'] keys stay locale-free
(no localized field); ['event-titles', locale] already keys by it.

Refs #32

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
getRegion now throws when a region's subtree holds zero events (located or
online), so the nearest ErrorBoundary renders the not-found state instead of
an empty region page. Mirrors getCountries already hiding 0-event countries
from the home list. Reachable only by a direct URL — region cards link only
to regions that have events.

Refs #32

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The written finding for the spike: measured basis (wire-cost benchmark +
the runtime signals verified in-browser), the S3 split decision (S1-S4
table), region-count sizing, feed weight + locale-switch cost, per-drawer
latency, what the prototype implements, and the residual follow-ups
(countryCode-from-slug gated on the local seed reflecting #556).

Refs #32

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Retire the now-dead breadcrumbs schema (RegionRefSchema.breadcrumbs +
  BreadcrumbSchema) — ancestry comes from the regions dict; completes the
  breadcrumb-free feed contract.
- Drop RegionIndex.childrenByParent (derivable state with a single reader that
  getCountries built and never read); childrenOf is now a filter.
- toMapSource takes the features array (drop the throwaway {...data} spread).
- Comment accuracy (stale filtered-memo + getRegions notes) and finding-doc
  residuals (breadcrumbs retired; locale-key query factory recommended).

Refs #32

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Review follow-ups (both reviews were otherwise clean):
- EventTitleSchema tolerates a null title (coalesced to '' in getEventTitles)
  so one bad row can't fail the whole titles read and break every
  title-loading view.
- Drop .passthrough() from RegionNodeSchema.legacyData so the legacy blob
  (osmId/legacyId/mailingService/…) is stripped at the zod boundary and never
  reaches the public ['regions'] cache (transitional — goes with #556).
- Tests: an online-only region must NOT 404 (it rolls up); a null title parses.
- Doc: legacyData wire-exposure + dict-ancestry-assumption residuals.

Refs #32

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

cloudflare-workers-and-pages Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Deploying sahajatlas with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2f0f912
Status: ✅  Deploy successful!
Preview URL: https://d9d8b43e.sahajatlas.pages.dev
Branch Preview URL: https://perf-region-feed-split.sahajatlas.pages.dev

View logs

The widget's language codes match SahajCloud's 1:1, so the `toSahajLocale`
mapping (+ its hand-maintained `SAHAJCLOUD_LOCALES` set, which had already
drifted — lowercase `pt-br`, no `en-AU`) was redundant and papered over a
language-set misalignment. Remove both: the interceptor now sends
`i18n.resolvedLanguage` directly, and every locale-dependent query keys by
plain `locale` (dropping `sahajLocale` from useLocale). SahajCloud falls back
to its default for any locale it doesn't recognize (verified on /api/regions
and /events/geojson), so this is safe; keeping the sets aligned is a policy —
a missing language is a SahajCloud ticket (sydevs/SahajCloud#578 for hu/nl),
not a client-side remap.

Refs #32

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

cloudflare-workers-and-pages Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Deploying sahajatlas-design with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2f0f912
Status: ✅  Deploy successful!
Preview URL: https://1122469d.sahajatlas-design.pages.dev
Branch Preview URL: https://perf-region-feed-split.sahajatlas-design.pages.dev

View logs

@Ardnived

Copy link
Copy Markdown
Contributor Author

Re-triggering CI — GitHub dropped the pull_request synchronize event for 8e36252 (no Actions run was created; Cloudflare deployed it fine). Reopening.

@Ardnived Ardnived closed this Jul 14, 2026
@Ardnived Ardnived reopened this Jul 14, 2026
Ardnived and others added 2 commits July 14, 2026 18:32
GitHub dropped the pull_request event for the previous push (8e36252); no
Actions run was created. Empty commit to re-schedule the gate.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Brings in #49 (pin-hover highlight) and #50 (date-range filter). Two
conflicts, resolved by combining both sides:
- Map.tsx `filtered` memo + fetch.ts `getEvents`: keep #50's `today` arg to
  matchesFilters AND this branch's geometry-only toMapSource trim /
  per-locale titles join.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Ardnived
Ardnived merged commit 3c3cfa6 into main Jul 14, 2026
4 checks passed
@Ardnived
Ardnived deleted the perf/region-feed-split branch July 14, 2026 20:18
Ardnived added a commit that referenced this pull request Jul 16, 2026
Resolve API-layer conflicts from the #53 region-hierarchy / event-feed
split:
- adopt main's RegionNode model + wholesale getRegions() tree; migrate the
  live-preview region-by-id read to getRegionNodeById (looks the node up in
  the cached tree instead of a per-region fetch) and PreviewController from
  RegionDoc -> RegionNode.
- keep regionRoute exported for the preview controller.
- fetch.test.ts: keep the shapeEventDoc import, drop the now-removed
  toSahajLocale; shorten a preview-secret test fixture so the merged-in
  security-scan hook doesn't false-positive on it.

client.ts auto-merged. Gate green: lint + typecheck + 240 tests + build.
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.

perf(api): spike — separate region hierarchy from the event feed

1 participant