Skip to main content
ServiceWorkerWallet runs the wallet in a service worker. The wallet’s work moves off the page’s main thread, and every open tab shares one wallet.

Quick Setup

ServiceWorkerWallet.setup registers the worker and returns a ready wallet:

Service Worker Implementation

Create a service worker file. It runs a MessageBus, which stores the wallet’s data in IndexedDB and handles the wallet calls that ServiceWorkerWallet sends from the page. Bundle this file into a single script before you serve it. The SDK registers the service worker as a classic script, and a classic script can’t use import statements.
For example, bundle it with esbuild:
Serve the bundled file at the serviceWorkerPath you pass to ServiceWorkerWallet.setup.

Storage

The service worker wallet stores its data in IndexedDB. The page and the service worker both open the default database, so ServiceWorkerWallet.setup needs no storage option. IndexedDB works in both contexts. localStorage isn’t available in a service worker. For the other repositories, see Storage Repositories.

Renewal in the Service Worker

The service worker runs the wallet’s automatic renewal while it runs. Get its VtxoManager from the wallet:
The worker runs send, settle, and renewVtxos one at a time, so a user’s send can’t race automatic renewal for the same outputs.

Advanced: Custom Service Worker

To handle your own messages in the service worker, implement the MessageHandler interface. Then add your handler to messageHandlers, next to WalletMessageHandler.

Best Practices

Keep the default IndexedDB repositories. The browser stops an idle service worker, and in-memory repositories lose the wallet’s data when it stops.
ServiceWorkerWallet.setup and wallet calls throw ServiceWorkerTimeoutError when the service worker doesn’t answer in time:

Troubleshooting

If the service worker fails to register:
  1. Ensure the service worker file path (serviceWorkerPath) is correct and accessible
  2. Service workers require HTTPS (or localhost for dev)
  3. Check browser console for registration errors
  4. Verify that the service worker script creates a MessageBus and calls bus.start()
  5. Make sure you serve the bundled service worker file, which has no import statements
If you encounter IndexedDB errors:
  1. Check browser console for specific error messages
  2. If you pass a database name, make sure the page and the service worker use the same name
  3. Clear browser data if the database schema changed
If you experience message timeouts:
  1. The SDK pings the service worker before every operation and restarts it if it doesn’t answer, so persistent timeouts usually mean a network issue.
  2. Check that the service worker file calls bus.start()
  3. Verify network connectivity
  4. Check for errors in the service worker console
  5. If using custom error handling, check for ServiceWorkerTimeoutError, which means the service worker didn’t answer in time