Skip to content

Instantly share code, notes, and snippets.

@GeorgDangl
Last active January 11, 2019 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgDangl/a3782d74e339f7ed46666776ccf30def to your computer and use it in GitHub Desktop.
Save GeorgDangl/a3782d74e339f7ed46666776ccf30def to your computer and use it in GitHub Desktop.
Caching bundled and minified JavaScript and CSS in the free tier on CloudFlare with the ClientDependency package in Umbraco and other IIS-hosted websites, see https://blog.dangl.me/archive/caching-bundled-and-minified-css-and-javascript-with-the-wrong-file-extension-in-the-free-tier-on-cloudflare-with-umbraco/
<mvc defaultRenderer="StandardRenderer">
<renderers>
<add name="StandardRenderer" type="Dangl.Blog.NonAxdDependenciesRenderer, Dangl.Blog" />
<add name="LazyLoadRenderer" type="ClientDependency.Core.FileRegistration.Providers.LazyLoadRenderer, ClientDependency.Core" />
</renderers>
</mvc>
namespace Dangl.Blog
{
/// <summary>
/// This renderer replaces the file extension .axd in the generated bundles for JavaScript and CSS. This
/// is required because the CloudFlare free tier does not support caching for .axd files. This can only
/// be used in combination with a rewrite rule that rewrites DependencyHandler.(css|js) to DependencyHandler.axd,
/// e.g. in the web.config file under system.WebServer/rewrite
/// </summary>
public class NonAxdDependenciesRenderer : ClientDependency.Core.FileRegistration.Providers.StandardRenderer
{
protected override string RenderSingleCssFile(string css, IDictionary<string, string> htmlAttributes)
{
return base.RenderSingleCssFile(css, htmlAttributes).Replace(".axd", ".css");
}
protected override string RenderSingleJsFile(string js, IDictionary<string, string> htmlAttributes)
{
return base.RenderSingleJsFile(js, htmlAttributes).Replace(".axd", ".js");
}
}
}
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="DependencyHandler">
<match url="(^DependencyHandler\.)(css|js)(.*)"/>
<action type="Rewrite" url="{R:1}axd{R:3}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment