Skip to content

ReferenceError: ShadowRoot is not defined when auto-polyfill runs without global ShadowRoot #451

Description

@steve02081504

Summary

Importing @oddbird/css-anchor-positioning (e.g. via esm.sh) auto-invokes polyfill() from src/index.ts. In environments that have window/document but do not expose a global ShadowRoot (incomplete DOM shims, some test harnesses), that call rejects with:

ReferenceError: ShadowRoot is not defined
    if (root instanceof ShadowRoot) {

at src/dom.ts (querySelectorAllRoots).

Why this is hard to catch for consumers

  1. index.ts calls polyfill() without awaiting or .catch()ing the returned promise, so the failure surfaces as an unhandled rejection, not as a failed import().
  2. Wrapping the dynamic import in .catch() therefore does not protect callers — the module evaluates successfully, then the async polyfill blows up later.

Repro sketch

// Any host with document but no global ShadowRoot
globalThis.document = /* minimal document */;
// globalThis.ShadowRoot intentionally unset

await import('@oddbird/css-anchor-positioning');
// → unhandled rejection: ReferenceError: ShadowRoot is not defined

Seen with @oddbird/css-anchor-positioning@0.10.1 under Deno + a partial happy-dom global install (document present, ShadowRoot not copied onto globalThis).

Suggested fixes

  1. Guard the check, e.g. typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot.
  2. Catch / await the auto-polyfill() promise in index.ts so a missing DOM feature does not become an unhandled rejection on mere import.
  3. Optionally skip auto-apply when required DOM APIs (ShadowRoot, etc.) are absent.

Happy to test a PR if useful.

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