Appending # in the Render method for SquishIt framework support web farm

459 views Asked by At

I am using SquishIt framework to work on Bundling and Magnification feature for bundling and minifying the js and css files.

I am using the code as mentioned below:

<%= Bundle.JavaScript()
        .Add("~/js/jquery-1.4.2.js")
        .Add("~/js/jquery-ui-1.8.1.js")
        .Render("~/js/combined_#.js")
%>

The above code works well in case I have a single webserver. I want to know whether appending _#" in the output file name will create an issue in the webfarm. If it creates an issue then what is the best solution to resolve the issue.

Can anyone help me to know more details about the occurrence of the issue in webfarm scenario.

Thanks & Regards, Santosh Kumar Patro

1

There are 1 answers

2
AlexCuse On

This can create issues if you don't have sticky sessions enabled in your load balancer. Because you are rendering the file in your view, it could be rendered on server 1 and the request for the asset actually ends up routed to server 2, where the file may not have been created yet.

In a web farm scenario I think it is best to create your bundles on Application_Start, then render in your view using one of the cached / named methods.

So if you want to keep rendering to static files instead you'd have something like this in application_start (global.asax.cs) or downstream (I like a specialized initializer for SquishIt)

Bundle.JavaScript()
    .Add("~/js/jquery-1.4.2.js")
    .Add("~/js/jquery-ui-1.8.1.js")
    .RenderNamed("bundleName", "~/js/combined_#.js") //2nd arg is used to resolve disk location

Then to render in your view:

<%= Bundle.JavaScript().RenderNamed("bundleName") %>

This will ensure that the file has been created by the time the server is ready to respond to requests, at the expense of application startup time (make sure your app pool isn't getting recycled too often!).

The asset controller method may be better because it gives you a chance to recover if the bundle is not found. You can read about that here: https://github.com/jetheredge/SquishIt/wiki/Using-SquishIt-programmatically-without-the-file-system

Finally, using a CDN may be a good option as well. You can do some reading on that (using Amazon S3 / Cloudfront, but the ideas apply to any CDN) here: http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/making-squishit-work-with-amazon