From 5468c9be4167e0ece6374415e8a029d086f53cc3 Mon Sep 17 00:00:00 2001 From: Mackinnon Buck Date: Thu, 5 Dec 2024 13:11:51 -0800 Subject: [PATCH] Don't cache index.html during development (#59340) --- .../WebAssembly/DevServer/src/Server/Startup.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index 5125c59a74af..dd5ef53cab2b 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Net.Http.Headers; namespace Microsoft.AspNetCore.Components.WebAssembly.DevServer.Server; @@ -65,6 +66,14 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati { OnPrepareResponse = fileContext => { + // Avoid caching index.html during development. + // When hot reload is enabled, a middleware injects a hot reload script into the response HTML. + // We don't want the browser to bypass this injection by using a cached response that doesn't + // contain the injected script. In the future, if script injection is removed in favor of a + // different mechanism, we can delete this comment and the line below it. + // See also: https://github.com/dotnet/aspnetcore/issues/45213 + fileContext.Context.Response.Headers[HeaderNames.CacheControl] = "no-store"; + if (applyCopHeaders) { // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.