Skip to content

Commit 98ae7b9

Browse files
committed
Update client-channel.js
1 parent f20a0d4 commit 98ae7b9

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

worker/client-channel.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
// update worker name when updating worker
7-
const WORKER_NAME = 'codeit-worker-v501';
7+
const WORKER_NAME = 'codeit-worker-v502';
88

99

1010
// internal paths
@@ -22,6 +22,11 @@ const INTERNAL_PATHS = {
2222
}
2323

2424

25+
// key : value
26+
// live view client ID: codeit client ID
27+
let liveViewClients = {};
28+
29+
2530
// get path type
2631
function getPathType(path) {
2732

@@ -72,51 +77,56 @@ function createResponse(data, type, status) {
7277

7378

7479
// send fetch request to client
75-
function sendRequestToClient(request) {
80+
function sendRequestToClient(request, clientId) {
7681

7782
return new Promise((resolve, reject) => {
7883

84+
// get client ID
85+
const clientId = liveViewClients[clientId] ?? clientId;
86+
87+
let url = request.url;
88+
89+
// append .html to url if navigating
90+
if (request.mode === 'navigate'
91+
&& !url.endsWith('.html')
92+
&& !url.endsWith('/')) url += '.html';
93+
94+
// send request to client
95+
workerChannel.postMessage({
96+
url: url,
97+
toClient: clientId,
98+
type: 'request'
99+
});
100+
101+
79102
// set MIME type depending on request mode
80103
let mimeType = 'application/octet-stream';
81104

82105
if (request.mode === 'navigate'
83-
|| request.url.endsWith('.html')) mimeType = 'text/html';
106+
|| url.endsWith('.html')) mimeType = 'text/html';
84107

85108
if (request.mode === 'script'
86-
|| request.url.endsWith('.js')) mimeType = 'text/javascript';
109+
|| url.endsWith('.js')) mimeType = 'text/javascript';
87110

88111
if (request.mode === 'style'
89-
|| request.url.endsWith('.css')) mimeType = 'text/css';
112+
|| url.endsWith('.css')) mimeType = 'text/css';
113+
114+
if (url.endsWith('.wasm')) mimeType = 'application/wasm';
90115

91-
if (request.url.endsWith('.wasm')) mimeType = 'application/wasm';
92116

93117
if (enableDevLogs) {
94118
console.warn(mimeType, request.mode, request.url);
95119
}
96120

97121

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-
113122
// add worker/client channel listener
114123

115124
function workerListener(event) {
116125

117126
// if response url matches
118127
if (event.data.type === 'response' &&
119-
event.data.url === url) {
128+
event.data.url === url &&
129+
event.data.fromClient === clientId) {
120130

121131
if (enableDevLogs) {
122132
console.debug('[ServiceWorker] Recived response data from client', event.data);
@@ -186,9 +196,27 @@ function handleFetchRequest(request, event) {
186196
if (enableDevLogs) {
187197
console.debug('[ServiceWorker] Intercepted live fetch', request.url, request);
188198
}
199+
200+
201+
const clientId = event.clientId;
202+
203+
// if codeit client is creating a new live view
204+
if (clientId && event.resultingClientId) {
205+
206+
// add live view to client array
207+
208+
const parentClientId = event.clientId;
209+
const liveViewClientId = event.resultingClientId;
210+
211+
// pair live view client ID
212+
// with codeit client ID
213+
// in client array
214+
liveViewClients[liveViewClientId] = parentClientId;
215+
216+
}
189217

190218
// return response from client
191-
resolve(sendRequestToClient(request));
219+
resolve(sendRequestToClient(request, clientId));
192220

193221
} else if (pathType === 'clientId') { // if fetching client ID
194222

0 commit comments

Comments
 (0)