Skip to content

Commit

Permalink
fix(client-navigation): make sure iframe is using the right location …
Browse files Browse the repository at this point in the history
…on refreshing (#587)
  • Loading branch information
danilowoz committed Sep 28, 2022
1 parent 041131c commit 317d456
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"packages": ["sandpack-client", "sandpack-react"],
"sandboxes": ["sowx8r", "c3uup0"],
"sandboxes": ["sowx8r", "p9l5gn"],
"node": "14"
}
24 changes: 19 additions & 5 deletions sandpack-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ export class SandpackClient {
);
}

const urlSource = options.startRoute
? new URL(options.startRoute, this.bundlerURL).toString()
: this.bundlerURL;

this.iframe.contentWindow?.location.replace(urlSource);
this.setLocationURLIntoIFrame();

this.iframeProtocol = new IFrameProtocol(this.iframe, this.bundlerURL);

Expand Down Expand Up @@ -230,6 +226,14 @@ export class SandpackClient {
);
}

public setLocationURLIntoIFrame(): void {
const urlSource = this.options.startRoute
? new URL(this.options.startRoute, this.bundlerURL).toString()
: this.bundlerURL;

this.iframe.contentWindow?.location.replace(urlSource);
}

cleanup(): void {
this.unsubscribeChannelListener();
this.unsubscribeGlobalListener();
Expand Down Expand Up @@ -318,6 +322,16 @@ export class SandpackClient {
}

public dispatch(message: SandpackMessage): void {
/**
* Intercept "refresh" dispatch: this will make sure
* that the iframe is still in the location it's supposed to be.
* External links inside the iframe will change the location and
* prevent the user from navigating back.
*/
if (message.type === "refresh") {
this.setLocationURLIntoIFrame();
}

this.iframeProtocol.dispatch(message);
}

Expand Down

0 comments on commit 317d456

Please sign in to comment.