Page MenuHomePhabricator

For raw script loading, use document.write() to get a callback for the previous document.write()
Closed, ResolvedPublic

Description

Right now when loading raw scripts in the head (which have to use document.write as the only widely supported way of synchronously loading scripts), there is no built-in callback mechanism.

While the write is somewhat synchronous, that only holds up from the perspective of the outer DOM and position in the HTML input stream. From the immediate execution point of view, it is not accurate code right below document.write() does run before the written script tag is loaded and executed.

This causes bugs when we use raw script loading in debug mode. It doesn't affect production mode much (we worked around in the few relevant cases).

Funny detail about document.write is that the browser's behaviour for regular html files (one script tag runs after the other, regardless of it sometimes fetching in parallel) holds up for dynamically written script tags as well.

As such we can actually get a decent callback for after the script is finished by queuing up another document.write().

Initially I thought we'd have to do this for each file in the module (utilising some kind of temporary global callback, similar to what jQuery does for JSON-P). However we should be able to just write all the files to the html buffer and add a mw.loader.state( .. ) call at the end. Similarly to how we do with only=script responses from load.php (which is what debug mode uses by default for dynamic modules not based in the file system).