Re:Managing Async Dependencies with JavaScript
by bfred_it on Saturday May 20, 2017 @07:36AM
(#14381934)
If your intent is to load scripts asynchronously and keep their order, just use `<script defer>`. From what I see, you only ever need something like `fetch-inject` if you also want to load stylesheets OR if you want a load callback.
Re:Managing Async Dependencies with JavaScript
by jayflux on Saturday May 20, 2017 @10:29AM
(#14382525)
Yes this is true, defer should pull down scripts asynchronously, but execute them in inserted order. If it's just scripts that's all you need to do, you don't need this.
Re:Managing Async Dependencies with JavaScript
by robocat on Saturday May 20, 2017 @12:40PM
(#14383164)
For mobile, my timings showed fetch/XHR to be heaps laggier than <script> tags. This matters for any resources on the critical path for rendering your page.
I naively thought that it would be quicker to have an inline <script> (coming first in the <head>) and have the script request JavaScript resources via XHR (to manage resource dependencies and load failures).
However, the browser decides the priority for resource requests and CPU usage. I found that XHR requests got low priority and wouldn't even get requested until after painting delays.
Re:Managing Async Dependencies with JavaScript
by jhabdas on Sunday May 21, 2017 @09:06AM
(#14387112)
WRT performance, fetch is a newer standard and still getting ironed out. Browsers have been using external scripts for years and know how to handle them efficiently. A downside to using `script` tags however, is they have to be on the page when the document is loaded, and they block the browser run loop.
Re:Managing Async Dependencies with JavaScript
by tpetry on Saturday May 20, 2017 @02:54AM
(#14381336)
What is the difference between XHR-inject and FETCH-inject? Isn't it that the second simply uses the fetch api and is more modern? But everything could be done with XHR too?
Re:Managing Async Dependencies with JavaScript
by z3t4 on Saturday May 20, 2017 @03:35PM
(#14383956)
You can use http push (link preload header) or prefetch to make the browser download all dependencies async. Then require the modules sync. when they are needed. The advantages with modules is that they can always be cached! You do not have to bundle or add compile steps. Just use sync. require. Here's an example require script: https://www.webtigerteam.com/webmodules/require.js
Re:Managing Async Dependencies with JavaScript
by jhabdas on Sunday May 21, 2017 @09:03AM
(#14387099)
Push has its use cases. It has its distinct drawbacks as well. Consider the scenario when you want to lazy-load a script based on a user action so you're not aggressively loading things the user might not need. You may also enjoy: https://www.smashingmagazine.com/2017/04/guide-http2-server-...
Re:Managing Async Dependencies with JavaScript
by z3t4 on Sunday May 21, 2017 @03:14PM
(#14388696)
It's up to the browser to optimize. What I've discovered so far is that push has low priority and that if a script is required while it's being downloaded (pushed), the browser (chrome) downloads it twice. The browser could however be smart about it and just make the required script top priority and only download it once. The browser could also parse the pushed scripts (chrome currently doesn't) if the cpu is idle. This opens up for modules on the web, like in Node.JS, but without the latency penalty. Also there is no need for the browser to pre-parse the scripts to look for modules when they are already pushed.
Re:Managing Async Dependencies with JavaScript
by darth_mastah on Saturday May 20, 2017 @02:22PM
(#14383604)
> Others may choose to use a bundler to concatenate all of the JavaScript files into one or two packages, and load them asynchronously to avoid these problems, typically leading to a SEO-unfriendly Single-Page Application primed for Front-end SPOF.
Shortsighted as I am, I struggle to see how fetch-inject guards against SPOF?
Having said that, I can see how this library could be useful for projects with no bundler.
Re:Managing Async Dependencies with JavaScript
by jhabdas on Sunday May 21, 2017 @09:09AM
(#14387121)
It helps guard against SPOF by enabling the application to load in smaller pieces, helping guard against not just SPOF in the front-end but also SPOF in the development pipeline when a left pad makes your bundler stop working.
Re:Managing Async Dependencies with JavaScript
by GroSacASacs on Saturday May 20, 2017 @04:13PM
(#14384129)
How does this compete with putting a big script tag at the end of the body in the HTML that contains all the code necessary ? What settings can make this way of requiring scripts useful ?
I guess this can be useful, to load extra functionality at run time, in very big single page application.
Re:Managing Async Dependencies with JavaScript
by jhabdas on Monday May 29, 2017 @10:10AM
(#14439146)
In a large document, or one with scripts embedded throughout, waiting until the body is finished parsing could take considerably longer than async loading in the head.
Re:Managing Async Dependencies with JavaScript
by jamescostian on Friday May 19, 2017 @09:03PM
(#14380444)
AFAICT using async defer, one cannot express "you need to load X.js and Y.js before running Z.js, and you need to load X.js and A.js before running B.js". It looks like fetchInject allows you to express that type of thing.
But how is this different from RequireJS? I think RequireJS uses XHR and I don't think it supports CSS, but aside from that, what's new? I feel like this is just reinventing AMD.
Re:Managing Async Dependencies with JavaScript
by thehandofkwll on Saturday May 20, 2017 @12:38AM
(#14381064)
Yep, this is the race condition that the author was referring to.
RequireJs also let's you express these dependencies, but will block behind the cssdom due to the way it injects.
Actually, I made a little library which let's you manage dependencies with script async or defer, and can be considered an alternative to the authors (neat) solution. https://github.com/bohdyone/adm.js
Re:Managing Async Dependencies with JavaScript
by lewisjoe on Saturday May 20, 2017 @03:34AM
(#14381409)
Curious question: Can you explain a bit more on "will block behind the cssdom due to the way it injects"
As far as I know, requireJS doesn't do XHR requests and evals. That sounds ugly. I think it dynamically injects a script tag and loads the module. I maybe wrong.
Either way, how's it a blocking one?
So, the only thing that makes fetchInject, differ from requireJS is the non-blocking way. Is my understanding correct?
Re:Managing Async Dependencies with JavaScript
by dgoldstein0 on Saturday May 20, 2017 @03:51AM
(#14381448)
also wondering about the cssom comment. I can confirm that requirejs works by injecting <script> tags - <script async> to be precise. requirejs' api isn't promise based, but that's mostly an aesthetic concern.
btw you seem to imply that requirejs blocks - what do you mean by that?
Re:Managing Async Dependencies with JavaScript
by yladiz on Friday May 19, 2017 @09:12PM
(#14380479)
This seems to be a lot simpler in scope. RequireJS allows you to essentially do dependency injection, whereas this doesn't handle that. It also uses the fetch API rather than plain XHR as well as using and returning native promises when fetching libraries. I think it's primarily for loading resources from a different server, like a CDN, in arbitrary but definable order, and that's it.
Re:Managing Async Dependencies with JavaScript
by dgoldstein0 on Saturday May 20, 2017 @03:52AM
(#14381453)
requirejs doesn't natively support css, but it has a pretty easy to use loader api so it'd be easy to add css support and in fact one already exists: https://github.com/guybedford/require-css
Re:Managing Async Dependencies with JavaScript
by bmh_ca on Friday May 19, 2017 @09:30PM
(#14380552)
Well, a note: async and defer are different and logically incompatible, in terms of load order. (IIRC) defer preserves order whereas async does not. These alter the dependency management strategy entirely.
I've been using `defer` exclusively ever since I read this: https://calendar.perfplanet.com/2016/prefer-defer-over-async...
The effort that goes into these posts without addressing the basics always boggles my mind.
I naively thought that it would be quicker to have an inline <script> (coming first in the <head>) and have the script request JavaScript resources via XHR (to manage resource dependencies and load failures).
However, the browser decides the priority for resource requests and CPU usage. I found that XHR requests got low priority and wouldn't even get requested until after painting delays.
From a user's perspective, you could create the exact same experience using vanilla XHR (which is exactly what AMD module loading does with `define`).
Believe it or not but currently it's actually the other way around: XHR is defined in terms of fetch:
> XHR is now defined in terms of fetch (see the calls to fetch in XHR's .send()), meaning fetch is lower level than XHR.
Source: https://jakearchibald.com/2015/thats-so-fetch/ and https://xhr.spec.whatwg.org/#the-send%28%29-method
Shortsighted as I am, I struggle to see how fetch-inject guards against SPOF?
Having said that, I can see how this library could be useful for projects with no bundler.
I guess this can be useful, to load extra functionality at run time, in very big single page application.
<script src=... async defer>
for all scripts - could anyone explain?
But how is this different from RequireJS? I think RequireJS uses XHR and I don't think it supports CSS, but aside from that, what's new? I feel like this is just reinventing AMD.
As far as I know, requireJS doesn't do XHR requests and evals. That sounds ugly. I think it dynamically injects a script tag and loads the module. I maybe wrong.
Either way, how's it a blocking one?
So, the only thing that makes fetchInject, differ from requireJS is the non-blocking way. Is my understanding correct?
btw you seem to imply that requirejs blocks - what do you mean by that?
imho this is sufficient:
usage: