Skip to content

Commit 2d92bea

Browse files
authored
fix(sw): assign new channel port on reload (#1166)
1 parent 55cb609 commit 2d92bea

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

sandpack-client/src/clients/runtime/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,18 @@ export class SandpackRuntime extends SandpackClient {
195195
}
196196
};
197197

198-
this.iframe.onload = () => {
198+
const sendMessage = () => {
199199
const initMsg = {
200200
$channel: CHANNEL_NAME,
201201
$type: "preview/init",
202202
};
203203

204204
iframeContentWindow.postMessage(initMsg, "*", [channel.port2]);
205+
206+
this.iframe.removeEventListener("load", sendMessage);
205207
};
208+
209+
this.iframe.addEventListener("load", sendMessage);
206210
}
207211

208212
private handleWorkerRequest(
@@ -362,6 +366,10 @@ export class SandpackRuntime extends SandpackClient {
362366
*/
363367
if (message.type === "refresh") {
364368
this.setLocationURLIntoIFrame();
369+
370+
if (this.options.experimental_enableServiceWorker) {
371+
this.serviceWorkerHandshake();
372+
}
365373
}
366374

367375
this.iframeProtocol.dispatch(message);

sandpack-react/src/presets/CustomSandpack.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const ExperimentalServiceWorker: React.FC = () => {
5555
`,
5656
}}
5757
options={{
58+
bundlerURL: "https://nqfrns-3000.csb.app/",
5859
experimental_enableServiceWorker: true,
5960
}}
6061
template="react"

0 commit comments

Comments
 (0)