-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-worker.js
64 lines (47 loc) · 2.35 KB
/
new-worker.js
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
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// Parse the URL from the incoming request
const url = new URL(request.url)
// List of paths to redirect
const pathsToRedirect = ["/custom-path-1", "/custom-path-2"]
// Check if the request path matches any of the specified paths
if (pathsToRedirect.includes(url.pathname)) {
// Set the destination URL based on the path
let destinationUrl = ''
if (url.pathname === "/custom-path-1") {
destinationUrl = 'YOUR-CUSTOM-FUNNEL-URL-1'
} else if (url.pathname === "/custom-path-2") {
destinationUrl = 'YOUR-CUSTOM-FUNNEL-URL-2'
}
// Create a new request object with the destination URL
const modifiedRequest = new Request(destinationUrl, request)
// Fetch the response from the destination URL
const response = await fetch(modifiedRequest)
// Create a new response based on the original response
const newResponse = new Response(response.body, response)
// Add CORS headers to allow access from any origin
newResponse.headers.set('Access-Control-Allow-Origin', '*')
// Ensure the correct content type is set (defaulting to HTML if missing)
newResponse.headers.set('Content-Type', response.headers.get('Content-Type') || 'text/html')
// Return the modified response to the user, keeping the original URL in the browser
return newResponse
}
// For all other routes, handle normally
const destinationUrl = 'YOUR-FUNNEL-URL'
// Modify the hostname but keep the original path and query parameters
url.hostname = destinationUrl.replace(/^https?:\/\//, '')
// Create a new request object with the modified URL
const modifiedRequest = new Request(url, request)
// Send the modified request to the new URL and await the response
const response = await fetch(modifiedRequest)
// Create a new response based on the original, but modify headers if needed
const newResponse = new Response(response.body, response)
// Set CORS headers to allow access from any origin
newResponse.headers.set('Access-Control-Allow-Origin', '*')
// Ensure the correct content type is set (defaulting to HTML if missing)
newResponse.headers.set('Content-Type', response.headers.get('Content-Type') || 'text/html')
// Return the final response to the user
return newResponse
}