Reload on all redirects to handle Cloudflare Access auth expiration (#4706)

* Reload on all redirects

* Update index.ts

* Create release notes

* Update index.ts

* Update index.ts

* Update 4706.md

---------

Co-authored-by: Roger Goldfinger <roger@remind101.com>
Co-authored-by: Matt Fiddaman <github@m.fiddaman.uk>
This commit is contained in:
Roger Goldfinger
2025-04-16 17:26:29 -07:00
committed by GitHub
parent c85769eb82
commit 2f5464c735
2 changed files with 14 additions and 7 deletions

View File

@@ -2,17 +2,18 @@ import * as connection from '../connection';
export const fetch = async (
input: RequestInfo | URL,
options?: RequestInit,
options: RequestInit = {},
): Promise<Response> => {
// Set redirect to manual so that we can detect and respond to redirects.
if (!options.redirect) options.redirect = 'manual';
const response = await globalThis.fetch(input, options);
// Detect if the API query has been redirected to a different origin. This may indicate that the
// request has been intercepted by an authentication proxy
const originalUrl = new URL(input instanceof Request ? input.url : input);
const responseUrl = new URL(response.url);
if (response.redirected && responseUrl.host !== originalUrl.host) {
// Authentication proxies redirect when authentication has expired. In this case,
// we want to fully reload and yeild control from the service worker back to the server.
if (response.type === 'opaqueredirect') {
connection.send('api-fetch-redirected');
throw new Error(`API request redirected to ${responseUrl.host}`);
throw new Error(`API request redirected`);
}
return response;

View File

@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [rgoldfinger]
---
Reload on all redirects to handle Cloudflare Access expiration. This fixes issue #4422