/api/sessions ships 1790 KB uncompressed (453 KB gzipped); firstMessage is 82.5% of it but only ~50 chars are rendered
Problem
/api/sessions and /api/sessions/[id] return uncompressed JSON. Static chunks under _next/static are served with Content-Encoding: gzip, but route handlers returning NextResponse.json() are not covered by Next's compress option, so these two endpoints ship their full payload as plain text.
On a session store with 715 sessions, /api/sessions is 1790 KB per request. It gzips to 453 KB (25.4%) with no other change.
This matters more than it first looks because /api/sessions is fetched on several interaction paths, not once per page load. In app/page.tsx there are three independent fetch("/api/sessions") call sites (initial load / session switch / keyboard-shortcut new session), so switching sessions re-downloads the whole list.
For LAN or remote access the transfer cost dominates. Measured payload against effective bandwidth:
| bandwidth | before | after |
|---|---|---|
| 10 Mbps | ~1398 ms | ~354 ms |
| 30 Mbps | ~466 ms | ~118 ms |
| 100 Mbps | ~140 ms | ~35 ms |
Where the payload comes from
Field-level breakdown of the 1790 KB response:
firstMessage 1158 KB 82.5% (longest single value 11828 chars, median 463)
path 93 KB 6.7%
id 26 KB 1.9%
created 18 KB 1.3%
modified 18 KB 1.3%
...
firstMessage is 82.5% of the body, but the sidebar only renders a truncated title:
// components/Sidebar.tsx (SessionRow)
const derived = skillTitle(session.firstMessage) ?? session.firstMessage;
const label = session.name || derived.slice(0, 50) || session.id.slice(0, 12);
So ~50 characters per row are displayed while up to ~11.8 KB per row is transferred.
Suggested fix
Two independent changes, in order of value:
1. gzip the response (no behavior change). Compress when the client advertises Accept-Encoding: gzip and the payload is over a small threshold; fall back to plain JSON otherwise. This alone is the 1790 → 453 KB win.
2. Optionally truncate firstMessage server-side. On top of gzip, truncating to 512 chars saves a further 38% (453 → 273 KB). This is not free and I would not do it naively:
- 230 of 715 sessions (32%) have a skill-wrapped
firstMessage.skillTitle()matches with an anchored regex that requires the closing</skill>tag, so truncating those strings makes the regex fail and the sidebar falls back to rendering a raw<skill name="..." location="...prefix as the title. firstMessagealso participates in sidebar search filtering, so truncation silently narrows what users can find.
A safe version would skip skill-wrapped values and truncate only plain ones. Given gzip already removes 75%, this may not be worth the behavior change.
Verification
I patched the compiled route handlers in the installed package locally (@agegr/[email protected]) to confirm Next's response pipeline preserves Content-Encoding and does not double-compress. After a restart:
/api/sessions Accept-Encoding: gzip -> 453 KB, content-encoding: gzip
/api/sessions no Accept-Encoding -> 1790 KB, plain (correct fallback)
/api/sessions/[id] -> 76 KB -> 26 KB (34.4%)
404 error responses -> unchanged, plain
Decompressed output is byte-identical to the plain-text path, and session counts match (715) on both.
Not addressed by this issue
Cold-start latency is separate and unaffected: the first /api/sessions after a restart takes ~2.9 s (vs ~42 ms warm) because the scan opens 715 .jsonl files. The scan itself reads only bounded 4096-byte head chunks, so the cost is syscall count, not bytes — a persisted index would be the fix, but that is a different change.
Environment
@agegr/[email protected],[email protected], Node v25.9.0, Windows- 715 sessions in the session store
A corroborating datapoint from a larger store: 2,008 sessions.
These measurements come from the raw session files, not the /api/sessions HTTP response:
- First user messages total 2,068 KB of raw text.
- Compressing that concatenated text with gzip produces 560 KB, or about 27%.
- The median first message is 497 characters; p95 is 3,331; the maximum is 34,330.
- Truncating plain first messages to 512 characters would remove 1,352 KB, or 65% of the raw text.
- Only 29 of 2,008 first messages are skill-wrapped in this store—1.4%, compared with 32% in the issue’s sample.
That last difference suggests the skillTitle() constraint depends heavily on usage. A conservative implementation that preserves skill-wrapped values but truncates plain messages would still capture almost all of the saving in a store like mine.