Fix Blazor PWA service worker to handle redirected cached responses#64612
Merged
Conversation
Handle redirected responses in onFetch by creating a new Response object without the redirected flag. This fixes issues with hosts like Cloudflare Pages that redirect index.html requests. Fixes #33872 Co-authored-by: javiercn <[email protected]>
Address code review feedback by cloning the cached response before creating a new Response object, ensuring the body can be safely read. Co-authored-by: javiercn <[email protected]>
Copilot
AI
changed the title
[WIP] Fix Blazor PWA issue on Cloudflare Pages due to index.html redirect
Fix Blazor PWA service worker to handle redirected cached responses
Dec 2, 2025
javiercn
reviewed
Dec 3, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical issue where Blazor PWA applications fail to load after the first visit when hosted on platforms like Cloudflare Pages that redirect index.html requests. The service worker caches these redirected responses, but browsers block returning redirected responses to navigation requests due to security policies, causing the application to break.
Key Changes:
- Added redirect handling logic to strip the
redirectedflag from cached responses by constructing a newResponseobject - The fix preserves essential response properties (body, headers, status, statusText) while removing the problematic redirected state
ilonatommy
approved these changes
Dec 4, 2025
CorruptComputer
added a commit
to CorruptComputer/home
that referenced
this pull request
Dec 31, 2025
CorruptComputer
added a commit
to CorruptComputer/BlazorWasmOsmOauth
that referenced
this pull request
Dec 31, 2025
…st, and move code workspace to repo root
CorruptComputer
added a commit
to CorruptComputer/Bones
that referenced
this pull request
Jan 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Handle redirected cached responses in PWA service worker to fix Cloudflare Pages hosting
Description
Blazor PWA breaks on hosts like Cloudflare Pages that redirect
index.htmlrequests. The service worker caches these responses withredirected=true, but browsers block returning redirected responses to navigation requests (which haveredirect: "manual"mode) per security policy.Changes:
service-worker.published.js, strip theredirectedflag from cached responses by constructing a newResponseobject:Fixes #33872
Original prompt
This section details on the original issue you should resolve
<issue_title>Blazor PWA breaks if hosted on Cloudflare Pages due to redirected index.html</issue_title>
<issue_description>Brief Description
An attempt to host a Blazor PWA on Cloudflare Pages results in broken service-worker: the website won't load after the first visit, installed PWA will display an error on start. The root of the problem seems to be the redirect of
index.html. The problem may not be specific only to Cloudflare Pages, but to any host that redirects requests to the index page.Example
Cause of the problem

The root of the problem seems to be the
index.htmlredirect that happens when the service-worker attempts to fetch static resources for offline mode during its initialization phase:As a result
index.htmlresponse gets saved in CacheStorage withredirected=true.When a user attempts to visit the website for the second time, the service-worker's
onFetchreturns cached redirected response ofindex.hmtl. This causes error because of the security policy of modern browsers:Solution
As a workaround the code that removes the
redirectedflag from cached responses may be added to the service worker:Although I'm not sure it's 100% correct approach to do that, it seems to be working: https://fix.cloudflarebreaksblazorpwa.pages.dev/
Maybe something like this should be included in the Blazor PWA project template for .NET 6 to make it more robust and suitable for hosting on Cloudflare Pages out of the box.</issue_description>
Comments on the Issue (you are @copilot in this section)
@ We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our [Triage Process](https://github.com/dotnet/aspnetcore/blob/master/docs/TriageProcess.md). @danroth27 Here's another example of a user that hit this issue: https://github.com//discussions/54830@SteveSandersonMS @mkArtakMSFT Should we consider reopening this issue? It was closed based on the current lack of support for PWAs for Blazor Web Apps in .NET 8, but we eventually hope to add PWA support and we still support building PWAs with Standalone Blazor WebAssembly apps. </c...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.