Skip to content

Commit

Permalink
fix: do not reset iframe location when loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jan 25, 2022
1 parent 67b1cd4 commit f516675
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
18 changes: 10 additions & 8 deletions src/lib/sandbox/main-register-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ export const registerWindow = (

const doc = $window$.document;
const history = $window$.history;

const envData: InitializeEnvironmentData = {
$winId$,
$parentWinId$: windowIds.get($window$.parent)!,
$url$: doc.baseURI,
};
const $parentWinId$ = windowIds.get($window$.parent)!;

const sendInitEnvData = () =>
worker.postMessage([WorkerMessageType.InitializeEnvironment, envData]);
worker.postMessage([
WorkerMessageType.InitializeEnvironment,
{
$winId$,
$parentWinId$,
$url$: doc.baseURI,
},
]);

const pushState = history.pushState.bind(history);
const replaceState = history.replaceState.bind(history);
Expand Down Expand Up @@ -59,7 +61,7 @@ export const registerWindow = (
}

if (debug) {
const winType = envData.$winId$ === envData.$parentWinId$ ? 'top' : 'iframe';
const winType = $winId$ === $parentWinId$ ? 'top' : 'iframe';
logMain(`Registered ${winType} window ${normalizedWinId($winId$)} (${$winId$})`);
}

Expand Down
6 changes: 2 additions & 4 deletions src/lib/web-worker/worker-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ export const createEnvironment = (
{ $winId$, $parentWinId$, $url$ }: InitializeEnvironmentData,
isIframeWindow?: boolean
) => {
if (environments[$winId$]) {
// this environment (iframe) is already initialized
environments[$winId$].$location$.href = $url$;
} else {
if (!environments[$winId$]) {
// create a simulated global environment for this window
// if it hasn't already been created (like an iframe)
new Window($winId$, $parentWinId$, $url$, isIframeWindow);

if (debug) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/web-worker/worker-exec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debug, len } from '../utils';
import { debug } from '../utils';
import {
EventHandler,
InitializeScriptData,
Expand All @@ -7,7 +7,7 @@ import {
WebWorkerEnvironment,
WorkerMessageType,
} from '../types';
import { environments, partytownLibUrl, postMessages, webWorkerCtx } from './worker-constants';
import { environments, partytownLibUrl, webWorkerCtx } from './worker-constants';
import { getInstanceStateValue, setInstanceStateValue } from './worker-state';
import { getOrCreateNodeInstance } from './worker-constructors';
import { logWorker } from '../log';
Expand Down
8 changes: 4 additions & 4 deletions src/lib/web-worker/worker-iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ export const HTMLIFrameDescriptorMap: PropertyDescriptorMap & ThisType<Node> = {
}
return src;
},
set(url: string) {
set(src: string) {
let xhr = new XMLHttpRequest();
let xhrStatus: number;
let winId = this[InstanceIdKey];
let env = environments[winId];

env.$location$.href = url = resolveUrl(getEnv(this), url);
env.$location$.href = src = resolveUrl(getEnv(this), src);
env.$isLoading$ = 1;

setInstanceStateValue(this, StateProp.loadErrorStatus, undefined);

xhr.open('GET', url, false);
xhr.open('GET', src, false);
xhr.send();
xhrStatus = xhr.status;

if (xhrStatus > 199 && xhrStatus < 300) {
setter(
this,
['srcdoc'],
`<base href="${url}">` +
`<base href="${src}">` +
xhr.responseText
.replace(/<script>/g, `<script type="${SCRIPT_TYPE}">`)
.replace(/<script /g, `<script type="${SCRIPT_TYPE}" `)
Expand Down
6 changes: 1 addition & 5 deletions tests/platform/iframe/location2.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
</head>
<body>
<div id="output"></div>
<script type="text/javascript">
document.getElementById('output').textContent = location.pathname;
parent.document.getElementById('testLocation').textContent = location.pathname;
parent.document.getElementById('testLocation').className = 'testLocation';
</script>
<script src="location2.js"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions tests/platform/iframe/location2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
document.getElementById('output').textContent = location.pathname;
parent.document.getElementById('testLocation').textContent = location.pathname;
parent.document.getElementById('testLocation').className = 'testLocation';

1 comment on commit f516675

@vercel
Copy link

@vercel vercel bot commented on f516675 Jan 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.