How to access src folder libraries by service worker in Next.js PWA

Viewed 266

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:
enter image description here

About the structure:

  • Within the /public folder there's a service worker called sw.js
  • The /src/lib folder contains a IDBManager.js for IndexedDB opertions
  • The /src/api folder contains a IDB.js as an API endpoint to execute IndexedDB operations. The IDB.js imports the IDBManager.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:
enter image description here

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?

0 Answers
Related