Background
I have an application where 3 views utilize html5 offline app functionality. As such, I have an app manifest generated in a razor view. A cut-down version of this view may look like the following:
CACHE MANIFEST
CACHE:
/site.min.css
/site.min.js
In order for the offline app to function correctly, the cached files must exactly match those requested by offline pages within the app. However, I would like to apply a 'cache-busting' version string to the js/css resources referenced in this manifest. For HTML tags, this is supported by the ScriptTagHelper
but I have not found any helpers/extension methods which support this for plain URLs (as required in the above manifest).
With reference to this post, I have resolved this by injecting a FileVersionProvider
into my manifest view and using the AddFileVersionToPath()
method as follows:
@inject FileVersionProvider versionProvider
CACHE MANIFEST
CACHE:
@versionProvider.AddFileVersionToPath("/site.min.css")
@versionProvider.AddFileVersionToPath("/site.min.js")
However, the FileVersionProvider
class is in the Microsoft.AspNetCore.Mvc.TagHelpers.Internal
namespace which does not fill me with confidence form a maintenance perspective.
Finally, my implementation of the DI setup for this is not exactly ideal (see below). I don't like the fact that I need to make calls to GetService()
and surely I should be specifying a specific MemoryCache
?
services.AddSingleton<FileVersionProvider>(s =>
new FileVersionProvider(
s.GetService<IHostingEnvironment>()?.WebRootFileProvider,
s.GetService<IMemoryCache>(),
new PathString("") ));
Question
Has anyone had a requirement to create a link to a js/css resource with a version string before? If so, is there a more elegant solution?