-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer-request.js
More file actions
117 lines (103 loc) · 5.4 KB
/
Copy pathviewer-request.js
File metadata and controls
117 lines (103 loc) · 5.4 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
import cf from 'cloudfront';
/**
* CloudFront Viewer Request Function: Edge Optimize Routing
*
* This function intercepts incoming viewer requests and determines
* whether they originate from agentic bots (e.g., ChatGPT, GPTBot).
* If so, it routes the request to the Edge Optimize origin with
* automatic failover to the default origin.
*
* Non-agentic (regular user) requests pass through unchanged.
*/
function handler(event) {
var request = event.request;
var headers = request.headers;
// ---------------------------------------------------------------
// Security: Strip any Edge Optimize headers that may have been
// injected by the client. These headers should only be set by
// this function or the origin, never by the end user.
// ---------------------------------------------------------------
delete headers['x-edgeoptimize-api-key'];
delete headers['x-edgeoptimize-url'];
delete headers['x-edgeoptimize-config'];
// ---------------------------------------------------------------
// Configuration: Define which bots are considered "agentic"
// and which paths should be routed to Edge Optimize.
//
// AGENTIC_BOTS: List of known AI bot user-agent strings.
// Add or remove entries as needed.
//
// TARGETED_PATHS: Controls which paths are eligible for routing.
// - Set to `null` to target ALL HTML pages.
// - Set to an array of specific paths to limit routing,
// e.g., ['/', '/products', '/about']
// ---------------------------------------------------------------
var AGENTIC_BOTS = ['AdobeEdgeOptimize-AI', 'ChatGPT-User', 'GPTBot', 'OAI-SearchBot', 'PerplexityBot', 'Perplexity-User', 'ClaudeBot', 'Claude-User', 'Claude-SearchBot'];
var TARGETED_PATHS = null;
// ---------------------------------------------------------------
// Extract the User-Agent header (lowercase for case-insensitive matching)
// ---------------------------------------------------------------
var userAgent = headers['user-agent'] ? headers['user-agent'].value.toLowerCase() : '';
// Check if this request was already processed by Edge Optimize
// (prevents re-routing on sub-requests or redirects)
var isEdgeOptimizeRequest = headers['x-edgeoptimize-request'];
// ---------------------------------------------------------------
// Path matching: Only route requests for HTML pages.
// This regex matches URLs ending in:
// - `/` (directory index)
// - `.html` (explicit HTML files)
// - `/some-path` (extensionless paths, assumed to be pages)
// It excludes static assets like .css, .js, .png, .jpg, etc.
// ---------------------------------------------------------------
var path = request.uri;
var pattern = /(?:\/[^./]+|\.html|\/)$/;
var isHtmlPage = pattern.test(path);
// If TARGETED_PATHS is null, all HTML pages are targeted.
// Otherwise, only pages whose path is in the array are targeted.
var isTargetedPath = TARGETED_PATHS === null
? isHtmlPage
: isHtmlPage && TARGETED_PATHS.includes(path);
// ---------------------------------------------------------------
// Bot detection: Check if the User-Agent matches any known
// agentic bot (case-insensitive comparison)
// ---------------------------------------------------------------
var isAgenticBot = AGENTIC_BOTS.some(function(bot) {
return userAgent.includes(bot.toLowerCase());
});
// ---------------------------------------------------------------
// Routing decision:
// If the request is from an agentic bot, targets an HTML page,
// and has not already been processed by Edge Optimize, then:
// 1. Set x-edgeoptimize-url header with the original request URI
// 2. Set x-edgeoptimize-config header to enable LLM client mode
// 3. Create an origin group that tries Edge Optimize first,
// with automatic failover to the default origin on errors
// ---------------------------------------------------------------
if (!isEdgeOptimizeRequest && isAgenticBot && isTargetedPath) {
// Pass the original URI to Edge Optimize so it knows which page to optimize
request.headers['x-edgeoptimize-url'] = { value: request.uri };
// Enable LLM client optimization mode
request.headers['x-edgeoptimize-config'] = { value: "LLMCLIENT=true" };
console.log("Adding origin group for userAgent: " + userAgent);
// Create an origin group: try EdgeOptimize_Origin first,
// fall back to YOUR_DEFAULT_ORIGIN if Edge Optimize returns
// any of the listed error status codes.
cf.createRequestOriginGroup({
"originIds": [
{ "originId": "EdgeOptimize_Origin" },
{ "originId": "YOUR_DEFAULT_ORIGIN" }
],
"failoverCriteria": {
"statusCodes": [400, 403, 404, 416, 500, 502, 503, 504]
}
});
console.log("Routing to Edge Optimize origin for userAgent: " + userAgent);
return request;
}
// ---------------------------------------------------------------
// Default: Non-agentic requests (or non-targeted paths) pass
// through to the default origin with no modifications.
// ---------------------------------------------------------------
console.log("Routing to Default origin for userAgent: " + userAgent);
return request;
}