-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace DNTCommon.Web.Core; | ||
|
||
/// <summary> | ||
/// This utility class will help you to add your `asp-append-version` like query string value to the .css or .js files. | ||
/// </summary> | ||
public static class FileVersionHashProvider | ||
{ | ||
private static readonly string ProcessExecutableModuleVersionId = | ||
Assembly.GetEntryAssembly()!.ManifestModule.ModuleVersionId.ToString("N"); | ||
|
||
/// <summary> | ||
/// Adds a version hash to a specified file. | ||
/// If you can't use `asp-append-version` directlty in Blazor apps, use this method instead. | ||
/// </summary> | ||
/// <param name="httpContext">Encapsulates all HTTP-specific information about an individual HTTP request</param> | ||
/// <param name="filePath">The path of the .js or .css file to which version should be added</param> | ||
/// <param name="defaultHash"> | ||
/// If it's not possible to calculate the hash of the filePath, this value will be used. | ||
/// If you don't specify it, Assembly.GetEntryAssembly()!.ManifestModule.ModuleVersionId will be used for it. | ||
/// </param> | ||
/// <returns></returns> | ||
public static string GetFileVersionedPath(this HttpContext httpContext, string filePath, string? defaultHash = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(httpContext); | ||
|
||
var fileVersionedPath = httpContext.RequestServices.GetRequiredService<IFileVersionProvider>() | ||
.AddFileVersionToPath(httpContext.Request.PathBase, filePath); | ||
|
||
return IsEmbeddedOrNotFound(fileVersionedPath, filePath) | ||
? QueryHelpers.AddQueryString(filePath, new Dictionary<string, string?>(StringComparer.Ordinal) | ||
{ | ||
{ | ||
"v", defaultHash ?? ProcessExecutableModuleVersionId | ||
} | ||
}) | ||
: fileVersionedPath; | ||
} | ||
|
||
private static bool IsEmbeddedOrNotFound(string fileVersionedPath, string filePath) | ||
=> string.Equals(fileVersionedPath, filePath, StringComparison.Ordinal); | ||
} |