-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirects-v2
106 lines (95 loc) · 4.34 KB
/
redirects-v2
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
/**
* Example using Service Worker to Implement Redirects
* You can see this here https://cloudflareworkers.com/#31db00788f92250dc6e7ce88fbda5c59:https://blog.cloudflare.com/old-category.asp
*/
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
/**
* Redirect if matches case
* @param {Request} request
*/
async function fetchAndApply(request) {
// This extracts the path so you have a pattern to match
var url = new URL(request.url);
var path = url.pathname;
// console.log('Path', path);
// This is to set-up any default protocol or host to minimize multiple redirects
// You can switch to using requested protocol (2nd option) if needed for dynamic environments
// var site = "https://www.cloudflare.com";
var site = url.protocol + "//" + url.host;
// console.log('Site:', site);
// This is a regex to match any extensions that we want to skip, these are static resources
var staticRegex = /\.(css|js|json|otf|ttf|woff|woff2|doc|pdf|ppt|xls|tsv|txt|xml|tif|tiff|vcf|bmp|gif|ief|jpe|jpeg|jpg|ico|png|svg|webp|m1v|mov|mp2|mp3|mp4|mpa|mpe|mpeg|mpg|pbm|pgm|pnm|ppm|pps|ps|qt|ras|rgb|swf|wav|xbm|xpdl|xpm|xwd)\/?$/i;
if (staticRegex.test(path) === true) {
var actionGroup = 'skip';
var actionCause = 'Skip filetype:';
// console.log(actionCause,path);
}
// This is regex to match specifically known files that are needed to load a page, so we can skip them
var knownRegex = /(specs\.html)|(iframe_history\.html|\.cloudflare\/ga)$/i;
if (knownRegex.test(path) === true) {
var actionGroup = 'skip';
var actionCause = 'Skip known path:';
// console.log(actionCause,path);
}
// This is a regex to match any legacy file extensions, such as .asp or .jsp so we can process them without delaying other requests
var legacyRegex = /\.(asp|jsp)$/i;
if (legacyRegex.test(path) === true) {
var actionGroup = 'legacy';
var actionCause = 'Redirect Legacy:';
// console.log(actionCause,path);
}
// If we know the action group, take action, otherwise apply defaults
switch(actionGroup) {
// This is any bucket that we've flagged to skip straight to loading
case "skip":
console.log(actionCause, path);
const response = await fetch(request);
return response;
break;
// This is a custom bucket of "legacy" pages we can check against
case "legacy":
// console.log(actionCause, path);
// create object with list of matches for redirects
const legacyRedirects = {
'/old-category.jsp': site + '/products/cloudflare-workers/',
'/example-2.asp': 'https://support.cloudflare.com/',
'/example-3.jsp': 'https://developers.cloudflare.com/workers/api/route-matching/',
'/test.asp': site + '/plans',
'/test-2.jsp': 'https://developers.cloudflare.com/workers/about/how-workers-work/',
'/test-3.asp': 'https://www.hivedigital.com/'
}
var legacyDestination = legacyRedirects[path];
if (typeof legacyDestination != 'undefined') {
console.log(actionCause,path, '->', legacyDestination);
return Response.redirect(legacyDestination, 301);
} else {
console.log('Default', actionCause, path);
return Response.redirect('https://www.hivedigital.com/2018/12/18/redirects-using-cloudflare-workers/', 301);
}
break;
// In this case, we didn't apply a pattern match, so there may be some one-off redirects to implement.
default:
// console.log('No Bucket: ', path);
// create object with list of matches for redirects
const noBucketredirects = {
'/foo-category': site + '/products/cloudflare-workers/',
'/foo-2': 'https://support.cloudflare.com/',
'/foo-3': 'https://developers.cloudflare.com/workers/api/route-matching/',
'/test': site + '/plans',
'/test-2': 'https://developers.cloudflare.com/workers/about/how-workers-work/',
'/test-3': 'https://www.hivedigital.com/'
}
var noBucketdestination = noBucketredirects[path];
if (typeof noBucketdestination != 'undefined') {
console.log('Default: ',path,'->',noBucketdestination);
return Response.redirect(noBucketdestination, 301);
} else {
console.log('Load: ',path);
const response = await fetch(request);
return response;
}
break;
}
}