Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: to add a noblock type to script tags #4529

Closed
ahmedHusseinF opened this issue Apr 12, 2019 · 5 comments
Closed

Proposal: to add a noblock type to script tags #4529

ahmedHusseinF opened this issue Apr 12, 2019 · 5 comments

Comments

@ahmedHusseinF
Copy link

Currently we have 3 types of including scripts in an html page:

  • Block HTML parsing by fetching and executing with just a plain script tag
  • Fetch the script in parallel to parsing HTML but block once downloaded to start execution (async)
  • Fetch the script in parallel to parsing HTML and start execution after the HTML finishes parsing (defer)

But sometimes I have huge JavaScript files that has nothing to do with the DOM and it is either blocking HTML parsing or waiting.

This proposal propose adding an option to download and parse and execute JavaScript in parallel to HTML parsing by adding a specific option on the script tag and this can let me mangle with just JavaScript code as long as my script doesn't touch the DOM.

This is my first time making a big open source contribution, so I am open to any critique or directing me in the correct direction if this doesn't look good.

@domenic
Copy link
Member

domenic commented Apr 12, 2019

This use case makes sense, but we already have a mechanism to support this: web workers. In other words, instead of

<script type=noblock src="foo.js"></script>

you'd do

<script>new Worker("foo.js");</script>

In my opinion the latter (existing) solution is clearer and more explicit. The noblock version is a bit weird, because it's strange to include a script via a script tag and have that script not operate in the same JS realm as the document.

@ahmedHusseinF
Copy link
Author

Thanks for replying quickly.

Actually I can see the workers covering at least half of the use cases but what I meant was including this big JS file and for example attaching it to window within the same page event loop.

<script type=noblock src="foo.js" onload="fooLoaded(event)"></script>
<script>
  function fooLoaded(loadEv){
    window.foo.whatever();
  }
</script>

I get this can be implemented with dynamic import and window.onload but this will have the edge as the script has fully been parsed and executed in parallel to HTML parsing in particular if I am relying on this to get my page to first render.

For example the runtime of angular abstracted without DOM access which can get quite big.

@domenic
Copy link
Member

domenic commented Apr 12, 2019

We can't allow access to window without allowing access to the DOM, because the window is a critical part of the DOM. That's why workers have their own separate global object.

@rniwa
Copy link

rniwa commented Apr 13, 2019

Yeah, we can't do this. You're either in a worker or in the main thread (same thread as the rest of the DOM). DOM isn't thread safe / threadable.

@annevk
Copy link
Member

annevk commented Apr 13, 2019

@ahmedHusseinF what kind of functionality do you want to access on window "in parallel" that isn't in workers?

@domenic domenic closed this as completed Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants