Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add cache busting to admin app (#133)

Leendert de Borst3bdc0f1171f8
diff --git a/src/AliasVault.Admin/Main/App.razor b/src/AliasVault.Admin/Main/App.razor
index 45749aa19..8bacef0ec 100644
--- a/src/AliasVault.Admin/Main/App.razor
+++ b/src/AliasVault.Admin/Main/App.razor
@@ -1,12 +1,14 @@
-<!DOCTYPE html>
+@inject VersionedContentService VersionService
+
+<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<base href="/"/>
- <link rel="stylesheet" href="css/tailwind.css"/>
- <link rel="stylesheet" href="css/app.css"/>
+ <link rel="stylesheet" href="@VersionService.GetVersionedPath("css/tailwind.css")"/>
+ <link rel="stylesheet" href="@VersionService.GetVersionedPath("css/app.css")"/>
<link rel="stylesheet" href="AliasVault.Admin.styles.css"/>
<link rel="icon" type="image/png" href="favicon.png"/>
<HeadOutlet @rendermode="RenderModeForPage"/>
@@ -14,9 +16,9 @@
<body class="bg-gray-50 dark:bg-gray-800">
<Routes @rendermode="RenderModeForPage"/>
-<script src="lib/qrcode.min.js?v=dev"></script>
-<script src="js/dark-mode.js?v=dev"></script>
-<script src="js/utilities.js?v=dev"></script>
+<script src="@VersionService.GetVersionedPath("lib/qrcode.min.js")"></script>
+<script src="@VersionService.GetVersionedPath("js/dark-mode.js")"></script>
+<script src="@VersionService.GetVersionedPath("js/utilities.js")"></script>
<script>
window.initTopMenu = function() {
diff --git a/src/AliasVault.Admin/Program.cs b/src/AliasVault.Admin/Program.cs
index 449367a8a..f21c2afe6 100644
--- a/src/AliasVault.Admin/Program.cs
+++ b/src/AliasVault.Admin/Program.cs
@@ -16,6 +16,7 @@ using AliasVault.Admin.Services;
using AliasVault.Logging;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
+using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
@@ -45,6 +46,7 @@ builder.Services.AddScoped<GlobalNotificationService>();
builder.Services.AddScoped<GlobalLoadingService>();
builder.Services.AddScoped<NavigationService>();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingAuthenticationStateProvider>();
+builder.Services.AddSingleton(new VersionedContentService(Directory.GetCurrentDirectory() + "/wwwroot"));
builder.Services.AddAuthentication(options =>
{
diff --git a/src/AliasVault.Admin/Services/VersionedContentService.cs b/src/AliasVault.Admin/Services/VersionedContentService.cs
new file mode 100644
index 000000000..fe3478dd3
--- /dev/null
+++ b/src/AliasVault.Admin/Services/VersionedContentService.cs
@@ -0,0 +1,59 @@
+//-----------------------------------------------------------------------
+// <copyright file="VersionedContentService.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Admin.Services;
+
+using System.Security.Cryptography;
+
+/// <summary>
+/// Service to provide versioned content paths for cache busting of static files.
+/// </summary>
+public class VersionedContentService
+{
+ private readonly Dictionary<string, string> _hashCache = new();
+ private readonly string _webRootPath;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="VersionedContentService"/> class.
+ /// </summary>
+ /// <param name="webRootPath">Web root path.</param>
+ /// <exception cref="ArgumentNullException">Thrown if webRootPath is not provided.</exception>
+ public VersionedContentService(string webRootPath)
+ {
+ _webRootPath = webRootPath ?? throw new ArgumentNullException(nameof(webRootPath));
+ }
+
+ /// <summary>
+ /// Get the versioned path for a content file.
+ /// </summary>
+ /// <param name="contentPath">Content path to the file.</param>
+ /// <returns>Path with version suffix added.</returns>
+ public string GetVersionedPath(string contentPath)
+ {
+ if (!_hashCache.TryGetValue(contentPath, out var version))
+ {
+ var serverPath = Path.Combine(_webRootPath, contentPath.TrimStart('/'));
+ version = GetVersionHashFrom(serverPath);
+ _hashCache[contentPath] = version;
+ }
+
+ return $"{contentPath}?v={version}";
+ }
+
+ /// <summary>
+ /// Calculate the version hash for a file.
+ /// </summary>
+ /// <param name="serverPath">Path to the file on the server.</param>
+ /// <returns>MD5 hash.</returns>
+ private string GetVersionHashFrom(string serverPath)
+ {
+ using var md5 = MD5.Create();
+ using var stream = File.OpenRead(serverPath);
+ byte[] hash = md5.ComputeHash(stream);
+ return BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
+ }
+}