Skip to content

Commit

Permalink
fix: remove isolated from service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jan 20, 2022
1 parent ccd77bf commit 47a264b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 71 deletions.
103 changes: 49 additions & 54 deletions src/lib/main/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,67 @@ export function snippet(
doc: Document,
nav: Navigator,
top: Window,
crossOriginIsolated: boolean,
useAtomics: boolean,
config?: PartytownConfig,
libPath?: string,
timeout?: any,
scripts?: NodeListOf<HTMLScriptElement>,
sandbox?: HTMLIFrameElement | HTMLScriptElement,
mainForwardFn?: any,
useAtomics?: boolean
isReady?: number
) {
// ES5 just so IE11 doesn't choke on arrow fns
function ready() {
if (debug) {
// default to use debug files
libPath = (config!.lib || '/~partytown/') + (config!.debug !== false ? 'debug/' : '');
} else {
// default to use production, non-debug files
libPath = (config!.lib || '/~partytown/') + (config!.debug ? 'debug/' : '');
}
if (!isReady) {
isReady = 1;
if (debug) {
// default to use debug files
libPath = (config!.lib || '/~partytown/') + (config!.debug !== false ? 'debug/' : '');
} else {
// default to use production, non-debug files
libPath = (config!.lib || '/~partytown/') + (config!.debug ? 'debug/' : '');
}

// grab all the partytown scripts
scripts = doc.querySelectorAll(`script[type="${SCRIPT_TYPE}"]`);
// grab all the partytown scripts
scripts = doc.querySelectorAll(`script[type="${SCRIPT_TYPE}"]`);

if (top != win) {
// this is an iframe
top!.dispatchEvent(new CustomEvent(PT_IFRAME_APPENDED, { detail: win }));
} else if (scripts!.length) {
// set a timeout to fire if PT hasn't initialized in Xms
timeout = setTimeout(fallback, debug ? 60000 : 10000);
doc.addEventListener(PT_INITIALIZED_EVENT, clearFallback);
if (top != win) {
// this is an iframe
top!.dispatchEvent(new CustomEvent(PT_IFRAME_APPENDED, { detail: win }));
} else if (scripts!.length) {
// set a timeout to fire if PT hasn't initialized in Xms
timeout = setTimeout(fallback, debug ? 60000 : 10000);
doc.addEventListener(PT_INITIALIZED_EVENT, clearFallback);

useAtomics = crossOriginIsolated;
if (debug && useAtomics) {
useAtomics = !win.location.search.includes('forceServiceWorker');
}
if (useAtomics) {
// atomics support
loadSandbox(1);
} else if (nav.serviceWorker) {
// service worker support
nav.serviceWorker
.register(libPath + 'partytown-sw.js' + (crossOriginIsolated ? '?isolated' : ''), {
scope: libPath,
})
.then(function (swRegistration) {
if (swRegistration.active) {
loadSandbox();
} else if (swRegistration.installing) {
swRegistration.installing.addEventListener('statechange', function (ev) {
if ((ev.target as any as ServiceWorker).state == 'activated') {
loadSandbox();
}
});
} else if (debug) {
console.warn(swRegistration);
}
}, console.error);
} else {
// no support for atomics or service worker
fallback();
if (debug && useAtomics) {
useAtomics = !win.location.search.includes('forceServiceWorker');
}
if (useAtomics) {
// atomics support
loadSandbox(1);
} else if (nav.serviceWorker) {
// service worker support
nav.serviceWorker
.register(libPath + 'partytown-sw.js' + (debug && useAtomics ? '?isolated' : ''), {
scope: libPath,
})
.then(function (swRegistration) {
if (swRegistration.active) {
loadSandbox();
} else if (swRegistration.installing) {
swRegistration.installing.addEventListener('statechange', function (ev) {
if ((ev.target as any as ServiceWorker).state == 'activated') {
loadSandbox();
}
});
} else if (debug) {
console.warn(swRegistration);
}
}, console.error);
} else {
// no support for atomics or service worker
fallback();
}
}
}
}
Expand Down Expand Up @@ -123,15 +125,8 @@ export function snippet(
}

if (doc.readyState == 'complete') {
// document is ready, kick it off
ready();
} else {
// not ready yet, wait for window load
win.addEventListener('load', ready);
}
}

const enum SandboxType {
Atomics = 1,
ServiceWorker = 2,
}
29 changes: 12 additions & 17 deletions src/lib/service-worker/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ export const onFetchServiceWorkerRequest = (ev: FetchEvent) => {
const req = ev.request;
const url = new URL(req.url);
const pathname = url.pathname;
const isolated = false;

if (debug && pathname.endsWith('sw.html')) {
// debug version (sandbox and web worker are not inlined)
ev.respondWith(response(SandboxDebug, isolated));
ev.respondWith(response(SandboxDebug));
} else if (!debug && pathname.endsWith('sw.html')) {
// sandbox and webworker, minified and inlined
ev.respondWith(response(Sandbox, isolated));
ev.respondWith(response(Sandbox));
} else if (pathname.endsWith('proxytown')) {
// proxy request
ev.respondWith(httpRequestFromWebWorker(req, isolated));
ev.respondWith(httpRequestFromWebWorker(req));
}
};

Expand Down Expand Up @@ -65,22 +65,17 @@ const swMessageError = (accessReq: MainAccessRequest, $error$: string): MainAcce

type MessageResolve = [(data?: any) => void, any];

const httpRequestFromWebWorker = (req: Request, isolated: boolean) =>
const httpRequestFromWebWorker = (req: Request) =>
new Promise<Response>(async (resolve) => {
const accessReq: MainAccessRequest = await req.clone().json();
const responseData = await sendMessageToSandboxFromServiceWorker(accessReq);
resolve(response(JSON.stringify(responseData), isolated, 'application/json'));
resolve(response(JSON.stringify(responseData), 'application/json'));
});

const response = (body: string, isolated: boolean, contentType?: string) => {
const headers: HeadersInit = {
'content-type': contentType || 'text/html',
'Cache-Control': 'no-store',
};
if (isolated) {
headers['Cross-Origin-Embedder-Policy'] = 'require-corp';
}
return new Response(body, {
headers,
const response = (body: string, contentType?: string) =>
new Response(body, {
headers: {
'content-type': contentType || 'text/html',
'Cache-Control': 'no-store',
},
});
};

1 comment on commit 47a264b

@vercel
Copy link

@vercel vercel bot commented on 47a264b Jan 20, 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.