11import { Server } from 'SERVER' ;
22import { manifest , prerendered , base_path } from 'MANIFEST' ;
3+ import { env } from 'cloudflare:workers' ;
34import * as Cache from 'worktop/cfw.cache' ;
45
56const server = new Server ( manifest ) ;
@@ -9,6 +10,27 @@ const app_path = `/${manifest.appPath}`;
910const immutable = `${ app_path } /immutable/` ;
1011const version_file = `${ app_path } /version.json` ;
1112
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+
1234export default {
1335 /**
1436 * @param {Request } req
@@ -17,10 +39,10 @@ export default {
1739 * @returns {Promise<Response> }
1840 */
1941 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+ }
2446
2547 // skip cache if "cache-control: no-cache" in request
2648 let pragma = req . headers . get ( 'cache-control' ) || '' ;
0 commit comments