feat(workerEntry): backfill regionId in segment from cf-region-code - #210
Merged
Conversation
Follow-up to #209. Without this, existing sites still need a manual edit to their worker-entry.ts's buildSegment to include `regionId: cf-region-code` — otherwise the cache key stays region-agnostic and a RJ-cached response gets served to SP visitors, defeating the matcher fix. createDecoWorkerEntry now wraps the consumer's buildSegment so that, if the returned segment doesn't include regionId, the framework backfills it from `cf-region-code` (header preferred, then `request.cf`). Sites that already set regionId explicitly (e.g. via VTEX regionalization) are unaffected — the wrapper is a no-op when seg.regionId is truthy. Sites without buildSegment also unaffected. Net effect: bumping @decocms/start to this release is enough to make the website/matchers/location.ts matcher work end-to-end. No worker-entry.ts edit required for existing storefronts. The bump does invalidate existing cache entries on first deploy (cache key now includes r=<region>), but that's a one-time miss spike, not a correctness issue.
There was a problem hiding this comment.
1 issue found across 1 file
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
…ader Addresses cubic P1 (confidence 6) on PR #210 — defense in depth against cache-key spoofing. CF strips inbound cf-* headers in standard deployments, so this is only exploitable behind a custom proxy, but reading request.cf first removes the concern entirely. Header stays as a fallback for environments without a populated cf object (tests, non-CF proxies).
|
🎉 This PR is included in version 6.4.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #209. Makes the location-matcher fix work for existing storefronts without any worker-entry edit — they only need to bump the package.
Today, after #209 merged, sites still hit a cache contamination bug:
/→ matcher resolves the RJ variant correctly → response cached with keydesktop/→ cache HIT on samedesktopkey → receives the RJ variantThe matcher is correct, but the cache key doesn't include region. To fix that today, every storefront has to edit
src/worker-entry.ts:buildSegment: (request) => { const vtx = extractVtexContext(request); + const cf = (request as unknown as { cf?: { regionCode?: string } }).cf; + const geoRegion = request.headers.get("cf-region-code") ?? cf?.regionCode ?? ""; return { device: ..., ... - regionId: (vtx as any).regionId ?? undefined, + regionId: (vtx as any).regionId ?? (geoRegion || undefined), }; },That's friction at every storefront. This PR moves the backfill into
createDecoWorkerEntryso the bump alone is enough.Change
createDecoWorkerEntrynow wraps the consumer'sbuildSegment. If the returned segment is missingregionId, the framework injects it fromcf-region-code(header first, thenrequest.cf).Compatibility
regionIdexplicitly (e.g. VTEX regionalization) → unchanged. Wrapper is a no-op whenseg.regionIdis truthy.buildSegmentat all → unchanged. Backfill only applies when the consumer opted in to segmentation.regionId: undefined→ now get it from CF. The cache key gains ar=<region>segment. One-time cache miss spike on first deploy after the bump, then stable.Why split from #209
#209 was the matcher logic + migration template default. This is a separate, narrowly-scoped runtime change to
workerEntry. Keeping it separate makes it easier to revert if any site sees an unexpected regression.Test plan
npm run typecheckRefs #209
🤖 Generated with Claude Code
Summary by cubic
Automatically backfills
segment.regionIdfrom Cloudflare geo insidecreateDecoWorkerEntry, preferringrequest.cf.regionCodeand falling back tocf-region-code. Cache keys become region-aware without editing storefrontworker-entry.ts, preventing cross-region cache contamination. Bumping@decocms/startis enough.buildSegment; ifregionIdis missing, sets it fromrequest.cf.regionCodefirst, thencf-region-codeheader.regionIdis already set or when nobuildSegmentis provided.Written for commit 044724c. Summary will update on new commits.