Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAccessing `document.currentScript` in async chunks returns null in Firefox #10510
Comments
why do you need access to the currect script? |
Currently when building with Vue CLI's lib mode, |
So this helper need to imported before any async chunk. But why do you need |
For example, if a library built with Vue CLI () is imported in a project and is later bundled inside an async chunk, |
Bug report
What is the current behavior?
If modules in an async chunk calls
document.currentScript
, the result will benull
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:Where
async.js
callsdocument.currentScript
:After we compiled
index.js
with webpack, two files will be produce into thedist
folder:After introducing the
main.js
inindex.html
, we'll notice that in Chrome, we can see the correct scriptsrc
: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 asnull
. While in other browsers, it's evaluated to the expectedscript
element.Per the HTML spec:
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 ofdocument.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: /