Skip to content

Improve the error message when trying to use OPFS outside of a worker #201

Description

@TheAndrewJackson

Description

I'm running into an issue when trying to create a new database using the OPFS AHP file system. This snippet

import { PGlite } from "@electric-sql/pglite";

const client = await PGlite.create({
  dataDir: "opfs-ahp://electric.db",
  relaxedDurability: true,
});

is producing this error

image

I have a minimal-repro repo here that has the issue: https://github.com/TheAndrewJackson/opfs-ahp_bug_repro

Activity

  1. AntonOfTheWoods commented on Aug 15, 2024

    @AntonOfTheWoods
    Contributor

    https://pglite.dev/examples/opfs

    It needs to be somewhere that allows the sync api, so in a web/service (shared?) worker.

  2. samwillis commented on Aug 15, 2024

    @samwillis
    Collaborator

    Thanks for the report @TheAndrewJackson

    The OPFS AHP VFS only works in a dedicated web worker as it requires the sync access handle API which is not available on the main thread, in shared workers, or in service workers.

    We should improve the error message on this.

  3. changed the title [-]Issue with creating database using the OPFS AHP file system[/-] [+]Improve the error message when trying to use OPFS outside of a worker[/+] on Aug 15, 2024
  4. TheAndrewJackson commented on Aug 15, 2024

    @TheAndrewJackson
    Author

    ahh, that makes sense @samwillis. Thanks for the clarification!

  5. psh4607 commented on Aug 12, 2026

    @psh4607
    Contributor

    I opened draft PR #1079 to address this issue. It now detects when the synchronous access handle API is unavailable and reports a clear error explaining that OPFS-AHP requires a Dedicated Web Worker. The PR also includes regression tests and updates the documentation.
    Feedback is welcome: #1079

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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