diff --git a/packages/trace-viewer/src/sw/traceModelBackends.ts b/packages/trace-viewer/src/sw/traceModelBackends.ts index 6ed16f037f..dfff79d159 100644 --- a/packages/trace-viewer/src/sw/traceModelBackends.ts +++ b/packages/trace-viewer/src/sw/traceModelBackends.ts @@ -23,9 +23,6 @@ const zipjs = zipImport as typeof zip; type Progress = (done: number, total: number) => undefined; -const searchParams = new URL(self.location.href).searchParams; -const testServerPort = searchParams.get('testServerPort'); - export class ZipTraceModelBackend implements TraceModelBackend { private _zipReader: zip.ZipReader; private _entriesPromise: Promise>; @@ -33,16 +30,8 @@ export class ZipTraceModelBackend implements TraceModelBackend { constructor(traceURL: string, progress: Progress) { this._traceURL = traceURL; - - const baseURL = new URL(self.location.href); - if (testServerPort) { - baseURL.pathname = '/trace/'; - baseURL.port = testServerPort; - } - - const url = new URL(formatUrl(traceURL), baseURL); this._zipReader = new zipjs.ZipReader( - new zipjs.HttpReader(url, { mode: 'cors', preventHeadRequest: true } as any), + new zipjs.HttpReader(formatUrl(traceURL), { mode: 'cors', preventHeadRequest: true } as any), { useWebWorkers: false }); this._entriesPromise = this._zipReader.getEntries({ onprogress: progress }).then(entries => { const map = new Map(); @@ -97,7 +86,7 @@ export class FetchTraceModelBackend implements TraceModelBackend { constructor(traceURL: string) { this._traceURL = traceURL; - this._entriesPromise = this._fetchFile(traceURL).then(async response => { + this._entriesPromise = fetch(formatUrl(traceURL)).then(async response => { const json = JSON.parse(await response.text()); const entries = new Map(); for (const entry of json.entries) @@ -140,22 +129,21 @@ export class FetchTraceModelBackend implements TraceModelBackend { if (!fileName) return; - return this._fetchFile(fileName); - } - - private async _fetchFile(path: string) { - const url = new URL('/trace/file', self.location.href); - url.searchParams.set('path', path); - if (testServerPort) - url.port = testServerPort; - return fetch(url); + return fetch(formatUrl(fileName)); } } +const baseURL = new URL(self.location.href); +baseURL.port = baseURL.searchParams.get('testServerPort') ?? baseURL.port; + function formatUrl(trace: string) { - let url = trace.startsWith('http') || trace.startsWith('blob') ? trace : `file?path=${encodeURIComponent(trace)}`; - // Dropbox does not support cors. - if (url.startsWith('https://www.dropbox.com/')) - url = 'https://dl.dropboxusercontent.com/' + url.substring('https://www.dropbox.com/'.length); + if (trace.startsWith('https://www.dropbox.com/')) + return 'https://dl.dropboxusercontent.com/' + trace.substring('https://www.dropbox.com/'.length); + + if (trace.startsWith('http') || trace.startsWith('blob')) + return trace; + + const url = new URL('/trace/file', baseURL); + url.searchParams.set('path', trace); return url; }