1
1
import { Server } from 'SERVER' ;
2
2
import { manifest , prerendered , base_path } from 'MANIFEST' ;
3
+ import { env } from 'cloudflare:workers' ;
3
4
import * as Cache from 'worktop/cfw.cache' ;
4
5
5
6
const server = new Server ( manifest ) ;
@@ -9,6 +10,27 @@ const app_path = `/${manifest.appPath}`;
9
10
const immutable = `${ app_path } /immutable/` ;
10
11
const version_file = `${ app_path } /version.json` ;
11
12
13
+ /**
14
+ * We don't know the origin until we receive a request, but
15
+ * that's guaranteed to happen before we call `read`
16
+ * @type {string }
17
+ */
18
+ let origin ;
19
+
20
+ const initialized = server . init ( {
21
+ // @ts -expect-error env contains environment variables and bindings
22
+ env,
23
+ read : async ( file ) => {
24
+ const response = await /** @type {{ ASSETS: { fetch: typeof fetch } } } */ ( env ) . ASSETS . fetch (
25
+ `${ origin } /${ file } `
26
+ ) ;
27
+ if ( ! response . ok ) {
28
+ throw new Error ( `Failed to fetch ${ file } : ${ response . status } ${ response . statusText } ` ) ;
29
+ }
30
+ return response . body ;
31
+ }
32
+ } ) ;
33
+
12
34
export default {
13
35
/**
14
36
* @param {Request } req
@@ -17,10 +39,10 @@ export default {
17
39
* @returns {Promise<Response> }
18
40
*/
19
41
async fetch ( req , env , ctx ) {
20
- await server . init ( {
21
- // @ts -expect-error env contains environment variables and bindings
22
- env
23
- } ) ;
42
+ if ( ! origin ) {
43
+ origin = new URL ( req . url ) . origin ;
44
+ await initialized ;
45
+ }
24
46
25
47
// skip cache if "cache-control: no-cache" in request
26
48
let pragma = req . headers . get ( 'cache-control' ) || '' ;
0 commit comments