-
Notifications
You must be signed in to change notification settings - Fork 21
Chore/vtex cdn cache optimization #1537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
830e4b9
3f3d90b
09b7212
041837f
fd07e26
be29dbc
bd4dc02
344a85d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,26 @@ export const isAnonymous = ( | |
| !regionId; | ||
| }; | ||
|
|
||
| /** | ||
| * Checks if the segment is cacheable for CDN purposes. | ||
| * By default, uses isAnonymous (UTMs affect cacheability because prices | ||
| * can vary by utm_source). With removeUTMFromCacheKey, UTMs are ignored | ||
| * (opt-in for stores that don't vary prices by UTM). | ||
| */ | ||
| export const isCacheableSegment = (ctx: AppContext) => { | ||
| const payload = getSegmentFromBag(ctx)?.payload; | ||
| if (payload?.channelPrivacy === "private") return false; | ||
|
|
||
| if (ctx.advancedConfigs?.removeUTMFromCacheKey) { | ||
| if (!payload) return true; | ||
| const { campaigns, channel, priceTables, regionId } = payload; | ||
| return !campaigns && | ||
| (!channel || isDefautSalesChannel(ctx, channel)) && | ||
| !priceTables && !regionId; | ||
| } | ||
| return isAnonymous(ctx); | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| const setSegmentInBag = (ctx: AppContext, data: WrappedSegment) => | ||
| ctx?.bag?.set(SEGMENT, data); | ||
|
|
||
|
|
@@ -223,24 +243,28 @@ export const setSegmentBag = ( | |
| const token = serialize(segment); | ||
| setSegmentInBag(ctx, { payload: segment, token }); | ||
|
|
||
| // If the user came from a sales channel in the URL, we set the cookie | ||
| if (segmentFromRequest.channel) { | ||
| setCookie(ctx.response.headers, { | ||
| value: `sc=${segmentFromRequest.channel}`, | ||
| name: SALES_CHANNEL_COOKIE, | ||
| path: "/", | ||
| secure: true, | ||
| }); | ||
| } | ||
| // Skip Set-Cookie when the segment only differs by UTMs. | ||
| // UTMs don't affect page content, so the response can still be cached. | ||
| // Only set cookies when content-affecting fields differ (campaigns, | ||
| // non-default sales channel, price tables, region). | ||
| if (!isCacheableSegment(ctx)) { | ||
| if (segmentFromRequest.channel) { | ||
| setCookie(ctx.response.headers, { | ||
| value: `sc=${segmentFromRequest.channel}`, | ||
| name: SALES_CHANNEL_COOKIE, | ||
| path: "/", | ||
| secure: true, | ||
| }); | ||
| } | ||
|
|
||
| // Avoid setting cookie when segment from request matches the one generated | ||
| if (vtex_segment !== token) { | ||
| setCookie(ctx.response.headers, { | ||
| value: token, | ||
| name: SEGMENT_COOKIE_NAME, | ||
| path: "/", | ||
| secure: true, | ||
| httpOnly: true, | ||
| }); | ||
| if (vtex_segment !== token) { | ||
| setCookie(ctx.response.headers, { | ||
| value: token, | ||
| name: SEGMENT_COOKIE_NAME, | ||
| path: "/", | ||
| secure: true, | ||
| httpOnly: true, | ||
| }); | ||
| } | ||
| } | ||
|
Comment on lines
+246
to
269
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Find all TypeScript/JavaScript files that might contain Analytics snippet or client-side cookie handling
fd -t f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) | head -20Repository: deco-cx/apps Length of output: 286 🏁 Script executed: #!/bin/bash
# Search for Analytics-related files
find . -type f -name "*analytics*" -o -name "*Analytics*" | grep -v node_modules | head -20Repository: deco-cx/apps Length of output: 687 🏁 Script executed: #!/bin/bash
# Search for document.cookie usage across the codebase
rg "document\.cookie" -t ts -t tsx -t js -A 2 -B 2 | head -100Repository: deco-cx/apps Length of output: 83 🏁 Script executed: #!/bin/bash
# Search for segment cookie handling (vtex_segment, SEGMENT_COOKIE_NAME, etc.)
rg "vtex_segment|SEGMENT_COOKIE_NAME|SALES_CHANNEL_COOKIE" -t ts -t tsx -t js -A 2 -B 2 | head -100Repository: deco-cx/apps Length of output: 83 🏁 Script executed: #!/bin/bash
# Check the current file to understand the cookie constants being used
cat -n vtex/utils/segment.ts | sed -n '1,50p'Repository: deco-cx/apps Length of output: 1586 🏁 Script executed: #!/bin/bash
# View lines 244-280 of segment.ts to see full context around the reviewed code
cat -n vtex/utils/segment.ts | sed -n '244,280p'Repository: deco-cx/apps Length of output: 966 🏁 Script executed: #!/bin/bash
# Find the isCacheableSegment function definition
rg "isCacheableSegment" vtex/utils/segment.ts -B 5 -A 10Repository: deco-cx/apps Length of output: 1280 🏁 Script executed: #!/bin/bash
# Check the VTEX Analytics section implementation
find ./vtex/sections/Analytics -type f \( -name "*.ts" -o -name "*.js" \) | xargs catRepository: deco-cx/apps Length of output: 38 🏁 Script executed: #!/bin/bash
# Search for client-side cookie persistence in VTEX-related analytics
rg "document\.cookie" . -g "vtex*" -B 2 -A 2 2>/dev/null | head -80Repository: deco-cx/apps Length of output: 38 🏁 Script executed: #!/bin/bash
# Check the main DecoAnalytics component for client-side cookie handling
cat analytics/components/DecoAnalytics.tsx 2>/dev/null | head -150Repository: deco-cx/apps Length of output: 3138 🏁 Script executed: #!/bin/bash
# Check the analytics loader script
cat analytics/loaders/DecoAnalyticsScript.ts 2>/dev/nullRepository: deco-cx/apps Length of output: 2977 🏁 Script executed: #!/bin/bash
# Search for any client-side cookie logic across analytics files
rg "document\.cookie|localStorage|sessionStorage" . -g "analytics*" -g "*Analytics*" -B 2 -A 2 2>/dev/null | head -80Repository: deco-cx/apps Length of output: 475 🏁 Script executed: #!/bin/bash
# Check if there are any TypeScript/JavaScript files in the VTEX Analytics section
find ./vtex/sections/Analytics -type f 2>/dev/null | xargs ls -laRepository: deco-cx/apps Length of output: 139 🏁 Script executed: #!/bin/bash
# Look for how segment token is handled in client-side code
rg "vtex_segment|SEGMENT_COOKIE_NAME" . --type js --type ts -B 3 -A 3 2>/dev/null | head -100Repository: deco-cx/apps Length of output: 6261 🏁 Script executed: #!/bin/bash
# Check the VTEX Analytics implementation for segment cookie handling
cat ./vtex/sections/Analytics/Vtex.tsxRepository: deco-cx/apps Length of output: 3902 🏁 Script executed: #!/bin/bash
# Search for any reference to setting cookies client-side in the entire codebase
rg "setCookie|document\.cookie\s*=" -t ts -t js --max-count=20 2>/dev/null | grep -v node_modulesRepository: deco-cx/apps Length of output: 6856 🏁 Script executed: #!/bin/bash
# Check if there's any client-side segment handling at all
rg "vtex_segment|SALES_CHANNEL_COOKIE|SEGMENT_COOKIE_NAME" . -t ts -t js --type-not minified -B 2 -A 2 2>/dev/null | grep -v "utils/segment.ts" | head -80Repository: deco-cx/apps Length of output: 3802 Critical: Client-side cookie persistence is not implemented for segment cookies. The server-side logic correctly skips This creates a gap: For cacheable segments (UTM-only or default), cookies are neither set server-side nor persisted client-side. Clarify the intended behavior:
🤖 Prompt for AI Agents |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting check is currently failing CI.
The pipeline reports
deno fmt --checkfailure; please run formatter and commit the result.🤖 Prompt for AI Agents