Skip to content

IndexedDB integration for service workers #116

@CodingDoug

Description

@CodingDoug

Normal IndexedDB persistence support works great for me so far. However, it doesn't work at all in a service worker because the persister is only trying to find the indexedDB object on the window object as you can see here:

const request = WINDOW.indexedDB.open(dbName, create ? 2 : undefined);

Error at runtime:

TypeError: Cannot read properties of undefined (reading 'indexedDB')

window just isn't present in service workers. Instead, service workers have access to indexedDB via ServiceWorkerGlobalScope (inherits from WorkerGlobalScope) as a top-level property. In TypeScript:

declare const self: ServiceWorkerGlobalScope
const indexedDB = self.indexedDB

One imagines this entrypoint to a indexedDB object is functionally identical. It would be great if the persister looked in both window and the service worker global scope to find an indexedDB property. This would make TinyBase incredibly more useful for PWAs!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions