Skip to content

Commit ad9df1a

Browse files
Copilotjaviercn
andauthored
[Blazor] Fix PWA service worker to handle redirected cached responses (#64612)
* Initial plan * Fix Blazor PWA service worker to handle redirected cached responses 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 <6995051+javiercn@users.noreply.github.com> * Clone response before extracting body for safer handling 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 <6995051+javiercn@users.noreply.github.com> * Apply suggestion from @javiercn * Apply suggestion from @javiercn --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com> Co-authored-by: Javier Calvarro Nelson <jacalvar@microsoft.com>
1 parent e150fbe commit ad9df1a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/ProjectTemplates/Web.ProjectTemplates/content/ComponentsWebAssembly-CSharp/wwwroot/service-worker.published.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,14 @@ async function onFetch(event) {
5151
cachedResponse = await cache.match(request);
5252
}
5353

54+
if (cachedResponse && cachedResponse.redirected) {
55+
const clonedResponse = cachedResponse.clone();
56+
cachedResponse = new Response(clonedResponse.body, {
57+
headers: clonedResponse.headers,
58+
status: clonedResponse.status,
59+
statusText: clonedResponse.statusText
60+
});
61+
}
62+
5463
return cachedResponse || fetch(event.request);
5564
}

0 commit comments

Comments
 (0)