@@ -9,62 +9,58 @@ const prefix = `/${manifest.appDir}/`;
9
9
/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }> } */
10
10
const worker = {
11
11
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 ;
17
16
18
- let { pathname } = new URL ( req . url ) ;
17
+ let { pathname } = new URL ( req . url ) ;
19
18
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 ) ;
23
22
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'
43
30
}
31
+ } ) ;
32
+ } else {
33
+ // prerendered pages and index.html files
34
+ pathname = pathname . replace ( / \/ $ / , '' ) || '/' ;
44
35
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
60
42
}
61
43
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
+ }
67
59
}
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 ;
68
64
}
69
65
} ;
70
66
0 commit comments