cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

Hijack dependency load requests into ONE #150

Closed jibiabraham closed 11 years ago

jibiabraham commented 11 years ago

Is it possible, without breaking dependency tracking, to hijack all the requests ( while loading dependencies ) into a single POST request, with the paths as params? This way, the server can concat and minify all the required files and the dependencies can be loaded in one call.

unscriptable commented 11 years ago

Hey Jibi!

This sounds possible. There are three hurdles to tackle. I can show you how to get past the first and give you a clue to how to solve the rest. :)

  1. Override default curl.js behavior.
  2. Collect module requests and launch a request in the "next turn".
  3. Create hidden form element(s) to execute the POST.

Overriding curl.js behavior is straightforward. There is a built-in module called "curl/_privileged". This module's API is not stable! However, the method you need to override, "loadScript" is fairly stable (and we're overriding it for many other things like our server-side shim). You should create a "shim" module like the following and ensure that it is loaded asap.

// my/post-shim
define(["curl/_privileged", function (priv) {
    priv.core.loadScript = function (def, success, fail) {
        // def is an object with useful info such as module id and url
    };
});

There are a few ways to ensure that the shim is loaded first. One way is to append the shim right onto the end of the curl.js file. Another way is to use the preloads config param: preloads: ["my/post-shim"].

Inside your new loadScript function I imagine you'll need to collect module requests together and send a single POST. The simplest way to do this is with a setTimeout, but take a peek here for much faster alternatives to setTimeout.

I imagine you could dream up the code to create one or more form elements to perform the POSTs. This part seems relatively simple, imho.

I'm going to close this issue, but feel free to keep commenting or asking questions. Or go over to the cujojs google group and open some discussion on it!

Regards,

-- John