Using URL Rewrite in IIS together with SquishIt?

176 views
Skip to first unread message

Samuel Milton

unread,
May 17, 2013, 6:54:53 PM5/17/13
to squi...@googlegroups.com
Hi guys!

First off, we've been using SquishIt for some time now and it works really well and is really stable, it hasn't failed us a single time. Great work!!

That said, I have an idea. Maybe someone already has worked this out...

When you go to release mode you have two options:
1) Render the file with fingerprint in filename, eg main_8656adf.css
2) Render the fingerprint as querystring, eg main.css?v-87654adeh

Both has advantages, and disadvantages. First one is good according to google page speed, but renders LOTS of files. Second one only renders a single file, but will fail in some caching proxys according to Page speed.

Then I stumbled over Mads Kristensens blog post about cache busting and URL Rewrite module in iis, http://madskristensen.net/post/Cache-busting-in-ASPNET.aspx

Would it be possible to combine SquishIt with URL Rewite in some way? So the url sent to the browser is /ui/css/v-7865489/main.css but the v-7865489 folder is just a virtual directory, the file is really located in /ui/css/ folder.

Is it possible?

Thanks again for a great library!

Alex Ullrich

unread,
May 18, 2013, 9:59:52 AM5/18/13
to squi...@googlegroups.com
This is a great idea. It'll probably require some structural changes but shouldn't be terribly painful. I'll try to get this into the next release (last planned release with v3.5 support).

Samuel Milton

unread,
May 21, 2013, 7:38:58 AM5/21/13
to squi...@googlegroups.com
That would be totally awesome! :)

I just did a couple of tests, and you can do like this: .Render("/ui/v-#/rel-top.js") and get /ui/v-23487987he55ab/rel-top.js. So it works to include "#" as part of a folder name.

But when you try to load the page you get an error:

Unable to open file 'C:\xxxxxx\rel-top.js'

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: Unable to open file 'C:\xxxx\ui\v-
23487987he55ab\rel-top.js'


Can you get notified somehow when a new release is out? Would be nice. 

Samuel Milton

unread,
May 21, 2013, 7:52:42 AM5/21/13
to squi...@googlegroups.com
I guess the problem is that SquishIt tries to render the output file to a system path, eg c:\inetpub\wwwroot\ui\v-23487987he55ab\rel-top.js. And therefore bypassing the IIS and the Rewrite rule?

Alex Ullrich

unread,
May 21, 2013, 8:56:51 AM5/21/13
to squi...@googlegroups.com
Yeah that is the problem.  I think we're going to need to add an extensibility point for cache invalidation strategies and use that.  Its going to need to be opt-in behavior since not everyone will have the rewrite rules in place.

As far as notifications go, this looks like it might do the trick: http://nugetfeed.org/

Alex Ullrich

unread,
May 21, 2013, 9:44:56 AM5/21/13
to squi...@googlegroups.com

Samuel Milton

unread,
May 22, 2013, 7:51:35 AM5/22/13
to squi...@googlegroups.com
Perfect, I'll be following it!

Alex Ullrich

unread,
May 24, 2013, 1:22:45 PM5/24/13
to squi...@googlegroups.com
This has been implemented.  Seems to work pretty well, though you do need to be careful to position your hash to match your rewrite rule.

see example here: 


I will get a package pushed to nuget as soon as I'm near a computer thats set up in a way that makes it easy.

Alex Ullrich

unread,
May 24, 2013, 6:20:26 PM5/24/13
to squi...@googlegroups.com
Just pushed v0.9.3-beta3 to nuget

Will do an introductory blog post in the next couple days.  You can turn it on globally using:

Bundle.ConfigureDefaults().UseCacheInvalidationStrategy(new SquishIt.Framework.Invalidation.HashAsVirtualDirectoryCacheInvalidationStrategy());

This strategy replaces the hash symbol in the url with {hashKeyName}-{hash}, and the web.config rule for it looks like this:

    <rewrite>
      <rules>
        <rule name="squishit">
          <match url="([\S]+)(/r-[\w]+/)([\S]+)" />
          <action type="Rewrite" url="{R:1}/{R:3}" />
        </rule>
      </rules>
    </rewrite>

You can implement the interface on your own if you want to use a different pattern.  SquishIt's default HashKeyName is "r" - I will add an option to configure it globally shortly.

Samuel Milton

unread,
May 26, 2013, 8:45:23 AM5/26/13
to squi...@googlegroups.com
Looks very good! Will be testing it out tomorrow. Is beta3 including the globally configurable hash name? Noticed you closed that issue on github.

Samuel Milton

unread,
May 27, 2013, 8:53:19 AM5/27/13
to squi...@googlegroups.com
Works like a charm Alex, great work!

Only thing I'm waiting for is the globally configurable hash name since I have ~20 bundles on the site and it's a bit messy setting the name on every bundle.

Thanks!

Alex Ullrich

unread,
May 28, 2013, 8:56:26 AM5/28/13
to squi...@googlegroups.com
I should have a new package up by week's end - just trying to take care of some issues that popped up when our CI environment was updated to mono 3.

Sorry about that - I could have sworn the hash name was already globally configurable.

Samuel Milton

unread,
Jun 4, 2013, 3:36:03 AM6/4/13
to squi...@googlegroups.com
Hi Alex!

Any progress on this? I know you do this in your spare time so I don't want to stress you about it, but it would be very appreciated with an updated package.

//Samuel

Alex Ullrich

unread,
Jun 5, 2013, 8:30:01 AM6/5/13
to squi...@googlegroups.com
Sorry, the mono fixes ended up more involved than I expected.

I pushed 0.9.3-beta4 this morning, you should be able to get it off nuget.

Look for Configuration.Instance.UseHashAsVirtualDirectoryInvalidationStrategy
Reply all
Reply to author
Forward
0 new messages