Search code examples
service-workerdexie

Importing Dexie in Service Worker


is it possible to import Dexie in service worker? importScripts("https://unpkg.com/dexie@latest/dist/dexie.js");

resulted in : The script resource is behind a redirect, which is disallowed. I've to import it locally to the worker by saving the code into a local file, but got a "window is undefined error" although the Dexie code seems to check for self instead of window...

Am I missing something? {typeof window !== 'undefined' ? window :global;}


Solution

  • URLs passed in to importScripts() can't result in HTTP redirections. They must return responses with the correct JavaScript MIME type, and with a ok (2xx) status code.

    Requesting https://unpkg.com/dexie@latest/dist/dexie.js results in an HTTP redirection to https://unpkg.com/[email protected]/dist/dexie.js.

    You should be able to import Dexie if you use the final, redirected URL:

    importScripts('https://unpkg.com/[email protected]/dist/dexie.js');