Skip to content

Commit

Permalink
fix(sw): assign new channel port on reload (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz committed Jul 25, 2024
1 parent 55cb609 commit 2d92bea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sandpack-client/src/clients/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,18 @@ export class SandpackRuntime extends SandpackClient {
}
};

this.iframe.onload = () => {
const sendMessage = () => {
const initMsg = {
$channel: CHANNEL_NAME,
$type: "preview/init",
};

iframeContentWindow.postMessage(initMsg, "*", [channel.port2]);

this.iframe.removeEventListener("load", sendMessage);
};

this.iframe.addEventListener("load", sendMessage);
}

private handleWorkerRequest(
Expand Down Expand Up @@ -362,6 +366,10 @@ export class SandpackRuntime extends SandpackClient {
*/
if (message.type === "refresh") {
this.setLocationURLIntoIFrame();

if (this.options.experimental_enableServiceWorker) {
this.serviceWorkerHandshake();
}
}

this.iframeProtocol.dispatch(message);
Expand Down
1 change: 1 addition & 0 deletions sandpack-react/src/presets/CustomSandpack.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const ExperimentalServiceWorker: React.FC = () => {
`,
}}
options={{
bundlerURL: "https://nqfrns-3000.csb.app/",
experimental_enableServiceWorker: true,
}}
template="react"
Expand Down

0 comments on commit 2d92bea

Please sign in to comment.