@@ -4,8 +4,6 @@ import * as Cache from 'worktop/cfw.cache';
44
55const server = new Server ( manifest ) ;
66
7- const app_path = `/${ manifest . appPath } /` ;
8-
97/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }> } */
108const worker = {
119 async fetch ( req , env , context ) {
@@ -17,64 +15,42 @@ const worker = {
1715 if ( res ) return res ;
1816
1917 let { pathname } = new URL ( req . url ) ;
18+ try {
19+ pathname = decodeURIComponent ( pathname ) ;
20+ } catch {
21+ // ignore invalid URI
22+ }
2023
21- // generated files
22- if ( pathname . startsWith ( app_path ) ) {
23- res = await env . ASSETS . fetch ( req ) ;
24- if ( ! res . ok ) return res ;
24+ const stripped_pathname = pathname . replace ( / \/ $ / , '' ) ;
2525
26- const cache_control = pathname . startsWith ( app_path + 'immutable/' )
27- ? 'public, immutable, max-age=31536000'
28- : 'no-cache' ;
26+ // prerendered pages and /static files
27+ let is_static_asset = false ;
28+ const filename = stripped_pathname . substring ( 1 ) ;
29+ if ( filename ) {
30+ is_static_asset =
31+ manifest . assets . has ( filename ) || manifest . assets . has ( filename + '/index.html' ) ;
32+ }
2933
30- res = new Response ( res . body , {
34+ const location = pathname . at ( - 1 ) === '/' ? stripped_pathname : pathname + '/' ;
35+
36+ if ( is_static_asset || prerendered . has ( pathname ) ) {
37+ res = await env . ASSETS . fetch ( req ) ;
38+ } else if ( location && prerendered . has ( location ) ) {
39+ res = new Response ( '' , {
40+ status : 308 ,
3141 headers : {
32- // include original headers, minus cache-control which
33- // is overridden, and etag which is no longer useful
34- 'cache-control' : cache_control ,
35- 'content-type' : res . headers . get ( 'content-type' ) ,
36- 'x-robots-tag' : 'noindex'
42+ location
3743 }
3844 } ) ;
3945 } else {
40- // prerendered pages and /static files
41-
42- try {
43- pathname = decodeURIComponent ( pathname ) ;
44- } catch {
45- // ignore invalid URI
46- }
47-
48- const stripped_pathname = pathname . replace ( / \/ $ / , '' ) ;
49-
50- let is_static_asset = false ;
51- const filename = stripped_pathname . substring ( 1 ) ;
52- if ( filename ) {
53- is_static_asset =
54- manifest . assets . has ( filename ) || manifest . assets . has ( filename + '/index.html' ) ;
55- }
56-
57- const counterpart_route = pathname . at ( - 1 ) === '/' ? stripped_pathname : pathname + '/' ;
58-
59- if ( is_static_asset || prerendered . has ( pathname ) ) {
60- res = await env . ASSETS . fetch ( req ) ;
61- } else if ( counterpart_route && prerendered . has ( counterpart_route ) ) {
62- res = new Response ( '' , {
63- status : 308 ,
64- headers : {
65- location : counterpart_route
66- }
67- } ) ;
68- } else {
69- // dynamically-generated pages
70- res = await server . respond ( req , {
71- // @ts -ignore
72- platform : { env, context, caches } ,
73- getClientAddress ( ) {
74- return req . headers . get ( 'cf-connecting-ip' ) ;
75- }
76- } ) ;
77- }
46+ // dynamically-generated pages
47+ res = await server . respond ( req , {
48+ // @ts -ignore
49+ platform : { env, context, caches } ,
50+ getClientAddress ( ) {
51+ return req . headers . get ( 'cf-connecting-ip' ) ;
52+ }
53+ } ) ;
7854 }
7955
8056 // Writes to Cache only if allowed & specified
0 commit comments