CodeByDay.FileTagger
1.2.0
See the version list below for details.
dotnet add package CodeByDay.FileTagger --version 1.2.0
NuGet\Install-Package CodeByDay.FileTagger -Version 1.2.0
<PackageReference Include="CodeByDay.FileTagger" Version="1.2.0" />
paket add CodeByDay.FileTagger --version 1.2.0
#r "nuget: CodeByDay.FileTagger, 1.2.0"
// Install CodeByDay.FileTagger as a Cake Addin #addin nuget:?package=CodeByDay.FileTagger&version=1.2.0 // Install CodeByDay.FileTagger as a Cake Tool #tool nuget:?package=CodeByDay.FileTagger&version=1.2.0
Code By Day: File Tagger
Repo for the Code By Day: File Tagger NuGet package: nuget.org/packages/CodeByDay.FileTagger
Creates HTML paths with a tag to circumvent Browser caching. Static files should be set to cache indefinitely. However, this creates an issue when you need to update said static files. CodeByDay.FileTagger offers the solution. This NuGet allows adding a tag to a static file. By updating this tag when the file is modified, browser caches are successfully circumvented.
Release Notes
1.2.0 [2020 Sep 21] (changes)
Added explicit support for .NET 4.6.1 as urged by the guidance.
Removed support for .NET 3.5 in favor of .NET 4.5. Stay on 1.1.0 if you need to support 3.5 or 4.0.
Usage Guide
File Tagger Kinds
ConstantTagger: Tags files with a given constant ("tag=constant"). Useful if you deploy with set versions.
WriteTimeTagger: Tags files with the destination file's write time as reported by the operating system ("tag=190102030405"). Useful if files are only written when they are updated.
HashTagger: Tags files with a hash of the file's content ("tag=CCDED65A"). This Tagger ensures that the user won't need to download the file again if it didn't change, but the write time or your deployed version did.
Tagging Methods
QueryString: Appends a query param to the file name ("path/file.css?tag=CCDED65A"). This should be fine in most cases, but may potentially cause files to not be cached. Query params, after all, are suppose to be indicative of an action, not a static file.
Path: Adds a fake folder in the filepath ("path/tag=CCDED65A/file.css"). You MUST use RewriteRules! This looks more correct in the source code and should never fail to cache.
FileName: Adds a fake section in the file name ("path/file.tag=CCDED65A.css"). You MUST use RewriteRules! The result when given a file without an extension is undefined. This could potentially cause issues if browsers only expect a single dot, but should never fail to cache.
Using a Tagger
Pass the native path into the GetPath function of a tagger to transform it with the tagger's method.
<link rel="stylesheet" type="text/css" href="@(new HashTagger().GetPath("/css/bundle.min.css"))" />
This yields the following in HTML:
<link rel="stylesheet" type="text/css" href="/css/bundle.min.css?tag=CCDED65A" />
I recommend using a singleton and accessing that same instance for all files. This allows you to only need to configure the Tagger in one place. It also allows the WriteTimeTagger and HashTagger to cache their results for a configurable about of time.
Configuring a Tagger
When using .NET Core, configuration can be done entirely in your Startup.Configure() function:
#!c#
// In my implementation, I've set up a singleton, FileTagger.Current, which is an ITagger: public static class FileTagger { public static ITagger Current { get; set; } }
// You can use a different File Tagger Kind: I use the HashTagger here as an example.
FileTagger.Current = new HashTagger
{
TaggingMethod = TaggingMethod.Path, // Use whichever method you'd like.
// Omit this compile-time if when using a Constant tagger. You should set the 'Constant' Property instead.
#if !DEBUG
CacheTime = System.TimeSpan.FromHours(1) // Use whatever length of time you'd like. I'd recommend only setting this caching in production.
#endif
};
// Set rewrites if we need to. You can also use GetRewriteOptions() or GetRewriteRule() if needed.
FileTagger.Current.TaggingMethod.SetRewriteRule(app);
When using .NET Framework, there is a slight caveat: You need to manually set rewrite rules. First, set the object settings in the Application_Start() function:
#!c#
// In my implementation, I've set up a singleton, FileTagger.Current, which is an ITagger: public static class FileTagger { public static ITagger Current { get; set; } }
// You can use a different File Tagger Kind: I use the HashTagger here as an example.
FileTagger.Current = new HashTagger
{
TaggingMethod = TaggingMethod.Path, // Use whichever method you'd like.
// Omit this compile-time if when using a Constant tagger. You should set the 'Constant' Property instead.
#if !DEBUG
CacheTime = System.TimeSpan.FromHours(1) // Use whatever length of time you'd like. I'd recommend only setting this caching in production.
#endif
};
Now, you'll need to manually add rewrite rules to your web.config. The content of this rewrite can be obtained by calling TaggingMethod.GetRewriteRule(). They are as follows.
Path:
#!c#
<rewrite>
<rules>
<rule name="FileTaggerRewrite" stopProcessing="true">
<match url="(.*?)(tag=[^/]*?)/([^/]*)$" />
<action type="Rewrite" url="{R:1}{R:3}" />
</rule>
</rules>
</rewrite>
FileName:
#!c#
<rewrite>
<rules>
<rule name="FileTaggerRewrite" stopProcessing="true">
<match url="(.*?)\.(tag=[^\.]*?)\.([^\.]*)$" />
<action type="Rewrite" url="{R:1}.{R:3}" />
</rule>
</rules>
</rewrite>
File Paths
For the WiteTimeTagger and HashTagger, the file must be readable. If the file is relative to the project root, nothing needs to be done. However, if you have a relative path, you need to set the FileRoot property. You can do this by appending the extra path sections.
Example: assume the currently loaded page is https://www.example.com/home/garden/soil/index.html
The following link will load https://www.example.com/home/garden/soil/soil.css
<link rel="stylesheet" type="text/css" href="soil.css" />
To make it work with the FileTagger, update the FileRoot:
@{
var tagger = new HashTagger();
tagger.FileRoot = Path.Combine(tagger.FileRoot, "home/garden/soil");
}
<link rel="stylesheet" type="text/css" href="@(tagger.GetPath("soil.css"))" />
Of course, it's much easier to use a full link. This feature is intended for rare use cases.
Supported .NET Versions
- .NET Framework 4.5+
- .NET Core 1.0+
Additionally, this project targets .NET Standard 1.3 and .NET Standard 2.0. However, the actual implementation is undefined on these platforms. These targets are only intended to allow the loading of this project if needed.
Repo
Branches
The branches in this repo relate directly to the release they are building up to. Once a release has been made, the branch is no longer commited to. This effectively means there is no "master" branch: only releases.
Versioning
This project uses Semantic Versioning 2.0.0
If you're making a NuGet package, I recommend editing your .csproj to force this Semantic Versioning. This example requires a version equal to or above 1.2.0 and disallows 2.0.0 or above.
#!c#
<PackageReference Include="CodeByDay.FileTagger" Version="[1.2.0,2.0.0)" />
Have an Issue?
If you find any issues with this NuGet, please use the Contact owners link on the NuGet page.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 is compatible. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.3 is compatible. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 1.0
- CodeByDay.Cache (>= 1.3.0 && < 2.0.0)
- Microsoft.AspNetCore.Rewrite (>= 1.0.0)
- Microsoft.NETCore.App (>= 1.0.0)
-
.NETFramework 4.5
- CodeByDay.Cache (>= 1.3.0 && < 2.0.0)
-
.NETFramework 4.6.1
- CodeByDay.Cache (>= 1.3.0 && < 2.0.0)
-
.NETStandard 1.3
- CodeByDay.Cache (>= 1.3.0 && < 2.0.0)
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- CodeByDay.Cache (>= 1.3.0 && < 2.0.0)
- Microsoft.AspNetCore.Rewrite (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.