Skip to content

feat(worker): regionalized cache segmentation and purge support - #15

Merged
vibe-dex merged 1 commit into
mainfrom
fix/deferred-wrapper-and-location-matcher
Mar 16, 2026
Merged

vibe-dex merged 1 commit into
mainfrom
fix/deferred-wrapper-and-location-matcher

Conversation

@vibe-dex

@vibe-dex vibe-dex commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add regionId to SegmentKey for per-region edge cache differentiation
  • Include regionId in hashSegment cache key computation
  • Refactor handlePurge to accept salesChannels and regionIds in request body, generating all segment permutations for thorough cache invalidation
  • Include __v (build hash) in purge cache keys for correctness
  • Update edge caching and route config skill docs

Context

Part of the VTEX regionalization support. Sites that use postal-code-based regionalization (e.g. Casa&Video) need the edge cache to be segmented per regionId, and the purge endpoint must be able to target those region-specific entries.

The regionId field is optional on SegmentKey — sites without regionalization simply omit it and get the same behavior as before.

Test plan

  • Verify tsc --noEmit passes (confirmed locally)
  • Deploy a site with buildSegment returning regionId and verify cache keys include r=<regionId>
  • Trigger /_cache/purge with regionIds in body and verify region-specific entries are purged

Made with Cursor


Summary by cubic

Add per-region edge cache keys and purge support to handle VTEX regionalization. Sites can now cache and invalidate content by regionId without impacting non-regionalized setups.

  • New Features

    • Added regionId to SegmentKey and included it in hashSegment to create region-scoped cache entries.
    • Purge handler now accepts salesChannels and regionIds, generates all segment permutations, and includes __v (build hash) in purge keys.
    • Added X-Cache and X-Cache-Reason headers; private, none, and cart profiles always bypass cache.
    • Updated docs for edge caching, route config, deploy-time cache versioning (BUILD_HASH), and route cache defaults.
  • Migration

    • If using VTEX regionalization, return regionId from buildSegment; omit it otherwise.
    • For /_cache/purge, pass regionIds (and salesChannels if needed) to invalidate region-specific entries.
    • Set BUILD_HASH in deploys so versioned cache keys are generated.

Written for commit 109d067. Summary will update on new commits.

- Add regionId to SegmentKey for per-region cache differentiation
- Include regionId in hashSegment cache key computation
- Refactor handlePurge to accept salesChannels and regionIds in
  request body, generating all segment permutations for thorough
  cache invalidation
- Include __v (build hash) in purge cache keys for correctness
- Update edge caching and route config skill docs

Made-with: Cursor
@vibe-dex
vibe-dex merged commit 6cb4fae into main Mar 16, 2026
1 check passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.24.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@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.

4 issues found across 3 files

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=".cursor/skills/deco-cms-route-config/SKILL.md">

<violation number="1" location=".cursor/skills/deco-cms-route-config/SKILL.md:45">
P2: Re-add `NotFoundPage` to the example import; the updated snippet still references it and will not compile as written.</violation>
</file>

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

<violation number="1" location="src/sdk/workerEntry.ts:468">
P1: `buildPurgeSegments` misses `device + regionId` cache keys when `salesChannel` is absent, so region-only segmented entries are not purged.</violation>
</file>

<file name=".cursor/skills/deco-edge-caching/SKILL.md">

<violation number="1" location=".cursor/skills/deco-edge-caching/SKILL.md:191">
P2: This deploy example does not actually bind `BUILD_HASH` into the Worker runtime, so `env.BUILD_HASH` will be empty and cache versioning will silently do nothing.</violation>

<violation number="2" location=".cursor/skills/deco-edge-caching/SKILL.md:292">
P2: `unknown` cache status is not equivalent to a HIT. The Worker also serves MISS/BYPASS responses, so this guidance can cause teams to overestimate cache hit rate.</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
Comment on lines +468 to +479
const channels = body.salesChannels ?? ["1"];
const regions: Array<string | undefined> = [undefined, ...(body.regionIds ?? [])];

const segments: SegmentKey[] = [];
for (const device of devices) {
for (const salesChannel of channels) {
for (const regionId of regions) {
segments.push({ device, salesChannel, regionId });
}
}
segments.push({ device });
}

@cubic-dev-ai cubic-dev-ai Bot Mar 16, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1: buildPurgeSegments misses device + regionId cache keys when salesChannel is absent, so region-only segmented entries are not purged.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/sdk/workerEntry.ts, line 468:

<comment>`buildPurgeSegments` misses `device + regionId` cache keys when `salesChannel` is absent, so region-only segmented entries are not purged.</comment>

<file context>
@@ -442,6 +454,32 @@ export function createDecoWorkerEntry(
+
+  function buildPurgeSegments(body: PurgeRequestBody): SegmentKey[] {
+    const devices: Array<"mobile" | "desktop"> = ["mobile", "desktop"];
+    const channels = body.salesChannels ?? ["1"];
+    const regions: Array<string | undefined> = [undefined, ...(body.regionIds ?? [])];
+
</file context>
Suggested change
const channels = body.salesChannels ?? ["1"];
const regions: Array<string | undefined> = [undefined, ...(body.regionIds ?? [])];
const segments: SegmentKey[] = [];
for (const device of devices) {
for (const salesChannel of channels) {
for (const regionId of regions) {
segments.push({ device, salesChannel, regionId });
}
}
segments.push({ device });
}
const channels: Array<string | undefined> = [undefined, ...(body.salesChannels ?? ["1"])];
const regions: Array<string | undefined> = [undefined, ...(body.regionIds ?? [])];
const segments: SegmentKey[] = [];
for (const device of devices) {
segments.push({ device });
for (const salesChannel of channels) {
for (const regionId of regions) {
if (salesChannel === undefined && regionId === undefined) continue;
segments.push({ device, salesChannel, regionId });
}
}
}
Fix with Cubic

import { createFileRoute, notFound } from "@tanstack/react-router";
import { cmsRouteConfig, NotFoundPage } from "@decocms/start/routes";
import { createFileRoute } from "@tanstack/react-router";
import { cmsRouteConfig, loadDeferredSection } from "@decocms/start/routes";

@cubic-dev-ai cubic-dev-ai Bot Mar 16, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Re-add NotFoundPage to the example import; the updated snippet still references it and will not compile as written.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At .cursor/skills/deco-cms-route-config/SKILL.md, line 45:

<comment>Re-add `NotFoundPage` to the example import; the updated snippet still references it and will not compile as written.</comment>

<file context>
@@ -41,41 +41,72 @@ The catch-all route handles all CMS-managed pages (PDP, PLP, institutional pages
-import { createFileRoute, notFound } from "@tanstack/react-router";
-import { cmsRouteConfig, NotFoundPage } from "@decocms/start/routes";
+import { createFileRoute } from "@tanstack/react-router";
+import { cmsRouteConfig, loadDeferredSection } from "@decocms/start/routes";
 import { DecoPageRenderer } from "@decocms/start/hooks";
+import type { ResolvedSection, DeferredSection } from "@decocms/start/cms";
</file context>
Suggested change
import { cmsRouteConfig, loadDeferredSection } from "@decocms/start/routes";
import { cmsRouteConfig, loadDeferredSection, NotFoundPage } from "@decocms/start/routes";
Fix with Cubic

When migrating to TanStack Workers, compare cache metrics:
- Production (Deno/Fresh on Kubernetes) typically shows high HIT rates because traffic volume keeps caches warm
- Staging Workers may show lower HIT rates due to lower traffic, plus "unknown" status from internal Cache API
- The "unknown" requests on Workers are functionally equivalent to HITs — they're served from the Worker's Cache API without hitting the origin server function

@cubic-dev-ai cubic-dev-ai Bot Mar 16, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: unknown cache status is not equivalent to a HIT. The Worker also serves MISS/BYPASS responses, so this guidance can cause teams to overestimate cache hit rate.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At .cursor/skills/deco-edge-caching/SKILL.md, line 292:

<comment>`unknown` cache status is not equivalent to a HIT. The Worker also serves MISS/BYPASS responses, so this guidance can cause teams to overestimate cache hit rate.</comment>

<file context>
@@ -181,13 +181,125 @@ const cachedPLP = createCachedLoader("vtex/plp", vtexPLP, {
+When migrating to TanStack Workers, compare cache metrics:
+- Production (Deno/Fresh on Kubernetes) typically shows high HIT rates because traffic volume keeps caches warm
+- Staging Workers may show lower HIT rates due to lower traffic, plus "unknown" status from internal Cache API
+- The "unknown" requests on Workers are functionally equivalent to HITs — they're served from the Worker's Cache API without hitting the origin server function
+
 ## Key Constraints
</file context>
Fix with Cubic

Comment on lines +191 to +193
run: npx wrangler deploy
env:
BUILD_HASH: ${{ github.sha }}

@cubic-dev-ai cubic-dev-ai Bot Mar 16, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: This deploy example does not actually bind BUILD_HASH into the Worker runtime, so env.BUILD_HASH will be empty and cache versioning will silently do nothing.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At .cursor/skills/deco-edge-caching/SKILL.md, line 191:

<comment>This deploy example does not actually bind `BUILD_HASH` into the Worker runtime, so `env.BUILD_HASH` will be empty and cache versioning will silently do nothing.</comment>

<file context>
@@ -181,13 +181,125 @@ const cachedPLP = createCachedLoader("vtex/plp", vtexPLP, {
+```yaml
+# .github/workflows/deploy.yml
+- name: Deploy to Cloudflare Workers
+  run: npx wrangler deploy
+  env:
+    BUILD_HASH: ${{ github.sha }}
</file context>
Suggested change
run: npx wrangler deploy
env:
BUILD_HASH: ${{ github.sha }}
run: npx wrangler deploy --var BUILD_HASH:${GITHUB_SHA}
Fix with Cubic

guitavano added a commit that referenced this pull request Nov 17, 2026
…anStack Start, esbuild) (#832)

Closes the open Dependabot alerts in one PR, with the lockfile. The
Dependabot PRs (#811, #828, #747) only touch `package.json`, so they all
fail `bun install --frozen-lockfile` in CI.

| Package | From | To | Alerts |
|---|---|---|---|
| `react-server-dom-webpack` (tanstack-rsc-smoke) | 19.2.7 | 19.2.9 |
#19, critical: RSC RCE, GHSA-xqc2-xf4c-x3h3 |
| `react` / `react-dom` (all pinned workspaces) | 19.2.7 | 19.2.9 | peer
floor of the above |
| `next` (nextjs, compat-v7, nextjs-binding-smoke) | 16.3.8 | 16.4.6 |
#10–12, #14, #15, #17, #18, #20–22 |
| `@tanstack/react-start` (tanstack, compat-v7, smokes) | 1.168.58 |
1.168.61 | #2–4, #7, #9 |
| `@tanstack/react-router` | 1.170.39 | 1.170.42 | the version
react-start 1.168.61 pins, so it stays a single copy |
| `esbuild` (blocks, tanstack, compat-v7) | ^0.27.3 | ^0.28.2 | #5, #6,
#8 (low) |

`bun.lock` was regenerated with bun 1.3.5 (the `packageManager`). React
dedupes to a single 19.2.9 at the root, with no nested copies. Vite
still has its own transitive `[email protected]` (its own range); that is
outside our manifests.

Supersedes #811, #828 and #747.

### Verified locally (bun 1.3.5, node 24)
- `bun install --frozen-lockfile` ✅
- `bun run typecheck` ✅ · `bun run lint` ✅
- `bun x vitest run`: 3388 passed, 0 failed ✅ (includes
`tests/release.test.ts`)
- `examples/tanstack-worker-smoke`: build + smoke ✅
- `examples/nextjs-binding-smoke`: build + smoke ✅
- `examples/tanstack-rsc-smoke`: build + typecheck ✅

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Bumps several dependencies to clear open Dependabot security alerts,
including a critical RCE in `react-server-dom-webpack`
(GHSA-xqc2-xf4c-x3h3). The lockfile was regenerated so `bun install
--frozen-lockfile` passes — React dedupes to a single 19.2.9 copy.

**Dependencies**
- `react`, `react-dom`, and `react-server-dom-webpack` 19.2.7 → 19.2.9
- `next` 16.3.8 → 16.4.6
- `@tanstack/react-start` 1.168.58 → 1.168.61, with
`@tanstack/react-router` 1.170.42
- `esbuild` ^0.27.3 → ^0.28.2; Vite's own transitive `[email protected]` is
untouched
- Typecheck, lint, the full test suite (3388 passed), and the Next.js
and TanStack smoke builds all pass

<sup>Written for commit 123acc8.
Summary will update on new commits.</sup>

<a href="https://cubic.dev/pr/decocms/blocks/pull/832?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<a
href="https://www.cubic.dev/action/auto-fix/pr/decocms/blocks/832?returnTo=https%3A%2F%2Fgithub.com%2Fdecocms%2Fblocks%2Fpull%2F832&source=description"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img
alt="Turn on auto-fix"
src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>

<!-- End of auto-generated description by cubic. -->

Co-authored-by: Claude Opus 5.5 <[email protected]>
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