question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing `document.currentScript` in async chunks returns null in Firefox

See original GitHub issue

Bug report

What is the current behavior?

If modules in an async chunk calls document.currentScript, the result will be null in Firefox while other browsers returns the expected <script> element.

If the current behavior is a bug, please provide the steps to reproduce.

The details of the issue is at this repo.

What is the expected behavior?

Developers should have a way to workaround Firefox’s potential bug. eg. webpack provide a __webpack_current_script__ inside each async chunk.

Details

The entry file is index.js which contains only:

import("./async")

Where async.js calls document.currentScript:

console.log(document.currentScript);

document.getElementById("output").textContent = document.currentScript
  ? document.currentScript.src
  : "null";

After we compiled index.js with webpack, two files will be produce into the dist folder:

dist/
  1.js
  main.js

After introducing the main.js in index.html, we’ll notice that in Chrome, we can see the correct script src:

$PROJECT_PATH/dist/1.js

While in Firefox we got null.

After I did a little investigation, the only difference I found is that Firefox is treating scripts run inside a micro task the same way as those in timeout callbacks, making document.currentScript being evaluated as null. While in other browsers, it’s evaluated to the expected script element.

Per the HTML spec:

(document.currentScript) Returns null if the Document is not currently executing a script or SVG script element (e.g., because the running script is an event handler, or a timeout), or if the currently executing script or SVG script element represents a module script.

I’d say it’s a little ambiguous about whether for micro tasks it should return null. Plus this section is labelled as “non-normative”. But it’s indeed unexpected that browsers handle this differently. I’ve filed an issue on Bugzilla here.

Though it’s a potential bug on the Firefox side, but I think we have a chance to fix this on the webpack side.

When webpack generates an async chunk, we can evaluate document.currentScript synchronously upfront, and save it to a private variable like __webpack_current_script__ so that if developers have a chance to work this around by try accessing __webpack_current_script__ instead of document.currentScript.

Thanks for your time!

Other relevant information: webpack version: 4.42.0 Node.js version: 12.13.1 Operating System: macOS 10.15.1 Additional tools: /

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
sokracommented, Mar 6, 2020

why do you need access to the currect script?

1reaction
Justineocommented, Feb 8, 2021

For example, if a library built with Vue CLI is imported in a project and is later bundled inside an async chunk, document.currentScript will be evaluated inside an async chunk.

Read more comments on GitHub >

github_iconTop Results From Across the Web

1620505 - document.currentScript returns null in microtasks
Per HTML spec: (document.currentScript) Returns null if the Document is not currently executing a script or SVG script element (e.g., because the running...
Read more >
How get a currentScript attributes? - Stack Overflow
*I am using this code to get the currentScript but this is not working because the script is async. var target = document.currentScript...
Read more >
HTML Standard
Otherwise, the default element namespace URI is null. ... When the Document is created, the currentScript must be initialized to null.
Read more >
Safari Technology Preview Release Notes - Apple Developer
Changed to keep MediaKeySystemAccess alive in a createMediaKeys() async task ... formData() to not reject the promise if the body is null and...
Read more >
Converse.js API Documentation Source: headless/dist ...
Web IDL requires DOMStrings to just be converted using ECMAScript // ToString, ... if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) { return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found