Skip to content

Commit

Permalink
sync xhr when updating iframe.srcdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Sep 16, 2021
1 parent b009038 commit ac6e74f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,6 @@ export const PT_PROXY_URL = `proxytown`;
export const PT_INITIALIZED_EVENT = `ptinit`;
export const PT_SCRIPT_TYPE = `text/partytown`;

export const PT_SCRIPT = `<script src="/~partytown/partytown.debug.js" async defer></script>`;
export const PT_SCRIPT = `<script src="/~partytown/partytown${
debug ? '.debug' : ''
}.js" async defer></script>`;
29 changes: 14 additions & 15 deletions src/lib/web-worker/worker-iframe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getter, proxy, setter } from './worker-proxy';
import { ExtraInstruction, InterfaceType, NodeName, PlatformApiId } from '../types';
import { InstanceIdKey, InterfaceTypeKey, PrivateValues, WinIdKey } from './worker-constants';
import { PT_SCRIPT, PT_SCRIPT_TYPE } from '../utils';
import { nextTick, PT_SCRIPT, PT_SCRIPT_TYPE } from '../utils';
import { resolveUrl, WorkerSrcElement } from './worker-node';
import { WorkerDocument } from './worker-document';

Expand Down Expand Up @@ -31,25 +31,24 @@ export class WorkerIFrameElement extends WorkerSrcElement {
return this[PrivateValues].$url$ || '';
}
set src(url: string) {
const privateValues = this[PrivateValues];
let privateValues = this[PrivateValues];
let xhr = new XMLHttpRequest();

url = resolveUrl(url) + '';

if (privateValues.$url$ !== url) {
privateValues.$url$ = url;

fetch(url).then((rsp) => {
if (rsp.ok) {
rsp.text().then((content) => {
setter(this, ['srcdoc'], updateIframeScripts(content));
if (privateValues.$onload$) {
privateValues.$onload$.forEach((onload) => onload({ type: 'load' }));
}
});
} else if (privateValues.$onerror$) {
privateValues.$onerror$.forEach((onerror) => onerror({ type: 'error' }));
xhr.open('GET', (privateValues.$url$ = url), false);
xhr.send();

if (xhr.status > 199 && xhr.status < 300) {
setter(this, ['srcdoc'], updateIframeScripts(xhr.responseText));

if (privateValues.$onload$) {
nextTick(() => privateValues.$onload$!.forEach((onload) => onload({ type: 'load' })));
}
});
} else if (privateValues.$onerror$) {
nextTick(() => privateValues.$onerror$!.forEach((onerror) => onerror({ type: 'error' })));
}
}
}
}
Expand Down

1 comment on commit ac6e74f

@vercel
Copy link

@vercel vercel bot commented on ac6e74f Sep 16, 2021

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.