Allow link/script tag helpers to generate cache bust for external files.
Right now, asp-append-version will generate a cache bust based on the file's bits, and cache that version string for future requests. However, if the file is externally sourced, i.e. a CDN, asp-append-version cannot calculate this version string.
I see 2 good solutions to this:
- Go ahead and download the external file, calculate the version string, and then cache that - sure the initial calculation will take a little longer than if the file existed on local disk, but not too long, right?
- Create a new tag helper attribute like asp-prepend-cdn-domain, or whatever, and simply prepend that string value to the original src/href. (I actually implemented this for our needs, very simple but not worthy to go in the core codebase)
When using a CDN don't you generally add a different file with a different URL instead of changing an existing file?
I don't think we want to do (1). It defeats the purpose of using a CDN, right?
For (2), why would you prepend instead of append? What would the prepended value look like?
We source a CDN from our site root, and point our links/scripts to the CDN in our markup. In the final rendered markup, it looks like this:
<script src="//somecdndomain/js/file.js?v=12345667" ></script>
The CDN is just sourced from our site root. With System.Web.Optimizations, this was easy, just add an extension method to IHtmlString, and bam, it worked. We got all the benefits of runtime bundling + the benefits of a CDN.
public static System.Web.IHtmlString ApplyCdn(this System.Web.IHtmlString originalBundleUrl, string cdnDomain)
{
var outString = originalBundleUrl.ToString()
.Replace("<link href=\"", "<link href=\"" + cdnDomain)
.Replace("<script src=\"", "<script src=\"" + cdnDomain);
return new HtmlString(outString);
}
Usage:
@Styles.Render("~/style-bundles/layouthead1").ApplyCdn("//somecdndomain")
In core1 - I've accomplished this personally in a less-than-elegant fashion with a custom tag helper that extends ScriptTagHelper & LinkTagHelper.
[HtmlTargetElement("script", Attributes = "myapp-prepend-cdn", TagStructure = TagStructure.WithoutEndTag)]
public class CdnScriptBundleHelper : ScriptTagHelper
{
[HtmlAttributeName("myapp-prepend-cdn")]
public string PrependCdn { get; set; }
public CdnScriptBundleHelper(ILogger<CdnScriptBundleHelper> logger,
IHostingEnvironment hostingEnvironment,
IMemoryCache cache,
IHtmlEncoder htmlEncoder,
IJavaScriptStringEncoder javaScriptEncoder,
IUrlHelper urlHelper)
: base(logger, hostingEnvironment, cache, htmlEncoder, javaScriptEncoder, urlHelper)
{
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
output.Attributes["src"].Value = PrependCdn + output.Attributes["src"].Value;
}
}
Then simply:
<link rel="stylesheet" href="~/css/site2.min.css" asp-append-version="true" myapp-prepend-cdn="//somecdndomain" />
That covers #2 above - but I realize not everyone uses this type of CDN architecture.
That's why I mentioned #1 - I don't think it defeats the purpose at all - except for the very 1st request. Given the speed of a CDN, and the fact that you'd cache the version string locally anyway, I think that is not a huge deal to pay that cost 1x per the app's lifecycle.
Just my 2 cents =) We will use my above implementation until a more official & elegant option is built out.
Backlog for now, will revisit again in the future. From my experience this pattern seems less common, so for now I would recommend the workaround.
We have chosen not to change the in-box tag helpers to read from external sites.