File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments