-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
168 lines (138 loc) · 6.22 KB
/
Copy pathworker.js
File metadata and controls
168 lines (138 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Edge Optimize BYOCDN - Cloudflare Worker
*
* This worker routes requests from agentic bots (AI/LLM user agents) to the
* Edge Optimize backend service for optimized content delivery.
*
* Features:
* - Routes agentic bot traffic to Edge Optimize backend
* - Failover to origin on Edge Optimize errors (any 4XX or 5XX errors)
* - Loop protection to prevent infinite redirects
* - Human visitors and SEO bots are served from the origin as usual
*/
// List of agentic bot user agents to route to Edge Optimize
const AGENTIC_BOTS = [
'AdobeEdgeOptimize-AI',
'ChatGPT-User',
'GPTBot',
'OAI-SearchBot',
'PerplexityBot',
'Perplexity-User',
'ClaudeBot',
'Claude-User',
'Claude-SearchBot'
];
// Targeted paths for Edge Optimize routing
// Set to null to route all HTML pages, or specify an array of paths
const TARGETED_PATHS = null; // e.g., ['/', '/page.html', '/products']
// Failover configuration
// Failover on any 4XX client error or 5XX server error from Edge Optimize
const FAILOVER_ON_4XX = true; // Failover on any 4XX error (400-499)
const FAILOVER_ON_5XX = true; // Failover on any 5XX error (500-599)
export default {
async fetch(request, env, ctx) {
return await handleRequest(request, env, ctx);
},
};
async function handleRequest(request, env, ctx) {
const url = new URL(request.url);
const userAgent = request.headers.get("user-agent")?.toLowerCase() || "";
// Check if request is already processed (loop protection)
const isEdgeOptimizeRequest = !!request.headers.get("x-edgeoptimize-request");
// Construct the original path and query string
const pathAndQuery = `${url.pathname}${url.search}`;
// Check if the path matches HTML pages (no extension or .html extension)
const isHtmlPage = /(?:\/[^./]+|\.html|\/)$/.test(url.pathname);
// Check if path is in targeted paths (if specified)
const isTargetedPath = TARGETED_PATHS === null
? isHtmlPage
: (isHtmlPage && TARGETED_PATHS.includes(url.pathname));
// Check if user agent is an agentic bot
const isAgenticBot = AGENTIC_BOTS.some((ua) => userAgent.includes(ua.toLowerCase()));
// Route to Edge Optimize if:
// 1. Request is NOT already from Edge Optimize (prevents infinite loops)
// 2. User agent matches one of the agentic bots
// 3. Path is targeted for optimization
if (!isEdgeOptimizeRequest && isAgenticBot && isTargetedPath) {
// Build the Edge Optimize request URL
const edgeOptimizeURL = `https://live.edgeoptimize.net${pathAndQuery}`;
// Clone and modify headers for the Edge Optimize request
const edgeOptimizeHeaders = new Headers(request.headers);
// Remove any existing Edge Optimize headers for security
edgeOptimizeHeaders.delete("x-edgeoptimize-api-key");
edgeOptimizeHeaders.delete("x-edgeoptimize-url");
edgeOptimizeHeaders.delete("x-edgeoptimize-config");
edgeOptimizeHeaders.delete("x-edgeoptimize-fetcher-key"); // Optional (required only in case of WAF)
// x-forwarded-host: The original site domain
// Use environment variable if set, otherwise use the request host
edgeOptimizeHeaders.set("x-forwarded-host", env.EDGE_OPTIMIZE_TARGET_HOST ?? url.host);
// x-edgeoptimize-api-key: Your Adobe-provided API key
edgeOptimizeHeaders.set("x-edgeoptimize-api-key", env.EDGE_OPTIMIZE_API_KEY);
// x-edgeoptimize-url: The original request URL path and query
edgeOptimizeHeaders.set("x-edgeoptimize-url", pathAndQuery);
// x-edgeoptimize-config: Configuration for cache key differentiation
edgeOptimizeHeaders.set("x-edgeoptimize-config", "LLMCLIENT=TRUE;");
// edgeOptimizeHeaders.set("x-edgeoptimize-fetcher-key", "<YOUR FETCHER KEY>"); // Optional (required only in case of WAF)
try {
// Send request to Edge Optimize backend
const edgeOptimizeResponse = await fetch(new Request(edgeOptimizeURL, {
headers: edgeOptimizeHeaders,
redirect: "manual", // Preserve redirect responses from Edge Optimize
}), {
cf: {
cacheEverything: true, // Enable caching based on origin's cache-control headers
},
});
// Check if we need to failover to origin
const status = edgeOptimizeResponse.status;
const is4xxError = FAILOVER_ON_4XX && status >= 400 && status < 500;
const is5xxError = FAILOVER_ON_5XX && status >= 500 && status < 600;
if (is4xxError || is5xxError) {
console.log(`Edge Optimize returned ${status}, failing over to origin`);
return await failoverToOrigin(request, env, url);
}
// Return the Edge Optimize response
return edgeOptimizeResponse;
} catch (error) {
// Network error or timeout - failover to origin
console.log(`Edge Optimize request failed: ${error.message}, failing over to origin`);
return await failoverToOrigin(request, env, url);
}
}
// For all other requests (human users, SEO bots), pass through unchanged
return fetch(request);
}
/**
* Failover to origin server when Edge Optimize returns an error
* @param {Request} request - The original request
* @param {Object} env - Environment variables
* @param {URL} url - Parsed URL object
*/
async function failoverToOrigin(request, env, url) {
// Build origin URL
const originURL = `${url.protocol}//${env.EDGE_OPTIMIZE_TARGET_HOST}${url.pathname}${url.search}`;
// Prepare headers - clean Edge Optimize headers and add loop protection
const originHeaders = new Headers(request.headers);
originHeaders.set("Host", env.EDGE_OPTIMIZE_TARGET_HOST);
originHeaders.delete("x-edgeoptimize-api-key");
originHeaders.delete("x-edgeoptimize-url");
originHeaders.delete("x-edgeoptimize-config");
originHeaders.delete("x-forwarded-host");
originHeaders.set("x-edgeoptimize-request", "fo");
// Create and send origin request
const originRequest = new Request(originURL, {
method: request.method,
headers: originHeaders,
body: request.body,
redirect: "manual",
});
const originResponse = await fetch(originRequest);
// Add failover marker header to response
const modifiedResponse = new Response(originResponse.body, {
status: originResponse.status,
statusText: originResponse.statusText,
headers: originResponse.headers,
});
modifiedResponse.headers.set("x-edgeoptimize-fo", "1");
return modifiedResponse;
}