@@ -9,62 +9,58 @@ const prefix = `/${manifest.appDir}/`;
99/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }> } */
1010const worker = {
1111 async fetch ( req , env , context ) {
12- try {
13- // skip cache if "cache-control: no-cache" in request
14- let pragma = req . headers . get ( 'cache-control' ) || '' ;
15- let res = ! pragma . includes ( 'no-cache' ) && ( await Cache . lookup ( req ) ) ;
16- if ( res ) return res ;
12+ // skip cache if "cache-control: no-cache" in request
13+ let pragma = req . headers . get ( 'cache-control' ) || '' ;
14+ let res = ! pragma . includes ( 'no-cache' ) && ( await Cache . lookup ( req ) ) ;
15+ if ( res ) return res ;
1716
18- let { pathname } = new URL ( req . url ) ;
17+ let { pathname } = new URL ( req . url ) ;
1918
20- // static assets
21- if ( pathname . startsWith ( prefix ) ) {
22- res = await env . ASSETS . fetch ( req ) ;
19+ // static assets
20+ if ( pathname . startsWith ( prefix ) ) {
21+ res = await env . ASSETS . fetch ( req ) ;
2322
24- res = new Response ( res . body , {
25- headers : {
26- // include original cache headers, minus cache-control which
27- // is overridden, and etag which is no longer useful
28- 'cache-control' : 'public, immutable, max-age=31536000' ,
29- 'content-type' : res . headers . get ( 'content-type' ) ,
30- 'x-robots-tag' : 'noindex'
31- }
32- } ) ;
33- } else {
34- // prerendered pages and index.html files
35- pathname = pathname . replace ( / \/ $ / , '' ) || '/' ;
36-
37- let file = pathname . substring ( 1 ) ;
38-
39- try {
40- file = decodeURIComponent ( file ) ;
41- } catch ( err ) {
42- // ignore
23+ res = new Response ( res . body , {
24+ headers : {
25+ // include original cache headers, minus cache-control which
26+ // is overridden, and etag which is no longer useful
27+ 'cache-control' : 'public, immutable, max-age=31536000' ,
28+ 'content-type' : res . headers . get ( 'content-type' ) ,
29+ 'x-robots-tag' : 'noindex'
4330 }
31+ } ) ;
32+ } else {
33+ // prerendered pages and index.html files
34+ pathname = pathname . replace ( / \/ $ / , '' ) || '/' ;
4435
45- if (
46- manifest . assets . has ( file ) ||
47- manifest . assets . has ( file + '/index.html' ) ||
48- prerendered . has ( pathname )
49- ) {
50- res = await env . ASSETS . fetch ( req ) ;
51- } else {
52- // dynamically-generated pages
53- res = await server . respond ( req , {
54- platform : { env, context } ,
55- getClientAddress ( ) {
56- return req . headers . get ( 'cf-connecting-ip' ) ;
57- }
58- } ) ;
59- }
36+ let file = pathname . substring ( 1 ) ;
37+
38+ try {
39+ file = decodeURIComponent ( file ) ;
40+ } catch ( err ) {
41+ // ignore
6042 }
6143
62- // Writes to Cache only if allowed & specified
63- pragma = res . headers . get ( 'cache-control' ) ;
64- return pragma ? Cache . save ( req , res , context ) : res ;
65- } catch ( e ) {
66- return new Response ( 'Error rendering route: ' + ( e . message || e . toString ( ) ) , { status : 500 } ) ;
44+ if (
45+ manifest . assets . has ( file ) ||
46+ manifest . assets . has ( file + '/index.html' ) ||
47+ prerendered . has ( pathname )
48+ ) {
49+ res = await env . ASSETS . fetch ( req ) ;
50+ } else {
51+ // dynamically-generated pages
52+ res = await server . respond ( req , {
53+ platform : { env, context } ,
54+ getClientAddress ( ) {
55+ return req . headers . get ( 'cf-connecting-ip' ) ;
56+ }
57+ } ) ;
58+ }
6759 }
60+
61+ // Writes to Cache only if allowed & specified
62+ pragma = res . headers . get ( 'cache-control' ) ;
63+ return pragma ? Cache . save ( req , res , context ) : res ;
6864 }
6965} ;
7066
0 commit comments