2

I'm using SquishIt to combine and minify my javascript files in an MVC 3 project. I'm trying to create an offline cache.manifest and the hash codes changing between edits is killing me. Is there a way to remove the hash that is appended to the bundle?

I checked in the BundleBase.cs class and see a HashKeyNamed method but can't figure out where I would use it.

Here is my existing method for combining:

@Html.Raw(SquishIt.Framework.Bundle.JavaScript()
.Add("~/js/libs/persistence.js")
.Add("~/js/offline.common.js")
.Add("~/js/offline.syncmanager.js")
// snip...
.ForceRelease()    
.WithMinifier(SquishIt.Framework.JavaScript.Minifiers.JavaScriptMinifiers.NullMinifier)    
.Render("~/js/offline_script.js"))
Jacob
  • 3,629
  • 3
  • 36
  • 44
  • The method you mention was probably intended to be added to the interface, though I can't say for sure. I suspect you'd be better off with a method to allow excluding the querystring hash from rendered filenames (not just rename it). I would recommend opening an issue on github for this - your use case for bypassing cache invalidation seems reasonable enough, and I can see this change being useful to others. – AlexCuse Feb 23 '12 at 10:26
  • @AlexCuse Thanks. The reason I mentioned the HashKeyNamed property is because if you set it to null I notice the render method will bypass adding it to the URL. – Jacob Feb 23 '12 at 14:48
  • Ah, ok. We could probably use that behind the scenes but I think a 'WithoutHash' method might make more sense. Since this will cripple normal browser caching I think its best to be as explicit as possible. – AlexCuse Feb 23 '12 at 21:52

3 Answers3

3

Sorry I'm late to the party.

In the latest version, there is a method on bundles called .WithoutRevisionHash() that will do what you needed. This method actual came into being thanks to Jacob's pull request mentioned here.

The method itself is just a wrapper for a previously existing method called .HashKeyNamed() that could be called with an empty string as he pointed out to accomplish the result you're after. Hopefully the new method is a bit more intuitive/discoverable though :)

Community
  • 1
  • 1
AlexCuse
  • 18,008
  • 5
  • 42
  • 51
2

I don't believe there is a way. You can see all the public API options here: https://github.com/jetheredge/SquishIt/blob/master/SquishIt.Framework/Base/IBundle.cs

It is OSS though, so you can always fork the project and make the addition!

Good luck.

Paul
  • 12,392
  • 4
  • 48
  • 58
2

I've recently submitted a pull for some better support of this in SquishIt, but in the mean time, I think you can pull this off by creating your own custom JavaScriptBundle and using the HashKeyNamed() method.

public class NoHashJavaScriptBundle : JavaScriptBundle
{
    public NoHashJavaScriptBundle()
        : base()
    { }

    protected override string BeforeMinify(string outputFile, List<string> files, IEnumerable<string> arbitraryContent)
    {
        // Set the hash key to empty to keep it from being appended in Render.
        HashKeyNamed(string.Empty);

        return base.BeforeMinify(outputFile, files, arbitraryContent);
    }
}

Then in your _Layout you could do something like this:

@Html.Raw(new NoHashJavaScriptBundle()
.Add("~/js/libs/persistence.js")
.Add("~/js/offline.common.js")
.Add("~/js/offline.syncmanager.js")
// snip...
.ForceRelease()    
.WithMinifier(SquishIt.Framework.JavaScript.Minifiers.JavaScriptMinifiers.NullMinifier)    
.Render("~/js/DontHashMeBro.js"))
Jacob
  • 3,629
  • 3
  • 36
  • 44