4 ms·
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
by z3t4 9y ago
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 https://www.webtigerteam.com/webmodules/require.js
- jhabdas 9y agoPush 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-push/ https://www.smashingmagazine.com/2017/04/guide-http2-server-...
- z3t4 9y agoIt'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.