Enable hot reloading for Blazor inside an iframe #51006
Merged
+26
−0
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.
Fixes hot reload functionality for Blazor applications running inside iframes by allowing
Sec-Fetch-Dest: frameandSec-Fetch-Dest: iframerequests to trigger browser refresh script injection.Problem
Hot Reload on Blazor apps doesn't work inside iframes because the
BrowserRefreshMiddleware.IsBrowserDocumentRequest()method only accepts requests withSec-Fetch-Dest: document. Requests from iframes useSec-Fetch-Dest: frameorSec-Fetch-Dest: iframe, causing the middleware to reject them and skip script injection.This prevents hot reload from working in scenarios like:
Solution
Updated the
IsBrowserDocumentRequestmethod to also accept "frame" and "iframe" values for theSec-Fetch-Destheader, in addition to the existing "document" value. This allows the browser refresh middleware to inject hot reload scripts into iframe-embedded Blazor applications.The change is minimal and surgical - it only adds two additional string comparisons to the existing validation logic without affecting any other functionality.
Testing
truewhile other values like "serviceworker" still returnfalseAddresses the "escape hatch" API request mentioned in the original issue for enabling hot reloading in iframe scenarios.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.