diff --git a/rollup.config.js b/rollup.config.js index b9c96206..6ba5f73b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,5 @@ import typescript from '@rollup/plugin-typescript'; import { basename, join } from 'path'; -import { createHash } from 'crypto'; import { copySync, emptyDir, @@ -28,7 +27,6 @@ export default async function (cmdArgs) { const reactBuildDir = join(rootDir, 'react'); const cacheDir = join(rootDir, '.cache'); const cache = {}; - let sandboxHash = ''; const minOpts = { compress: { @@ -184,11 +182,6 @@ export default async function (cmdArgs) { const sandboxCode = isDev ? '' : await getSandbox(false); - if (!isDev) { - sandboxHash = createHash('sha1').update(sandboxCode).digest('hex'); - sandboxHash = sandboxHash.substr(0, 6).toLowerCase(); - } - return { input: join(srcLibDir, 'service-worker', 'index.ts'), output, @@ -219,11 +212,6 @@ export default async function (cmdArgs) { if (id === '@sandbox') { return `const Sandbox = ${JSON.stringify(sandboxCode)}; export default Sandbox;`; } - if (id === '@sandbox-hash') { - return `const SandboxHash = ${JSON.stringify( - sandboxHash - )}; export default SandboxHash;`; - } if (id === '@sandbox-debug') { return `const SandboxDebug = ${JSON.stringify( await getSandbox(true) @@ -267,16 +255,6 @@ export default async function (cmdArgs) { outputToFilesystem: false, }), { - resolveId(id) { - if (id.startsWith('@')) return id; - }, - async load(id) { - if (id === '@sandbox-hash') { - return `const SandboxHash = ${JSON.stringify( - sandboxHash - )}; export default SandboxHash;`; - } - }, writeBundle() { copySync(buildDir, testsBuildDir); }, @@ -350,7 +328,7 @@ export default async function (cmdArgs) { } return { - input: join(srcReactDir, 'index.tsx'), + input: join(srcReactDir, 'index.ts'), output: [ { file: join(reactBuildDir, 'index.cjs'), diff --git a/src/lib/main/index.ts b/src/lib/main/index.ts index 649e4794..c9b5f7b2 100644 --- a/src/lib/main/index.ts +++ b/src/lib/main/index.ts @@ -1,6 +1,5 @@ import { debug, PT_INITIALIZED_EVENT, PT_SCRIPT_TYPE } from '../utils'; import type { MainWindow } from '../types'; -import SandboxHash from '@sandbox-hash'; (function ( doc: Document, @@ -13,10 +12,10 @@ import SandboxHash from '@sandbox-hash'; function ready() { if (!sandbox) { sandbox = doc.createElement('iframe'); + sandbox.dataset.partytown = 'sandbox'; sandbox.setAttribute('style', 'display:block;width:0;height:0;border:0;visibility:hidden'); sandbox.setAttribute('aria-hidden', 'true'); - sandbox.src = - scope + 'partytown-sandbox' + (debug ? '.debug' : '-' + SandboxHash) + '?' + Date.now(); + sandbox.src = scope + 'partytown-sandbox' + (debug ? '.debug?' : '?') + Date.now(); doc.body.appendChild(sandbox); } } diff --git a/src/lib/modules.d.ts b/src/lib/modules.d.ts index 6bc7af0f..b07de09d 100644 --- a/src/lib/modules.d.ts +++ b/src/lib/modules.d.ts @@ -3,11 +3,6 @@ declare module '@sandbox' { export default str; } -declare module '@sandbox-hash' { - const str: string; - export default str; -} - declare module '@sandbox-debug' { const str: string; export default str; diff --git a/src/lib/service-worker/fetch.ts b/src/lib/service-worker/fetch.ts index d0a9f02a..827be81e 100644 --- a/src/lib/service-worker/fetch.ts +++ b/src/lib/service-worker/fetch.ts @@ -1,7 +1,7 @@ -import { httpRequestFromWebWorker } from './sw-message'; +import { CacheControl, ContentType, response } from './response'; import { debug, PT_PROXY_URL } from '../utils'; +import { httpRequestFromWebWorker } from './sw-message'; import Sandbox from '@sandbox'; -import SandboxHash from '@sandbox-hash'; import SandboxDebug from '@sandbox-debug'; export const onFetchServiceWorkerRequest = (self: ServiceWorkerGlobalScope, ev: FetchEvent) => { @@ -9,21 +9,13 @@ export const onFetchServiceWorkerRequest = (self: ServiceWorkerGlobalScope, ev: const pathname = new URL(req.url).pathname; if (debug && pathname.endsWith('partytown-sandbox.debug')) { - ev.respondWith( - new Response(SandboxDebug, { - headers: { 'content-type': 'text/html', 'Cache-Control': 'no-store' }, - }) - ); - } else if (!debug && pathname.endsWith('partytown-sandbox-' + SandboxHash)) { - ev.respondWith( - new Response(Sandbox, { - headers: { - 'content-type': 'text/html', - 'Cache-Control': 'max-age=31556952, immutable', - }, - }) - ); + // debug version (sandbox and web worker are not inlined) + ev.respondWith(response(SandboxDebug, ContentType.HTML, CacheControl.NoStore)); + } else if (!debug && pathname.endsWith('partytown-sandbox')) { + // sandbox and webworker, minified and inlined + ev.respondWith(response(Sandbox, ContentType.HTML, CacheControl.NoStore)); } else if (pathname.endsWith(PT_PROXY_URL)) { + // proxy request ev.respondWith(httpRequestFromWebWorker(self, req)); } }; diff --git a/src/lib/service-worker/response.ts b/src/lib/service-worker/response.ts new file mode 100644 index 00000000..610d1ce2 --- /dev/null +++ b/src/lib/service-worker/response.ts @@ -0,0 +1,18 @@ +export const response = (body: string, contentType: ContentType, cacheControl: CacheControl) => + new Response(body, { + headers: { + 'content-type': contentType === ContentType.JSON ? 'application/json' : 'text/html', + 'Cache-Control': + cacheControl === CacheControl.Immutable ? 'max-age=31556952,immutable' : 'no-store', + }, + }); + +export const enum ContentType { + HTML, + JSON, +} + +export const enum CacheControl { + Immutable, + NoStore, +} diff --git a/src/lib/service-worker/sw-message.ts b/src/lib/service-worker/sw-message.ts index beec4843..239ba77c 100644 --- a/src/lib/service-worker/sw-message.ts +++ b/src/lib/service-worker/sw-message.ts @@ -1,5 +1,6 @@ import type { MainAccessRequest, MainAccessResponse } from '../types'; import { debug } from '../utils'; +import { CacheControl, ContentType, response } from './response'; const resolves = new Map(); @@ -57,11 +58,7 @@ export const httpRequestFromWebWorker = (self: ServiceWorkerGlobalScope, req: Re const accessReq: MainAccessRequest = await req.clone().json(); const responseData = await sendMessageToSandboxFromServiceWorker(self, accessReq); - resolve( - new Response(JSON.stringify(responseData), { - headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' }, - }) - ); + resolve(response(JSON.stringify(responseData), ContentType.JSON, CacheControl.Immutable)); }); type MessageResolve = [(data?: any) => void, any];