[release/8.0] [Blazor WASM standalone] Avoid caching index.html
during development
#59349
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Avoid caching
index.html
during developmentBackport of #59340
Fixes an issue where running a Blazor WebAssembly standalone app with hot reload enabled doesn't work if the app had been previously run with hot reload disabled.
Description
Background
The
Microsoft.AspNetCore.Components.WebAssembly.DevServer
package provides a server for running Blazor WebAssembly standalone apps in development, and it's what gets used by default in new Blazor WebAssembly standalone projects. One of its responsibilities is serving static files, and this includes the app'sindex.html
.Hot reload functionality depends on an additional
<script />
element being dynamically injected into the response forindex.html
. A development-only middleware provides this behavior.The problem
The development server was serving
index.html
with aCache-Control
header that allowed the browser to cache the response. This meant that if the app was initially run with hot reload disabled, the browser would cache a version ofindex.html
without the injected script. After enabling hot reload, the server would not detect that the cachedindex.html
was invalid, because the file on disk had not changed. As a result, the hot reload script injection middleware would be skipped, and the cached response (without the injected script) would be used. This prevented any hot reload functionality from working.The fix
The fix is simple: change the development server to serve
index.html
with aCache-Control: no-store
header, which prevents the browser from caching the response. This always gives the script injection middleware a chance to inject the hot reload script.Fixes #59276
Customer Impact
Customers relying on the dotnet CLI to run their Blazor WebAssembly standalone app are very likely to encounter this issue. The problem can be reproduced by first running the app using
dotnet run
, then subsequently withdotnet watch
. While less common, the bug can also be reproduced in Visual Studio by explicitly disabling hot reload, running the app, then re-enabling hot reload.Workarounds do exist:
index.html
so that the server recognizes that the file has changedHowever, the user may not be aware of these workarounds, and being forced to manually clear the browser cache creates a frustrating development experience.
Note
Only Blazor WebAssembly standalone apps are affected. The bug does not reproduce on other types of Blazor apps (Web, Server, Hybrid).
Regression?
This bug has existed since .NET 6, and possibly earlier than that.
Risk
The change is small and only impacts how the
index.html
file is served during development.Verification
Packaging changes reviewed?