Skip to content

Exception due to trying to access document from a worker #7222

Closed
@cobarx

Description

@cobarx

We are seeing an exception coming from shell.js because the worker accesses document.currentScript.

From src/shell.js#231

if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
  if (ENVIRONMENT_IS_WEB) {
    if (document.currentScript) {
      scriptDirectory = document.currentScript.src;
    }
  } else { // worker
    scriptDirectory = self.location.href;
  }

I've tested and at least in our environment, the worker has ENVIRONMENT_IS_WEB and ENVIRONMENT_IS_WORKER true. If this is always the case, the else clause is a no-op. I'm not completely familiar with this code but would suggest this becomes:

if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
  if (ENVIRONMENT_IS_WORKER) {
    scriptDirectory = self.location.href;
  } else if (document.currentScript) { // web
    scriptDirectory = document.currentScript.src;
  }

The other approach would be:

if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
  if (ENVIRONMENT_IS_WEB) {
    if (typeof document !== 'undefined' && document.currentScript) {
      scriptDirectory = document.currentScript.src;
    }
  } else { // worker
    scriptDirectory = self.location.href;
  }

The code was introduced in #6894, maybe @kripken can provide some insight on the right approach. Let me know if you'd like me to open a PR.

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