Skip to content
Snippets Groups Projects
Unverified Commit fe17c6ba authored by Tim Quelch's avatar Tim Quelch Committed by GitHub
Browse files

Forcibly reload app from server when API is redirected (#3286)


* Add advanced setting to reload app from server

* Add change notes

* Automatically reload when API call is redirected

* Error on redirect so we don't use the response

* Remove setting to reload

* Update release notes

---------

Co-authored-by: default avataryoungcw <calebyoung94@gmail.com>
parent 3a9a929f
No related branches found
No related tags found
No related merge requests found
...@@ -51,6 +51,22 @@ global.Actual = { ...@@ -51,6 +51,22 @@ global.Actual = {
window.location.reload(); window.location.reload();
}, },
reload: () => {
if (window.navigator.serviceWorker == null) return;
// Unregister the service worker handling routing and then reload. This should force the reload
// to query the actual server rather than delegating to the worker
return window.navigator.serviceWorker
.getRegistration('/')
.then(registration => {
if (registration == null) return;
return registration.unregister();
})
.then(() => {
window.location.reload();
});
},
restartElectronServer: () => {}, restartElectronServer: () => {},
openFileDialog: async ({ filters = [] }) => { openFileDialog: async ({ filters = [] }) => {
......
...@@ -142,4 +142,8 @@ export function handleGlobalEvents(actions: BoundActions, store: Store<State>) { ...@@ -142,4 +142,8 @@ export function handleGlobalEvents(actions: BoundActions, store: Store<State>) {
actions.closeBudgetUI(); actions.closeBudgetUI();
actions.setAppState({ loadingText: null }); actions.setAppState({ loadingText: null });
}); });
listen('api-fetch-redirected', () => {
actions.reloadApp();
});
} }
...@@ -46,3 +46,9 @@ export function focused() { ...@@ -46,3 +46,9 @@ export function focused() {
return send('app-focused'); return send('app-focused');
}; };
} }
export function reloadApp() {
return () => {
global.Actual.reload();
};
}
export const fetch = globalThis.fetch; import * as connection from '../connection';
export const fetch = async (
input: RequestInfo | URL,
options?: RequestInit,
): Promise<Response> => {
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) {
connection.send('api-fetch-redirected');
throw new Error(`API request redirected to ${responseUrl.host}`);
}
return response;
};
...@@ -18,4 +18,5 @@ export interface ServerEvents { ...@@ -18,4 +18,5 @@ export interface ServerEvents {
'start-load': unknown; 'start-load': unknown;
'sync-event': { type; subtype; meta; tables; syncDisabled }; 'sync-event': { type; subtype; meta; tables; syncDisabled };
'undo-event': UndoState; 'undo-event': UndoState;
'api-fetch-redirected': unknown;
} }
...@@ -15,6 +15,7 @@ declare global { ...@@ -15,6 +15,7 @@ declare global {
opts: Parameters<import('electron').Dialog['showOpenDialogSync']>[0], opts: Parameters<import('electron').Dialog['showOpenDialogSync']>[0],
) => Promise<string[]>; ) => Promise<string[]>;
relaunch: () => void; relaunch: () => void;
reload: (() => Promise<void>) | undefined;
restartElectronServer: () => void; restartElectronServer: () => void;
}; };
......
---
category: Bugfix
authors: [TimQuelch]
---
Forcibly reload app when API request is redirected. This fixes issue #2793
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment