Html: Allow running text/javascript directly without a text/html

Created on 2 Nov 2017  路  3Comments  路  Source: whatwg/html

Some modern websites (e.g. google.com and gmail.com) serve a mostly blank text/html with primarily a mechanism to deliver a text/javascript content. text/javascript is responsible for loading other modules/resources as well as for constructing DOM.

Example (curl google.com):

screen shot 2017-11-02 at 12 23 21 pm

HTML comes with a set of expectations (e.g. with regards to rendering, responsiveness and dealing with input events), and I'm wondering if allowing browsers to run text/javascript directly, even in the absence of a text/html, to control (explicitly, imperatively) how/when things get executed would speed things up (compared to the amount of guesses that browsers have to do).

Specifically, I'm wondering if a combination of the following would help (in no particular order):

  • one less file to download (unlikely to help)
  • scheduling network resources before rendering/input/compositing
  • other reasons (e.g. i'm wondering if there is a point to be made about ergonomics)?

For example, looking how google.com is loaded, I'm wondering if running scripts first before we get into any rendering at all (e.g. all of the work done in the GPU, compositing, input) would enable authors to parallelize better.

screen shot 2017-11-02 at 12 27 36 pm

In this trace from google search, the first big script is loaded at 297ms late in the game. It had to wait until about 14 images were loaded and rendering work started.

The browser has to make a lot of assumptions on how things are going to be loaded, and I'm wondering if we left that to the application developer they'd have a more fined grained control and would be able to derive more value from the different threads/processes browsers provide.

e.g. something along the lines of:

// google.com/index.js

// kicks off network requests for resources that we are going to need
let main = import("/dynamically-load-module.js");
let sidebar = import("/not-so-important-module.js");

// maybe kick off loading of images?

// ok, now that we have kicked off the network request and
// the threads/processes are going to be working on them
// lets kick off some UI and play with the DOM
window.document.title = "Google Search"
window.document.body.style = "background-color: white";
window.document.body.appendChild(...)
...

No data to back me up here, just kicking off a thread with an intuition that something useful can come out of it :)

additioproposal needs implementer interest navigation

Most helpful comment

In general, it is advisable to start by figuring out what problem you are trying to solve. https://whatwg.org/faq#adding-new-features has a good guide towards adding new features that touches upon that. https://whatwg.org/working-mode#changes has some additional requirements.

All 3 comments

This sounds like the opposite to progressive enhancement, and I don't see the advantages.

  • It will only work if JavaScript is enabled, otherwise you just see a blank page.
  • It will not work with web crawlers, e.g. a crawler that wants to get metadata for display in a social network post or a search results page. These crawlers typically only parse the HTML and ignore any JavaScript.
  • In general, there is no need to execute JavaScript during page load, and any such JavaScript would have a worse performance than embedding the HTML directly. The initial DOM tree (including CSS styles) for the first paint can be generated server-side; and any JavaScript event handlers can be added after DOMContentLoaded.

Regarding resource loading, web developers already have a lot of control. You can:

  • Indicate to user agents that they should preload a resource with <link rel="preload">
  • Include a JavaScript files with the async and defer attributes (