@@ -109,6 +109,14 @@ function randomId(): string {
109109 } ) ;
110110}
111111
112+ // URLs are often relative to the zone. Sometimes the base zone
113+ // will be grey-clouded, and so the host must be swapped out for
114+ // the worker route host, which is more likely to be orange-clouded
115+ function switchHost ( originalUrl : string , host ?: string ) : URL {
116+ const url = new URL ( originalUrl ) ;
117+ url . hostname = host ?? url . hostname ;
118+ return url ;
119+ }
112120/**
113121 * Generates a preview session token.
114122 */
@@ -129,9 +137,13 @@ export async function createPreviewSession(
129137 abortSignal
130138 ) ;
131139
132- logger . debug ( `-- START EXCHANGE API REQUEST: GET ${ exchange_url } ` ) ;
140+ const switchedExchangeUrl = switchHost ( exchange_url , ctx . host ) . toString ( ) ;
141+
142+ logger . debug ( `-- START EXCHANGE API REQUEST: GET ${ switchedExchangeUrl } ` ) ;
133143 logger . debug ( "-- END EXCHANGE API REQUEST" ) ;
134- const exchangeResponse = await fetch ( exchange_url , { signal : abortSignal } ) ;
144+ const exchangeResponse = await fetch ( switchedExchangeUrl , {
145+ signal : abortSignal ,
146+ } ) ;
135147 const bodyText = await exchangeResponse . text ( ) ;
136148 logger . debug (
137149 "-- START EXCHANGE API RESPONSE:" ,
@@ -147,16 +159,15 @@ export async function createPreviewSession(
147159 token : string ;
148160 prewarm : string ;
149161 } > ( bodyText ) ;
150-
151- const { host } = new URL ( inspector_websocket ) ;
152- const query = `cf_workers_preview_token=${ token } ` ;
162+ const inspector = new URL ( inspector_websocket ) ;
163+ inspector . searchParams . append ( "cf_workers_preview_token" , token ) ;
153164
154165 return {
155166 id : randomId ( ) ,
156167 value : token ,
157- host,
158- inspectorUrl : new URL ( ` ${ inspector_websocket } ? ${ query } ` ) ,
159- prewarmUrl : new URL ( prewarm ) ,
168+ host : ctx . host ?? inspector . host ,
169+ inspectorUrl : switchHost ( inspector . href , ctx . host ) ,
170+ prewarmUrl : switchHost ( prewarm , ctx . host ) ,
160171 } ;
161172}
162173
0 commit comments