Skip to content

Commit 636ac5d

Browse files
committed
Modify cookie expiry from session to 1 day
1 parent 7c16e53 commit 636ac5d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

public/_worker.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ export default {
1111
if (url.pathname === ('/api/fred')) {
1212
// TODO: Add custom logic here.
1313
url.pathname = newHomepagePathName;
14-
return env.ASSETS.fetch(url);
15-
//return new Response('Ok api called');
14+
15+
const percentage = Math.floor(Math.random() * 100);
16+
let version = "current"; // default version
17+
// change pathname and version name for 50% of traffic
18+
if (percentage < 50) {
19+
url.pathname = newHomepagePathName;
20+
version = "new";
21+
}
22+
// get the static file from ASSETS, and attach a cookie
23+
const asset = await env.ASSETS.fetch(url);
24+
let response = new Response(asset.body, asset);
25+
let expires = (new Date(Date.now()+ 86400*1000)).toUTCString(); // Valid for 1 Day = 24 Hrs = 24*60*60 = 86400.
26+
response.headers.append("Set-Cookie", `${cookieName}=${version}; expires= ${expires} ;path=/`);
27+
return response;
28+
1629
}
1730
// Otherwise, serve the static assets.
1831
// Without this, the Worker will error and no assets will be served.

0 commit comments

Comments
 (0)