Skip to content

perf: edge-cache via caches.default in middleware - #14

Merged
neuromechanist merged 1 commit into
feature/issue-1-epic-nemar-redesignfrom
feature/issue-1-perf-cache-middleware
May 18, 2026
Merged

neuromechanist merged 1 commit into
feature/issue-1-epic-nemar-redesignfrom
feature/issue-1-perf-cache-middleware

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Adds src/middleware.ts that talks directly to the Cloudflare Workers Cache API (caches.default-equivalent via caches.open(\"nemar-edge-v1\")).

Why

Cloudflare's automatic CDN cache honors Cache-Control: s-maxage on production custom domains but skips caching on *.pages.dev preview URLs (and on some short-cached responses). This middleware closes that gap so:

  • Repeat visits to a deployed preview get instant TTFB (no Worker SSR runs).
  • Once the production domain points at the project, the explicit cache is redundant with the auto-CDN but still safe.

Pairs with the Cache-Control headers landed in 1879a96.

What it does

  • GET requests only.
  • Cache hit → return the stored response with x-nemar-cache: HIT.
  • Cache miss → run next(), store the response if Cache-Control is public + (max-age|s-maxage)=N, and tag the response with x-nemar-cache: MISS.
  • Uses runtime.ctx.waitUntil when available so the cache write doesn't block the response.
  • Skips 4xx/5xx and anything no-store / private.

Test plan

  • Build passes (verified locally).
  • First request to /dataset/<id> shows x-nemar-cache: MISS and normal TTFB.
  • Subsequent requests within s-maxage window show HIT and dramatically lower TTFB.
  • /discover?modality=eeg and /discover?modality=meg get separate cache entries (URL keying).
  • No regression on POST routes (none yet, but the middleware short-circuits them).

@neuromechanist
neuromechanist force-pushed the feature/issue-1-perf-cache-middleware branch from 87cf429 to 44283d6 Compare May 18, 2026 08:16
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 18, 2026 •

Copy link
Copy Markdown

@neuromechanist
neuromechanist merged commit 94a7394 into feature/issue-1-epic-nemar-redesign May 18, 2026
1 check passed
@neuromechanist
neuromechanist deleted the feature/issue-1-perf-cache-middleware branch May 18, 2026 08:19
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