Skip to content

feat(api): negotiate compression, add ETag/304 and stale-while-revalidate purges / API 协商压缩,支持 ETag/304 与 stale-while-revalidate 失效 - #1546

Merged
adibarra merged 6 commits into
masterfrom
feat/api-response-caching
Oct 29, 2026
Merged

adibarra merged 6 commits into
masterfrom
feat/api-response-caching

Conversation

@adibarra

@adibarra adibarra commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 2 of the stack split from #1530. Review and merge in order. CI only runs on PRs that target master, so each layer was verified locally; the combined change passed full CI on #1530.

Stack (merge in order):

  1. fix(next): stop the image optimizer hanging after a cancelled request / 修复请求取消后图片优化器卡住的问题 #1545 Next image optimizer patch
  2. feat(api): negotiate compression, add ETag/304 and stale-while-revalidate purges / API 协商压缩,支持 ETag/304 与 stale-while-revalidate 失效 #1546 API response caching ← this PR
  3. fix(dashboard): load benchmarks in parallel with availability / dashboard 的 benchmarks 与 availability 并行加载 #1547 Dashboard data waterfall
  4. fix(seo): social images, canonicals, titles, intros, structured data and sitemap / SEO:社交图片、canonical、标题、简介、结构化数据与 sitemap #1548 SEO metadata
  5. feat(seo): render compare, run, rankings, overview and blog index with ISR / compare、run、rankings、overview 与博客索引改用 ISR #1549 ISR for programmatic pages
  6. fix(perf): cut client bundle, image and prefetch weight / 减少客户端代码、图片与预取体积 #1550 Client bundle, image and prefetch weight
  7. fix(ux): remove layout shifts and explain blocked legend toggles / 消除布局偏移并说明被禁用的图例切换 #1551 Layout shift and UX
  8. fix(analytics): correct pageviews, proxy PostHog and stop sending search text / 统计:修正 pageview、代理 PostHog 并不再发送搜索文本 #1552 Analytics pageviews and privacy
  9. feat(analytics): cover untracked interactions, add event context and standardize keys / 统计:覆盖未追踪的交互,补充事件上下文并统一键名 #1555 Analytics coverage

Dashboard API responses were gzip-only with max-age=0 and no ETag, so browsers downloaded multi-MB payloads on every visit. Every ingest also hard-expired all caches, which made the next visitor wait for a cold rebuild.

  • Negotiation and revalidation. cachedJson(request, …) picks br, gzip or identity from Accept-Encoding. It always sends Vary: Accept-Encoding, sets a strong ETag, and answers a matching If-None-Match with 304. Browsers reuse responses for 60 s. CDN s-maxage is unchanged.
  • Separate cache tags. Server data (db), ISR page data (db-page) and CDN responses (db-cdn) are tagged separately. An ingest expires route data immediately and marks page data and CDN responses stale, so visitors get the previous response once while fresh data loads.
  • Hard purge. purgeAll, purgeCollectiveX and purgeOperatorX accept immediate. /api/v1/invalidate?immediate=true and invalidate-cache.ts --immediate expose it, and apply-run-overrides/cache-refresh use it for corrections. CollectiveX deletions purge immediately.
  • Warmup. The script reads response bodies and warms the main dashboard endpoints after ingest and deploy.
  • Docs and API reference. API docs (EN/ZH), route-catalog digests and cache docs are updated.

Risks

  • Fixture builds (E2E_FIXTURES=1) send no-store so Cypress intercepts see every request. E2E therefore doesn't exercise browser reuse; unit tests cover it.
  • The page/route split reads an internal Next path (work-async-storage.external).
  • On a Vercel preview of the combined change, API responses came back brotli with ETag and Vary, and 304s were served from a CDN HIT. Still unverified: whether a gzip-only client can get a cached br variant, and whether Pragma: no-cache forces revalidation.
Validation details

Targeted Cypress on a fixtures build (Chrome): historical-trends 18/18, model-route-slugs 10/10, inference-chart 39/39, evaluation-chart 13/13. packages/db vitest passes. Lint, format, typecheck and the full unit suite pass on this layer.

AI model disclosure
  • Model/version: anthropic/claude-opus-5-5 (runtime-exposed identifier for the coordinating agent).
  • Role: audited the site, planned and integrated the change set, split it into this stack, and wrote commit and PR text.
  • Delegated agents (scout, worker and critic subagents) implemented slices, built the stack and ran verification under the same harness. Their exact model identifier was not exposed to the coordinating agent, so it could not be verified.
PR checklist
  • I have completed the AI model disclosure and kept it current
  • Added, changed, or removed filters, views, or pages include the matching API and inferencex-skills changes (no filters or views change)
  • New or changed indexable pages ship their /zh sibling
  • Changed Chinese copy follows the PR review checklist in docs/chinese-copy.md (Chinese maintainer review pending)
中文

本 PR 为从 #1530 拆分的 stack 中的第 2 部分,请按顺序审核与合并。CI 只在以 master 为目标的 PR 上运行,因此每一层都在本地验证;整体改动已在 #1530 上通过完整 CI。

Dashboard API 响应此前仅支持 gzip、max-age=0 且没有 ETag,浏览器每次访问都会重新下载数 MB 数据;每次数据导入还会强制清空所有缓存,下一位访问者需要等待冷启动重建。

  • 协商与重新验证。 cachedJson(request, …) 按 Accept-Encoding 选择 br、gzip 或 identity,始终返回 Vary: Accept-Encoding,设置强 ETag,并对匹配的 If-None-Match 返回 304。浏览器复用 60 秒,CDN s-maxage 不变。
  • 分离缓存标签。 服务端数据(db)、ISR 页面数据(db-page)与 CDN 响应(db-cdn)分开打标签。数据导入时立即使路由数据过期,页面数据与 CDN 响应仅标记为过时,访问者在新数据加载期间先拿到旧响应。
  • 立即清除。 purgeAll、purgeCollectiveX、purgeOperatorX 支持 immediate,可通过 /api/v1/invalidate?immediate=true 与 invalidate-cache.ts --immediate 调用;apply-run-overrides 与 cache-refresh 在数据更正时使用;CollectiveX 删除操作立即清除。
  • 预热。 预热脚本会读取响应体,并在导入与部署后预热主要 dashboard 接口。
  • 文档。 已同步更新中英文 API 文档、路由目录摘要与缓存文档。

风险

  • fixture 构建(E2E_FIXTURES=1)返回 no-store,以便 Cypress 拦截每个请求,因此 E2E 不覆盖浏览器缓存复用,由单元测试覆盖。
  • 页面/路由的区分读取了 Next 内部路径(work-async-storage.external)。
  • 在整体改动的 Vercel preview 上已确认 brotli、ETag、Vary,以及 CDN 命中时返回 304;仍未确认仅支持 gzip 的客户端是否可能拿到缓存的 br 版本,以及 Pragma: no-cache 是否会强制重新验证。

验证:fixture 构建上的定向 Cypress(Chrome):historical-trends 18/18、model-route-slugs 10/10、inference-chart 39/39、evaluation-chart 13/13;packages/db 单元测试通过。本层的 lint、格式、类型检查与完整单元测试均通过。

AI 模型使用说明:协调 agent 使用 anthropic/claude-opus-5-5(运行环境提供的标识),负责审计、规划与整合改动、拆分 stack 以及撰写提交与 PR 文本;委派的子 agent 在同一环境中实现各部分、构建 stack 并运行验证,其确切模型标识无法获取,因此无法确认。


Note

Medium Risk
Changes core CDN/server invalidation and response shaping for most public APIs; incorrect purge or encoding behavior could briefly serve stale benchmark data or break clients, though behavior is heavily unit-tested.

Overview
Dashboard API responses now go through cachedJson(request, …), which negotiates brotli/gzip/identity from Accept-Encoding, adds Vary: Accept-Encoding, a strong ETag, and 304 on matching If-None-Match, and uses max-age=60 for browsers while keeping the one-day CDN TTL. CDN entries are tagged db-cdn separately from server db and ISR db-page caches; normal ingests expire route data immediately but only mark page/CDN tags stale so one more stale response can be served while refresh runs. ?immediate=true (CLI --immediate, corrections, CollectiveX deletions) hard-expires every layer so retracted data is never served again.

CI and ops wire this up: ingest runs admin:cache:warmup --dashboard, deploy warmup checks out the repo and warms dashboard APIs plus more top pages with browser Accept-Encoding, and the warmup script reads full bodies and can target dashboard-only paths. Internal view routes use sourceRequest() (no Accept-Encoding) so in-process handler calls always get plain JSON; /api/v1/datasets/[slug] shares a per-slug cached reader with AgentX pages.

Fixture mode sends Cache-Control: no-store. Docs, API catalog digests, and targeted Cypress waits are updated for the new caching behavior.

Reviewed by Cursor Bugbot for commit c139ce0. Bugbot is set up for automated code reviews on this repo. Configure here.

@vercel

vercel Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
inferencemax-app Ready Ready Preview Oct 29, 2026 3:22am UTC

Request Review

Patch [email protected] through root patchedDependencies. Unpatched, the
self-hosted optimizer reads a local source image through a mocked request
that reuses the browser socket. When the browser cancels the image, the
read is dropped and every later request for that variant hangs until the
server restarts. A unit test fails if a Next upgrade loses the patch, and
the unit and E2E workflows now run when patches/ changes.

中文:通过根目录 patchedDependencies 为 [email protected] 打补丁。未打补丁时,
自托管图片优化器复用浏览器 socket 读取本地源图,浏览器取消请求后读取被丢弃,
同一尺寸的后续请求会一直挂起,直到服务重启。新增单元测试在升级 Next 丢失补丁
时失败,patches/ 变更也会触发单元测试与 E2E 工作流。
…mediately

cachedJson now takes the request. It answers with brotli, gzip or plain
JSON per Accept-Encoding, always sends Vary and a strong ETag, and returns
304 for a matching If-None-Match. Browsers reuse a response for 60 s.
Route data, ISR page data (db-page) and CDN responses (db-cdn) carry
separate tags, so an ingest expires route data at once and lets pages and
the CDN refresh in the background. purgeAll, purgeCollectiveX and
purgeOperatorX accept immediate, exposed through /api/v1/invalidate and the
admin scripts. Fixture builds send no-store. The ingest and deploy
workflows warm the dashboard APIs with a browser Accept-Encoding. The
datasets route shares a per-slug cache helper. Docs, API reference text
and catalog digests follow.

中文:cachedJson 改为接收请求对象,按 Accept-Encoding 返回 brotli、gzip 或
未压缩 JSON,并始终携带 Vary 与强 ETag,命中 If-None-Match 时返回 304;浏览器
复用响应 60 秒。路由数据、ISR 页面数据(db-page)与 CDN 响应(db-cdn)使用
独立标签,ingest 后路由数据立即失效,页面与 CDN 在后台刷新。purgeAll、
purgeCollectiveX、purgeOperatorX 支持 immediate,并通过 /api/v1/invalidate
与管理脚本提供。fixture 构建返回 no-store。ingest 与部署工作流以浏览器的
Accept-Encoding 预热仪表板 API。datasets 路由改用按 slug 共享的缓存。同步更新
文档、API 参考文案与路由目录摘要。
@adibarra
adibarra force-pushed the fix/next-image-optimizer-hang branch from 153ffcf to d78bbd5 Compare October 29, 2026 02:06
@adibarra
adibarra force-pushed the feat/api-response-caching branch from e645ff3 to 3a1c60d Compare October 29, 2026 02:06
Base automatically changed from fix/next-image-optimizer-hang to master October 29, 2026 02:11
@adibarra
adibarra deleted the branch fix/next-image-optimizer-hang October 29, 2026 02:11
@adibarra adibarra closed this Oct 29, 2026
中文:将 master 合并到 feat/api-response-caching
@adibarra adibarra reopened this Oct 29, 2026
中文:将 master 合并到 feat/api-response-caching

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b3fe7a0. Configure here.

Comment thread packages/app/scripts/warmup-cache.ts Resolved
After an ingest the CDN still serves the previous availability payload once, so the warmup missed the newest run date. It now sends Pragma/Cache-Control no-cache for that lookup.

中文:预热 dashboard 日期前重新验证 availability。数据导入后 CDN 仍会返回一次旧的 availability,导致预热遗漏最新日期;现在该请求会带上 Pragma/Cache-Control: no-cache。
中文:针对 master 重新运行 CI
@adibarra
adibarra merged commit 6048eba into master Oct 29, 2026
2 checks passed
@adibarra
adibarra deleted the feat/api-response-caching branch October 29, 2026 04:11
hbarclay added a commit that referenced this pull request Oct 29, 2026
Keeps the intent of master's OperatorX changes in this PR's additions:
- #1561 (one GPU control): no GPU multi-select; the controls row is
  Workload, Devices, Metric, Baseline, and the chart legends offer the GPUs
  with results at the selected split
- #1562 (Roofline first): Model View's op dialog also shows Roofline before
  Metric vs size; the OperatorX e2e spec now expects Roofline to mount
  first and scrolls to Metric vs size, which mounts on approach
- #1555 (analytics): the Devices selector and Model View's model, metric,
  precision, GPU, op and fold controls are tracked with canonical keys
- #1546 (compression, ETag): the public data API passes the request to
  cachedJson
- route catalog digests, views registry and skills reference merged

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — 9cecb512 Deployed Oct 29, 2026 by vercel[bot]
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.

1 participant