3
votes

I am using something like airbnb's rendr, i.e. my own implementation of sharing backbone code between client and server, to build full HTML on the server using the same Backbone models, views, collections and templates I am using on the client. My files/modules are defined as requirejs modules, that way I can share them easily on the client. In development mode, I want requirejs to refetch/reload any modules from disc when I refresh my browser (without restarting the server), so I get my server rendering uses the newest templates and javascript to finally serve me the newest HTML.

when using requirejs on the server with nodejs, the trick of appending bust parameters to the urlArgs like the following doesn't work, i.e. the server doesn't reload/refetch any modules from disc

urlArgs: "bust=v2" 

I wonder, if reloading/refetching requirejs modules from disc-space without restarting the server is possible in node? specifically, that would be very useful for the require-text plugin for template. additionally, it would be nice to apply reloading only to a restricted set of modules.

2
You again! :P How exactly does it not work? Also, I find urlArgs: "bust=" + (new Date()).getTime() is better while you're developing. Also also, why aren't you using node's require system?c24w
hi c24w :-) I updated my answer. I hope it's clearer nowforste

2 Answers

2
votes

I've never had to do this, but a quick search shows a few options, including automating the server restart:

You may also be able to do something similarly creative to this:

  • delete require.cache['/home/shimin/test2.js']

You could almost definitely clear the version RequireJS has in cache, forcing it to reload it, although I suspect the node would just serve up the old file again.

Finally, maybe take a look at hot-reloading, which seems to work around the caching without needing to restart the server (example).

1
votes

Like the OP, I was looking for a way to make development in node with RequireJS easier and faster. In my case, I am using RequireJS with a grunt watch task in node, and wanted to be able to force RequireJS to always get the latest version of a module from the file system. And happily, I found a very simple solution!

I had to slightly modify Henning Kvinnesland’s suggestion by adding the timeout, but now it is working like a charm:

var requirejs = require('requirejs');
// Set up requirejs.config here...
// Disable caching of modules
requirejs.onResourceLoad = function(context, map) {
    setTimeout(function() {
        requirejs.undef(map.name);
    }, 1);
};