Jacob
Jacob

Reputation: 3639

Is there a way to remove the hash from combined javascript files in SquishIt?

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"))

Upvotes: 2

Views: 801

Answers (3)

AlexCuse
AlexCuse

Reputation: 18306

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 :)

Upvotes: 3

Jacob
Jacob

Reputation: 3639

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"))

Upvotes: 2

Paul
Paul

Reputation: 12440

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.

Upvotes: 2

Related Questions