diff --git a/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs b/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs index 6fb5c203..ac4d9bba 100644 --- a/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs +++ b/code/backend/Cleanuparr.Api/DependencyInjection/ApiDI.cs @@ -5,6 +5,7 @@ using Cleanuparr.Infrastructure.Hubs; using Cleanuparr.Infrastructure.Logging; using Microsoft.AspNetCore.Http.Json; using Microsoft.OpenApi.Models; +using System.Text; namespace Cleanuparr.Api.DependencyInjection; @@ -100,8 +101,44 @@ public static class ApiDI app.UseAuthorization(); app.MapControllers(); - // SPA fallback - must be last - app.MapFallbackToFile("index.html"); + // Custom SPA fallback to inject base path + app.MapFallback(async context => + { + var basePath = app.Configuration.GetValue("BASE_PATH") ?? "/"; + + // Normalize the base path (remove trailing slash if not root) + if (basePath != "/" && basePath.EndsWith("/")) + { + basePath = basePath.TrimEnd('/'); + } + + var webRoot = app.Environment.WebRootPath ?? Path.Combine(app.Environment.ContentRootPath, "wwwroot"); + var indexPath = Path.Combine(webRoot, "index.html"); + + if (!File.Exists(indexPath)) + { + context.Response.StatusCode = 404; + await context.Response.WriteAsync("index.html not found"); + return; + } + + var indexContent = await File.ReadAllTextAsync(indexPath); + + // Inject the base path into the HTML + var scriptInjection = $@" + "; + + // Insert the script right before the existing script tag + indexContent = indexContent.Replace( + "