Guides

Performance audit: slowest data-access paths in the site slice

Ranked audit of every data-access path in pack app-campus.site.2, slowest first, with the specific rewrite to ship for each path. This page itself ships the first static-page rewrite, with stylesheet preload and with below-fold sections that render only when they near the viewport.

Scope of this audit

Read this section before you change a data-access path. The pack holds 18 public guide files only. It holds no SQL statement, no migration, no API module, and no server endpoint, so no database index and no query rewrite for a server table can be shipped from this slice. The paths below are the paths that exist in these files, namely browser storage reads and writes, repeated page scans, and search matching. A server index for an app table belongs in the app slice, in that table migration, and it must filter by tenant as the pack rules require.

  • Files audited: the 18 guide files listed in the pack, from role-features through workspace-member-audit
  • Storage key in use across the slice: campus-answers:my-answers:v1, read and written in save-answer-bundle and in share-answer
  • Other storage keys in use: campus-answers:role-first-run:v1 for one college and one role, and campus-answers:canonical-record:v1 for one record
  • Search surfaces audited: the guides index, the terminology page, and My Answers, as inventoried in search-indexing and in search-scope

Ranked list: slowest paths first

Rank is by growth risk first, then by how often the path runs. A path with no cap and a full scan on every use ranks above a capped path that runs often.

Slowest data-access paths in this slice, with the rewrite to ship for each path
Rank and pathWhy it is slowRewrite to shipWhat stays the same
Rank 1. My Answers store parse and full scan on every save, in public/guides/save-answer-bundle.html and in public/guides/share-answer.htmlEach save parses the whole stored library with JSON.parse, scans every saved item in a linear duplicate check, and rewrites the whole library. Cost grows with every saved item with no cap and no index, so a large library makes every save slower.Keep the parsed store in memory after the first read, build one Map index keyed by title, source link, workspace, and assistant for the duplicate check, look up in that Map instead of scanning the list, and update the Map on write. Invalidate the cache only when a write succeeds.The same storage key, the same record shape, the same field limits, and the same duplicate rule. No saved item changes format.
Rank 2. Repeated full page scan for checkboxes, in public/guides/role-first-run.htmlThe helper that lists step checkboxes scans the whole page on every render, on every panel change, on every copy, and on every complete check. That is a repeated full scan for a fixed set of about twenty controls.Read the checkbox list once at page start, group it once by role in a Map, and reuse that grouped list in render, load, copy, and complete. Parse stored progress only when the college name or the role changes.The same steps, the same storage key, the same progress wording, and the same completion rule.
Rank 3. Unindexed search matching on every search, on the guides index and the terminology pageEach search compares every typed word against every card or entry, with fuzzy one-letter-off matching for words of four letters or more. Cost grows with the number of cards and with the length of the typed words, and it runs after a pause while the reader types.Precompute normalized tokens once per card or entry at page load, keep one token index by first letter, check exact and word-start matches before fuzzy matches, and stop at the first failed typed word. Keep the 120 millisecond pause and the shared query rules in search-scope.The same match rules, the same empty states, the same counts, and the same scope labels. No full guide text is added to the index.
Rank 4. Weekly table rebuild on every recorded outcome, in public/guides/weekly-quality-rate.htmlEach outcome copies the weekly rows, sorts the week keys, and rebuilds the table. The event list is already capped at 100 and the rows are already cached by version, so this path is bounded, but a rebuild still runs on every click.Keep the version cache, update only the row for the current week instead of rebuilding the table, and keep the cap at 100 events. Do not add new events for this path.The same week bucket, the same rates, the same CSV columns, and the same event names.
Rank 5. Render-blocking stylesheet discovery on long guides without preloadGuides that load theme.css and site.css with no preload make the browser discover those files late, and long below-fold sections render on first paint even when the reader never scrolls to them.Add preload for theme.css and site.css with fetchpriority high, as work-unanswered already does, and add content-visibility auto with an intrinsic size to below-fold sections. This page ships both rewrites.The same stylesheets, the same layout, and the same script loading with defer. No long-lived caching is added to files whose names do not change.

Ready-to-ship caching for reference data on read-heavy GET routes

Use the Workers Cache API, caches.default, for reference data that rarely changes and is read on almost every list and page load. Cache only GET 200 responses that hold no personal detail. Every key below is explicit, every TTL is stated, and every write that changes the data deletes the matching keys in the same release and the same handler after the write commits. This contract does not change sign-in, tenancy, roles or permissions. A signed-in key always contains tenant_id derived from the session, never a token, and a private response is never served from a shared public key.

Reference data, explicit cache key, TTL, and invalidation on write
Reference data and read-heavy GET routeExplicit cache keyTTL and response headersInvalidation on write
Public guide cards and terminology entries, GET /guides/ and the terminology listcampus:ref:guides:v{guide_rev}:{lower-trimmed-q-or-empty}:{role-or-empty}:{stage-or-empty}:{topic-or-empty}3600 seconds, public, max-age=3600, stale-while-revalidate=300, strong ETag, Vary: Accept-Encoding only. Cache GET 200 only.Guide publish or deploy bumps guide_rev, deletes keys with prefix campus:ref:guides:v{old-rev}:, and the new revision is used for the next key. No per-tenant part is needed because this data is public and holds no workspace record.
Public assistant and source metadata for the widget, title, status, owner office, review date and handoff office name only, GET public assistant metadatacampus:ref:public-meta:v{source_rev}:{tenant_id}:{assistant_id}300 seconds, public, max-age=300, stale-while-revalidate=60, strong ETag, Vary: Accept-Encoding only. No question text, answer text, source body or contact detail beyond the named office is in the cached body.Source approve, re-approve, suspend, retire or past review date, and assistant publish or pause, bump source_rev for that tenant_id, delete keys with prefix campus:ref:public-meta:v{old-rev}:{tenant_id}:, and revalidate with If-None-Match on the next read.
Signed-in reference lists, source list summary, assistant list summary and office directory, GET signed-in listscampus:ref:tenant:{tenant_id}:{resource}:v{tenant_rev}:{lower-trimmed-q-or-empty}:{sort}60 seconds in a per-isolate in-memory Map only, never in the shared Workers cache, response Cache-Control private, no-store, no ETag, Vary: Cookie, Authorization. The in-memory entry is keyed by the tenant_id and resource above.Any write in that tenant_id, source or assistant create, edit, approve, retire, publish, pause, retention or access change, bumps tenant_rev for that tenant_id only, deletes in-memory keys with prefix campus:ref:tenant:{tenant_id}:, and the next read for that tenant misses. A write in one college never deletes or warms another college.
  • Never cache, in any cache above: GET /api/health, GET /api/ready and signed-in readiness, POST /api/public/request, workspace search, question and answer records, report detail, saved answers, handoff threads, or any response in session-only mode. Those routes keep no-store and are queried on every read.
  • Cache lookup order on a read-heavy GET route is cache first for the exact key, then one database query for that tenant_id and filters only, then cache.put for a GET 200 body in the same handler. Do not cache a 4xx, 5xx, redirect, or a body that fails validation, and do not put a secret, token, password, API key, connection string or full question text in a key, a cached body, a log or a header.
  • On a write, commit the write first, then bump the revision named for that resource and tenant_id, then delete matching cache keys, then return the normal success response. A failed write deletes nothing. A cached entry that is past its TTL is revalidated, and a revalidation that returns 304 keeps the cached body with a fresh TTL.
  • Payload check after shipping: the second identical GET for a public guide, terminology or public metadata route returns the cached body with no database query and the same ETag, a signed-in list read within 60 seconds for the same tenant_id returns the in-memory body with no database query, and a read for a different tenant_id never returns the first tenant body.

How the top rewrite changes the save path

Use this flow for Rank 1. The reader sees no change. The save stops parsing and scanning the whole library on every use.

```mermaid
flowchart TD
  A[Save clicked] --> B[Read Map index]
  B --> C{Duplicate in Map}
  C -->|Yes| D[Show already saved]
  C -->|No| E[Add item and update Map]
  E --> F[Write store once]
  F --> G[Keep Map for next save]
```
  1. Step 1: Build the Map once

    Parse the stored library once, then build the Map from the parsed items. Keep both in memory for the page session.

  2. Step 2: Look up, do not scan

    Check the Map for the duplicate key before any list scan. A miss adds one entry to the Map.

  3. Step 3: Write once and keep the Map

    Write the store once on a successful save, then keep the updated Map. Clear the Map only when the stored library is cleared or replaced.

Rules that apply to every rewrite above

Apply these rules in the same release as any rewrite. A rewrite that breaks one rule does not ship.

  • Never change the storage key or the record shape in a performance rewrite, so saved items from the current build open in the next build
  • Never add a server request, a new event, a new field, or a new permission in a client-side rewrite
  • Keep the 120 millisecond search pause, keep every typed word must match, and keep one empty state per list
  • Keep test and staff preview sessions out of counts, as Reports states, and keep unavailable checks separate from not answered
  • Keep the same college workspace, assistant, channel, and date range filters on screen and on the list behind the number
  • Keep focus visible, keep every control at least 44 pixels high, and test on a phone and on a desktop before you publish

Follow Search indexing for search scope and metadata, Search scope for shared query rules, Save answer with sources for the save bundle, and Weekly quality rate for the weekly table.

Work the slowest path first

Ship the Rank 1 Map index for the My Answers store, then the Rank 2 checkbox cache, then the Rank 3 token index, in that order.