Skip to content

[wasm] HotReload script consolidation#54142

Draft
maraf wants to merge 3 commits into
mainfrom
maraf/WasmHotReloadScriptConsolidation
Draft

[wasm] HotReload script consolidation#54142
maraf wants to merge 3 commits into
mainfrom
maraf/WasmHotReloadScriptConsolidation

Conversation

@maraf
Copy link
Copy Markdown
Member

@maraf maraf commented Apr 29, 2026

Summary

Consolidates hot reload WebSocket communication for Blazor WebAssembly by embedding the logic from WebSocketScriptInjection.js directly into the WASM lib.module.js JavaScript initializer. This eliminates the need for ScriptInjectingStream to inject a <script> tag into HTML responses for .NET 11+ WASM apps.

Changes

New config endpoint (/_framework/browser-refresh-config)

  • Returns JSON {webSocketUrls, serverKey} read from existing middleware env vars
  • Allows lib.module.js to discover WebSocket connection parameters without script injection

Embed WebSocket logic into lib.module.js

  • Ported all handlers from WebSocketScriptInjection.js: reload, wait, CSS updates, managed code updates, diagnostics, capabilities, browser refresh, toast notifications
  • Fetches config endpoint in onRuntimeConfigLoaded; establishes WebSocket in onRuntimeReady
  • Maintains backward compatibility: falls back to detecting injected script tag for hosted scenarios

Disable script injection for .NET 11+ WASM

  • New ASPNETCORE_AUTO_RELOAD_SUPPRESS_SCRIPT_INJECTION env var
  • WebAssemblyHotReloadClient sets it when project TFM >= 11.0
  • BrowserRefreshMiddleware skips HTML response interception when set
  • Avoids the overhead of ScriptInjectingStream for WASM apps

Backward compatibility

  • ScriptInjectingStream.cs and WebSocketScriptInjection.js remain untouched for non-WASM and pre-.NET 11 scenarios
  • Sentinel-based double-connection prevention (_dotnet_watch_ws_injected) ensures only one WebSocket connection when both mechanisms are present
  • If config endpoint fetch fails, lib.module.js still enables Blazor hooks via injected script tag detection

Testing

  • 3 new unit tests for the config endpoint and script injection suppression
  • All 113 existing middleware tests pass

maraf and others added 2 commits April 29, 2026 08:47
…ingStream

For Blazor WebAssembly, the hot reload WebSocket communication logic from
WebSocketScriptInjection.js is now embedded directly into the lib.module.js
JavaScript initializer. This eliminates the need for ScriptInjectingStream
to inject a <script> tag into HTML responses for WASM scenarios.

Changes:
- Add /_framework/browser-refresh-config JSON endpoint that returns the
  WebSocket URL and server key (read from existing env vars)
- Port all WebSocket handlers from WebSocketScriptInjection.js into
  lib.module.js (reload, CSS updates, managed code updates, diagnostics,
  capabilities, browser refresh, toast notifications)
- Maintain backward compatibility: if config endpoint is unavailable,
  falls back to detecting the injected script tag (hosted scenarios)
- Sentinel-based double-connection prevention between lib.module.js and
  the injected script, with fallback clearing on connection failure
- Add tests for the new config endpoint

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
For WebAssembly apps targeting .NET 11+, the lib.module.js handles
the browser refresh WebSocket connection directly, making the injected
<script> tag redundant. This change:

- Adds ASPNETCORE_AUTO_RELOAD_SUPPRESS_SCRIPT_INJECTION env var
- WebAssemblyHotReloadClient sets it for .NET 11+ projects
- BrowserRefreshMiddleware skips HTML response interception when set
- Avoids the overhead of ScriptInjectingStream for WASM apps

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, StaticWebAssetsSDK label Apr 29, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Thanks for your PR, @@maraf.
To learn about the PR process and branching schedule of this repo, please take a look at the SDK PR Guide.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, StaticWebAssetsSDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant