I'm facing a design challenge regarding how to reuse libraries by a service worker in a Next.js progressive web app (PWA)
The folder structure of my PWA looks like this:

About the structure:
- Within the
/publicfolder there's a service worker calledsw.js - The
/src/libfolder contains aIDBManager.jsfor IndexedDB opertions - The
/src/apifolder contains aIDB.jsas an API endpoint to execute IndexedDB operations. TheIDB.jsimports theIDBManager.js.
The situation is that the sw.js service worker can just access files in its scope that means in the same directory and subfolders. In order to execute IndexedDB opertions by the service worker I would have to duplicate code of IDBManager.js to a file in the public folder.
To prevent duplicate code my idea is to provide the service worker the API endpoint (IDB.js) which can be reached by the service worker via a fetch() call.
The service worker's fetch() call works but IDB.js gives an error when calling openDB() to open the IndexedDB. The error message is:

This error doesn't appear when openDB() is not called by the service worker. Does anyone have an idea what the reason for this error is or how to design a PWA with code reuse by the service worker?