Skip to content

feat: edge-cache POST _serverFn responses - #70

Merged
JonasJesus42 merged 6 commits into
mainfrom
feat/serverfn-edge-cache
Apr 7, 2026
Merged

JonasJesus42 merged 6 commits into
mainfrom
feat/serverfn-edge-cache

Conversation

@JonasJesus42

@JonasJesus42 JonasJesus42 commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #69 (merged before these commits landed).

  • Set cache headers on non-cacheable responses (was empty after fix: allow caching for _serverFn requests #69)
  • Edge-cache POST _serverFn responses using SHA-256 body hash as cache key
  • Full SWR + SIE support, Set-Cookie responses never cached

Flow

  1. Read POST body, SHA-256 hash
  2. Cache key = URL + body hash + device + build version (synthetic GET)
  3. HIT (<120s) serve from edge, STALE-HIT (120s-1020s) serve stale + background refresh, MISS fetch origin + store
  4. Set-Cookie = private, no-cache (protects cart/auth)

Test plan

  • _serverFn shelf: X-Cache: MISS first, X-Cache: HIT second
  • Shelf response ~1s to <10ms on hit
  • Cart/checkout with Set-Cookie not cached
  • X-Cache-Profile: listing on responses

Summary by cubic

Edge-cache POST _serverFn/_server responses using a body-hash key, and switch deferred section loads to GET to leverage standard HTTP caching. Also apply profile cache headers on non-cacheable responses and strip CDN directives when storing refreshed entries.

  • New Features

    • Edge-cache POST responses using a SHA-256 body-hash as a synthetic GET key; uses the "listing" profile with SWR + SIE and never caches Set-Cookie.
    • loadDeferredSection now uses GET so responses cache at the edge and in browsers; payload is serialized into the URL (~3–4KB, within Cloudflare Workers’ limit).
    • Adds X-Cache and X-Cache-Profile; shelf hits drop from ~1s to <10ms.
  • Bug Fixes

    • Apply profile cache headers on non-cacheable paths/methods; include segment + CF geo in the POST key, bypass logged-in users, and hoist buildSegment to avoid duplicate calls.
    • Delete CDN-Cache-Control before caching during SWR background refresh to avoid storing origin CDN directives.

Written for commit 1eae7e4. Summary will update on new commits.

JonasJesus42 and others added 2 commits April 7, 2026 20:19
Non-cacheable responses (POST _serverFn, bypass paths) were leaving
Cache-Control empty after removing the blanket no-cache. Now applies
the profile cache headers so responses are explicit about cacheability.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Server function POST requests (/_serverFn/, /_server/) carry public CMS
section data (shelves, deferred sections) that benefits from edge caching.
Since POST responses can't use standard HTTP caching, we hash the request
body with SHA-256 and use it as a synthetic GET cache key in the
Cloudflare Cache API.

Includes full SWR + SIE support (stale-while-revalidate with background
refresh, stale-if-error fallback) matching the existing GET cache flow.
Responses with Set-Cookie are never cached to prevent session leaking.

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

@cubic-dev-ai cubic-dev-ai 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.

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/sdk/workerEntry.ts">

<violation number="1" location="src/sdk/workerEntry.ts:915">
P2: Missing `toStore.headers.delete("CDN-Cache-Control")` in the SWR background revalidation store path. Both the MISS path (below) and the existing `storeInCache` helper delete this header before putting into the Cache API. Without it, a `CDN-Cache-Control` header from the origin response leaks into the cached entry during background refresh.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src/sdk/workerEntry.ts Resolved
JonasJesus42 and others added 2 commits April 7, 2026 20:24
Without segment info in the cache key, users in different regions or
sales channels would share cached responses — leaking regional prices.

Now mirrors the GET cache key logic: includes buildSegment (device,
salesChannel, regionId, flags) and CF geo data. Logged-in users
bypass the cache entirely to prevent personalized content leaking.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Matches the MISS path and existing storeInCache helper which both
delete this header before cache.put(). Without it, a CDN-Cache-Control
header from the origin response leaks into the cached entry.

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

@cubic-dev-ai cubic-dev-ai 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.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/sdk/workerEntry.ts">

<violation number="1" location="src/sdk/workerEntry.ts:862">
P2: Redundant `buildSegment(request)` call — this user-provided callback is invoked twice for every non-logged-in POST request. Hoist the segment result above the body-read so both the logged-in check and cache-key builder share it.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src/sdk/workerEntry.ts Resolved
JonasJesus42 and others added 2 commits April 7, 2026 20:30
buildSegment(request) was called twice per non-logged-in POST request.
Hoist result into sfnSegment, reuse for both logged-in check and cache key.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Deferred section data (shelves, product lists) is public and read-only.
Using GET allows standard HTTP caching at edge and browser level.

Typical payload is ~3KB, well within Cloudflare Workers 16KB URL limit.
TanStack Start serializes the input into the URL automatically via TSS.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@JonasJesus42
JonasJesus42 merged commit fb16a5b into main Apr 7, 2026
1 check passed
@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant