3
3
// applied after this worker runs.
4
4
5
5
// When any cookie in this list is present in the request, cache will be skipped
6
- const PRIVATE_COOKIES = [ " sessionid" ] ;
6
+ const PRIVATE_COOKIES = [ ' sessionid' ] ;
7
7
8
8
// These querystring keys are stripped from the request as they are generally not
9
9
// needed by the origin.
10
10
const STRIP_QUERYSTRING_KEYS = [
11
- " utm_source" ,
12
- " utm_campaign" ,
13
- " utm_medium" ,
14
- " utm_term" ,
15
- " utm_content" ,
16
- " gclid" ,
17
- " fbclid" ,
18
- " dm_i" , // DotDigital
19
- " msclkid" ,
20
- " al_applink_data" , // Meta outbound app links
11
+ ' utm_source' ,
12
+ ' utm_campaign' ,
13
+ ' utm_medium' ,
14
+ ' utm_term' ,
15
+ ' utm_content' ,
16
+ ' gclid' ,
17
+ ' fbclid' ,
18
+ ' dm_i' , // DotDigital
19
+ ' msclkid' ,
20
+ ' al_applink_data' , // Meta outbound app links
21
21
22
22
// https://docs.flying-press.com/cache/ignore-query-strings
23
- " age-verified" ,
24
- " ao_noptimize" ,
25
- " usqp" ,
26
- " cn-reloaded" ,
27
- " sscid" ,
28
- " ef_id" ,
29
- " _bta_tid" ,
30
- " _bta_c" ,
31
- " fb_action_ids" ,
32
- " fb_action_types" ,
33
- " fb_source" ,
34
- " _ga" ,
35
- " adid" ,
36
- " _gl" ,
37
- " gclsrc" ,
38
- " gdfms" ,
39
- " gdftrk" ,
40
- " gdffi" ,
41
- " _ke" ,
42
- " trk_contact" ,
43
- " trk_msg" ,
44
- " trk_module" ,
45
- " trk_sid" ,
46
- " mc_cid" ,
47
- " mc_eid" ,
48
- " mkwid" ,
49
- " pcrid" ,
50
- " mtm_source" ,
51
- " mtm_medium" ,
52
- " mtm_campaign" ,
53
- " mtm_keyword" ,
54
- " mtm_cid" ,
55
- " mtm_content" ,
56
- " epik" ,
57
- "pp" ,
58
- " pk_source" ,
59
- " pk_medium" ,
60
- " pk_campaign" ,
61
- " pk_keyword" ,
62
- " pk_cid" ,
63
- " pk_content" ,
64
- " redirect_log_mongo_id" ,
65
- " redirect_mongo_id" ,
66
- " sb_referer_host" ,
23
+ ' age-verified' ,
24
+ ' ao_noptimize' ,
25
+ ' usqp' ,
26
+ ' cn-reloaded' ,
27
+ ' sscid' ,
28
+ ' ef_id' ,
29
+ ' _bta_tid' ,
30
+ ' _bta_c' ,
31
+ ' fb_action_ids' ,
32
+ ' fb_action_types' ,
33
+ ' fb_source' ,
34
+ ' _ga' ,
35
+ ' adid' ,
36
+ ' _gl' ,
37
+ ' gclsrc' ,
38
+ ' gdfms' ,
39
+ ' gdftrk' ,
40
+ ' gdffi' ,
41
+ ' _ke' ,
42
+ ' trk_contact' ,
43
+ ' trk_msg' ,
44
+ ' trk_module' ,
45
+ ' trk_sid' ,
46
+ ' mc_cid' ,
47
+ ' mc_eid' ,
48
+ ' mkwid' ,
49
+ ' pcrid' ,
50
+ ' mtm_source' ,
51
+ ' mtm_medium' ,
52
+ ' mtm_campaign' ,
53
+ ' mtm_keyword' ,
54
+ ' mtm_cid' ,
55
+ ' mtm_content' ,
56
+ ' epik' ,
57
+ 'pp' ,
58
+ ' pk_source' ,
59
+ ' pk_medium' ,
60
+ ' pk_campaign' ,
61
+ ' pk_keyword' ,
62
+ ' pk_cid' ,
63
+ ' pk_content' ,
64
+ ' redirect_log_mongo_id' ,
65
+ ' redirect_mongo_id' ,
66
+ ' sb_referer_host' ,
67
67
] ;
68
68
69
69
// If this is true, the querystring keys stripped from the request will be
70
70
// addeed to any Location header served by a redirect.
71
- const REPLACE_STRIPPED_QUERYSTRING_ON_REDIRECT_LOCATION = false
71
+ const REPLACE_STRIPPED_QUERYSTRING_ON_REDIRECT_LOCATION = false ;
72
72
73
73
// If this is true, querystring key are stripped if they have no value eg. ?foo
74
74
// Disabled by default, but highly recommended
@@ -78,7 +78,7 @@ const STRIP_VALUELESS_QUERYSTRING_KEYS = false;
78
78
// (from https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4)
79
79
const CACHABLE_HTTP_STATUS_CODES = [ 200 , 203 , 206 , 300 , 301 , 410 ] ;
80
80
81
- addEventListener ( " fetch" , ( event ) => {
81
+ addEventListener ( ' fetch' , ( event ) => {
82
82
event . respondWith ( main ( event ) ) ;
83
83
} ) ;
84
84
@@ -106,7 +106,10 @@ async function main(event) {
106
106
}
107
107
108
108
if ( REPLACE_STRIPPED_QUERYSTRING_ON_REDIRECT_LOCATION ) {
109
- response = replaceStrippedQsOnRedirectResponse ( response , strippedParams ) ;
109
+ response = replaceStrippedQsOnRedirectResponse (
110
+ response ,
111
+ strippedParams ,
112
+ ) ;
110
113
}
111
114
112
115
return response ;
@@ -120,15 +123,15 @@ function requestIsCachable(request) {
120
123
* Given a Request, determine if it should be cached.
121
124
* Currently the only factor here is whether a private cookie is present.
122
125
*/
123
- return ! hasPrivateCookie ( request )
126
+ return ! hasPrivateCookie ( request ) ;
124
127
}
125
128
126
129
function responseIsCachable ( response ) {
127
130
/*
128
131
* Given a Response, determine if it should be cached.
129
132
* Currently the only factor here is whether the status code is cachable.
130
133
*/
131
- return CACHABLE_HTTP_STATUS_CODES . includes ( response . status )
134
+ return CACHABLE_HTTP_STATUS_CODES . includes ( response . status ) ;
132
135
}
133
136
134
137
function getCachingRequest ( request ) {
@@ -142,12 +145,14 @@ function getCachingRequest(request) {
142
145
const requestURL = new URL ( request . url ) ;
143
146
144
147
// Cache based on the mode
145
- requestURL . searchParams . set ( 'cookie-torchbox-mode' , cookies [ 'torchbox-mode' ] || 'dark' ) ;
148
+ requestURL . searchParams . set (
149
+ 'cookie-torchbox-mode' ,
150
+ cookies [ 'torchbox-mode' ] || 'dark' ,
151
+ ) ;
146
152
147
153
return new Request ( requestURL , request ) ;
148
154
}
149
155
150
-
151
156
/*
152
157
* Request Utilities
153
158
*/
@@ -189,7 +194,7 @@ function hasPrivateCookie(request) {
189
194
/*
190
195
* Given a Request, determine if one of the 'private' cookies are present.
191
196
*/
192
- const cookieHeader = request . headers . get ( " Cookie" ) ;
197
+ const cookieHeader = request . headers . get ( ' Cookie' ) ;
193
198
if ( ! cookieHeader ) {
194
199
return false ;
195
200
}
@@ -234,7 +239,7 @@ function replaceStrippedQsOnRedirectResponse(response, strippedParams) {
234
239
response = new Response ( response . body , response ) ;
235
240
236
241
if ( [ 301 , 302 ] . includes ( response . status ) ) {
237
- const locationHeaderValue = response . headers . get ( " location" ) ;
242
+ const locationHeaderValue = response . headers . get ( ' location' ) ;
238
243
let locationUrl ;
239
244
240
245
if ( ! locationHeaderValue ) {
@@ -246,9 +251,12 @@ function replaceStrippedQsOnRedirectResponse(response, strippedParams) {
246
251
if ( ! isAbsolute ) {
247
252
// If the Location URL isn't absolute, we need to provide a Host so we can use
248
253
// a URL object.
249
- locationUrl = new URL ( locationHeaderValue , "http://www.example.com" ) ;
254
+ locationUrl = new URL (
255
+ locationHeaderValue ,
256
+ 'http://www.example.com' ,
257
+ ) ;
250
258
} else {
251
- locationUrl = new URL ( locationHeaderValue )
259
+ locationUrl = new URL ( locationHeaderValue ) ;
252
260
}
253
261
254
262
for ( const [ key , value ] of Object . entries ( strippedParams ) ) {
@@ -258,20 +266,20 @@ function replaceStrippedQsOnRedirectResponse(response, strippedParams) {
258
266
let newLocation ;
259
267
260
268
if ( isAbsolute ) {
261
- newLocation = locationUrl . toString ( )
269
+ newLocation = locationUrl . toString ( ) ;
262
270
} else {
263
271
newLocation = `${ locationUrl . pathname } ${ locationUrl . search } ` ;
264
272
}
265
273
266
- response . headers . set ( " location" , newLocation ) ;
274
+ response . headers . set ( ' location' , newLocation ) ;
267
275
}
268
276
269
- return response
277
+ return response ;
270
278
}
271
279
272
280
/**
273
281
* URL Utilities
274
282
*/
275
283
function isUrlAbsolute ( url ) {
276
- return ( url . indexOf ( '://' ) > 0 || url . indexOf ( '//' ) === 0 ) ;
284
+ return url . indexOf ( '://' ) > 0 || url . indexOf ( '//' ) === 0 ;
277
285
}
0 commit comments