1
1
// @ts -ignore
2
2
import AssetWorker from "@cloudflare/workers-shared/dist/asset-worker.mjs" ;
3
3
import { UNKNOWN_HOST } from "../shared" ;
4
- import type { WorkerEntrypoint } from "cloudflare:workers" ;
5
4
6
5
interface Env {
7
6
__VITE_ASSET_EXISTS__ : Fetcher ;
8
7
__VITE_FETCH_ASSET__ : Fetcher ;
9
8
}
10
9
11
- export default class CustomAssetWorker extends ( AssetWorker as typeof WorkerEntrypoint < Env > ) {
12
- override async fetch ( request : Request ) : Promise < Response > {
10
+ export default class CustomAssetWorker extends AssetWorker {
11
+ async fetch ( request : Request ) : Promise < Response > {
13
12
const response = await super . fetch ! ( request ) ;
14
13
const modifiedResponse = new Response ( response . body , response ) ;
15
14
modifiedResponse . headers . delete ( "ETag" ) ;
@@ -21,7 +20,9 @@ export default class CustomAssetWorker extends (AssetWorker as typeof WorkerEntr
21
20
eTag : string
22
21
) : Promise < { readableStream : ReadableStream ; contentType : string } > {
23
22
const url = new URL ( eTag , UNKNOWN_HOST ) ;
24
- const response = await this . env . __VITE_FETCH_ASSET__ . fetch ( url ) ;
23
+ const response = await (
24
+ this as typeof AssetWorker as { env : Env }
25
+ ) . env . __VITE_FETCH_ASSET__ . fetch ( url ) ;
25
26
26
27
if ( ! response . body ) {
27
28
throw new Error ( `Unexpected error. No HTML found for ${ eTag } .` ) ;
@@ -32,9 +33,20 @@ export default class CustomAssetWorker extends (AssetWorker as typeof WorkerEntr
32
33
async unstable_exists ( pathname : string ) : Promise < string | null > {
33
34
// We need this regex to avoid getting `//` as a pathname, which results in an invalid URL. Should this be fixed upstream?
34
35
const url = new URL ( pathname . replace ( / ^ \/ { 2 , } / , "/" ) , UNKNOWN_HOST ) ;
35
- const response = await this . env . __VITE_ASSET_EXISTS__ . fetch ( url ) ;
36
+ const response = await (
37
+ this as typeof AssetWorker as { env : Env }
38
+ ) . env . __VITE_ASSET_EXISTS__ . fetch ( url ) ;
36
39
const exists = await response . json ( ) ;
37
40
38
41
return exists ? pathname : null ;
39
42
}
43
+ async unstable_canFetch ( request : Request ) {
44
+ // the 'sec-fetch-mode: navigate' header is stripped by something on its way into this worker
45
+ // so we restore it from 'x-mf-sec-fetch-mode'
46
+ const secFetchMode = request . headers . get ( "X-Mf-Sec-Fetch-Mode" ) ;
47
+ if ( secFetchMode ) {
48
+ request . headers . set ( "Sec-Fetch-Mode" , secFetchMode ) ;
49
+ }
50
+ return await super . unstable_canFetch ( request ) ;
51
+ }
40
52
}
0 commit comments