1- type BareResponseFetch = any ;
1+ type BareResponseFetch = Response & {
2+ finalURL ?: string ;
3+ rawHeaders : Record < string , string | string [ ] > ;
4+ } ;
25type BareClient = any ;
36import { EpoxyClient } from "@mercuryworkshop/epoxy-tls" ;
47
@@ -57,15 +60,15 @@ export async function handleFetch(
5760 } ) ;
5861 }
5962
60- let workerType = "" ;
63+ let scriptType = "" ;
6164 let topFrameName ;
6265 let parentFrameName ;
6366
64- let extraParams : Record < string , string > = { } ;
67+ const extraParams : Record < string , string > = { } ;
6568 for ( const [ param , value ] of [ ...requestUrl . searchParams . entries ( ) ] ) {
6669 switch ( param ) {
6770 case "type" :
68- workerType = value ;
71+ scriptType = value ;
6972 break ;
7073 case "dest" :
7174 break ;
@@ -117,7 +120,7 @@ export async function handleFetch(
117120 response as BareResponseFetch ,
118121 meta ,
119122 request . destination ,
120- workerType ,
123+ scriptType ,
121124 this . cookieStore
122125 ) ;
123126 }
@@ -272,9 +275,9 @@ export async function handleFetch(
272275 ) ;
273276 this . dispatchEvent ( ev ) ;
274277
275- const response : BareResponseFetch =
278+ const response =
276279 ( await ev . response ) ||
277- ( await this . epoxy . fetch ( ev . url , {
280+ ( ( await this . epoxy . fetch ( ev . url , {
278281 method : ev . method ,
279282 body : ev . body ,
280283 headers : ev . requestHeaders ,
@@ -284,13 +287,13 @@ export async function handleFetch(
284287 redirect : "manual" ,
285288 // @ts -ignore why the fuck is this not typed microsoft
286289 duplex : "half" ,
287- } ) ) ;
290+ } ) ) as BareResponseFetch ) ;
288291 response . finalURL = ev . url . href ;
289292
290293 return await handleResponse (
291294 url ,
292295 meta ,
293- workerType ,
296+ scriptType ,
294297 request . destination ,
295298 request . mode ,
296299 response ,
@@ -330,7 +333,7 @@ export async function handleFetch(
330333async function handleResponse (
331334 url : URL ,
332335 meta : URLMeta ,
333- workertype : string ,
336+ scriptType : string ,
334337 destination : RequestDestination ,
335338 mode : RequestMode ,
336339 response : BareResponseFetch ,
@@ -382,6 +385,13 @@ async function handleResponse(
382385 bareClient
383386 ) ;
384387 await getMostRestrictiveSite ( redirectUrl . toString ( ) , newSiteDirective ) ;
388+
389+ // ensure that ?type=module is not lost in a redirect
390+ if ( scriptType ) {
391+ const url = new URL ( responseHeaders [ "location" ] ) ;
392+ url . searchParams . set ( "type" , scriptType ) ;
393+ responseHeaders [ "location" ] = url . href ;
394+ }
385395 }
386396
387397 const maybeHeaders = responseHeaders [ "set-cookie" ] || [ ] ;
@@ -414,7 +424,7 @@ async function handleResponse(
414424 response ,
415425 meta ,
416426 destination ,
417- workertype ,
427+ scriptType ,
418428 cookieStore
419429 ) ;
420430 }
0 commit comments