diff --git a/src/components/Search/provider/index.ts b/src/components/Search/provider/index.ts index 32e6dd1..61cead4 100644 --- a/src/components/Search/provider/index.ts +++ b/src/components/Search/provider/index.ts @@ -43,10 +43,7 @@ export class SearchProvider implements ISearchProvider { // }; private get base() { - return window.location.pathname - .split('/') - .slice(0, -(this.config.depth + 1)) - .join('/'); + return window.location.href.split('/').slice(0, -this.config.depth).join('/'); } private async request(message: object) { @@ -54,8 +51,29 @@ export class SearchProvider implements ISearchProvider { } } +const BAD_ORIGIN_ERROR = /Script at '(.*?)' cannot be accessed from origin/; + +async function loadWorker() { + try { + return new Worker(new URL('../worker/index.ts', import.meta.url)); + } catch (error) { + // @see https://stackoverflow.com/questions/21408510/chrome-cant-load-web-worker + if (error instanceof DOMException) { + const match = BAD_ORIGIN_ERROR.exec(error.message); + if (match) { + const url = match[1]; + const blob = new Blob([`importScripts('${url}');`], {type: 'text/javascript'}); + + return new Worker(URL.createObjectURL(blob)); + } + } + + throw error; + } +} + async function initWorker(config: WorkerConfig) { - const worker = new Worker(new URL('../worker/index.ts', import.meta.url)); + const worker = await loadWorker(); await request(worker, {...config, type: 'init'}); diff --git a/src/components/Search/worker/index.ts b/src/components/Search/worker/index.ts index ebaab71..bc34a59 100644 --- a/src/components/Search/worker/index.ts +++ b/src/components/Search/worker/index.ts @@ -48,7 +48,7 @@ const HANDLERS = { async init(config: InitMessage) { self.config = config; - importScripts(self.config.api); + importScripts(self.config.base + '/' + self.config.api); AssertApi(self.api);