44
55
66// update worker name when updating worker
7- const WORKER_NAME = 'codeit-worker-v501 ' ;
7+ const WORKER_NAME = 'codeit-worker-v503 ' ;
88
99
1010// internal paths
@@ -16,12 +16,19 @@ const INTERNAL_PATHS = {
1616 run : 'https://codeit.codes/run' ,
1717 run_ : 'https://dev.codeit.codes/run' ,
1818
19+ relLivePath : ( '/run/' + '_/' . repeat ( 15 ) ) ,
20+
1921 clientId : 'https://codeit.codes/worker/getClientId' ,
2022 clientId_ : 'https://dev.codeit.codes/worker/getClientId' ,
2123
2224}
2325
2426
27+ // key : value
28+ // live view client ID: codeit client ID
29+ let liveViewClients = { } ;
30+
31+
2532// get path type
2633function getPathType ( path ) {
2734
@@ -72,51 +79,56 @@ function createResponse(data, type, status) {
7279
7380
7481// send fetch request to client
75- function sendRequestToClient ( request ) {
82+ function sendRequestToClient ( request , clientId ) {
7683
7784 return new Promise ( ( resolve , reject ) => {
7885
86+ // get client ID
87+ clientId = liveViewClients [ clientId ] ?? clientId ;
88+
89+ let url = request . url ;
90+
91+ // append .html to url if navigating
92+ if ( request . mode === 'navigate'
93+ && ! url . endsWith ( '.html' )
94+ && ! url . endsWith ( '/' ) ) url += '.html' ;
95+
96+ // send request to client
97+ workerChannel . postMessage ( {
98+ url : url ,
99+ toClient : clientId ,
100+ type : 'request'
101+ } ) ;
102+
103+
79104 // set MIME type depending on request mode
80105 let mimeType = 'application/octet-stream' ;
81106
82107 if ( request . mode === 'navigate'
83- || request . url . endsWith ( '.html' ) ) mimeType = 'text/html' ;
108+ || url . endsWith ( '.html' ) ) mimeType = 'text/html' ;
84109
85110 if ( request . mode === 'script'
86- || request . url . endsWith ( '.js' ) ) mimeType = 'text/javascript' ;
111+ || url . endsWith ( '.js' ) ) mimeType = 'text/javascript' ;
87112
88113 if ( request . mode === 'style'
89- || request . url . endsWith ( '.css' ) ) mimeType = 'text/css' ;
114+ || url . endsWith ( '.css' ) ) mimeType = 'text/css' ;
115+
116+ if ( url . endsWith ( '.wasm' ) ) mimeType = 'application/wasm' ;
90117
91- if ( request . url . endsWith ( '.wasm' ) ) mimeType = 'application/wasm' ;
92118
93119 if ( enableDevLogs ) {
94120 console . warn ( mimeType , request . mode , request . url ) ;
95121 }
96122
97123
98- let url = request . url ;
99-
100- // append .html to url if navigating
101- if ( request . mode === 'navigate'
102- && ! url . endsWith ( '.html' )
103- && ! url . endsWith ( '/' ) ) url += '.html' ;
104-
105-
106- // send request to client
107- workerChannel . postMessage ( {
108- url : url ,
109- type : 'request'
110- } ) ;
111-
112-
113124 // add worker/client channel listener
114125
115126 function workerListener ( event ) {
116127
117128 // if response url matches
118129 if ( event . data . type === 'response' &&
119- event . data . url === url ) {
130+ event . data . url === url &&
131+ event . data . fromClient === clientId ) {
120132
121133 if ( enableDevLogs ) {
122134 console . debug ( '[ServiceWorker] Recived response data from client' , event . data ) ;
@@ -186,9 +198,34 @@ function handleFetchRequest(request, event) {
186198 if ( enableDevLogs ) {
187199 console . debug ( '[ServiceWorker] Intercepted live fetch' , request . url , request ) ;
188200 }
201+
202+
203+ let clientId = event . clientId ;
204+
205+ let [ url , parentClientId ] = request . url . split ( '?' ) ;
206+
207+ const liveFramePath = INTERNAL_PATHS . relLivePath ;
208+
209+ // if codeit client is creating a new live view
210+ if ( url . endsWith ( liveFramePath )
211+ && event . resultingClientId ) {
212+
213+ // add live view to client array
214+
215+ const liveViewClientId = event . resultingClientId ;
216+
217+ parentClientId = parentClientId . slice ( 0 , - 1 ) ;
218+ clientId = parentClientId ;
219+
220+ // pair live view client ID
221+ // with codeit client ID
222+ // in client array
223+ liveViewClients [ liveViewClientId ] = parentClientId ;
224+
225+ }
189226
190227 // return response from client
191- resolve ( sendRequestToClient ( request ) ) ;
228+ resolve ( sendRequestToClient ( request , clientId ) ) ;
192229
193230 } else if ( pathType === 'clientId' ) { // if fetching client ID
194231
0 commit comments