Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcibly reload app from server when API is redirected #3286

Merged
merged 15 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/desktop-client/src/browser-preload.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ global.Actual = {
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: () => {},

openFileDialog: async ({ filters = [] }) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/desktop-client/src/global-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,8 @@ export function handleGlobalEvents(actions: BoundActions, store: Store<State>) {
actions.closeBudgetUI();
actions.setAppState({ loadingText: null });
});

listen('api-fetch-redirected', () => {
actions.reloadApp();
});
}
6 changes: 6 additions & 0 deletions packages/loot-core/src/client/actions/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ export function focused() {
return send('app-focused');
};
}

export function reloadApp() {
return () => {
global.Actual.reload();
};
}
20 changes: 19 additions & 1 deletion packages/loot-core/src/platform/server/fetch/index.web.ts
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
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;
};
1 change: 1 addition & 0 deletions packages/loot-core/src/types/server-events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface ServerEvents {
'start-load': unknown;
'sync-event': { type; subtype; meta; tables; syncDisabled };
'undo-event': UndoState;
'api-fetch-redirected': unknown;
}
1 change: 1 addition & 0 deletions packages/loot-core/typings/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare global {
opts: Parameters<import('electron').Dialog['showOpenDialogSync']>[0],
) => Promise<string[]>;
relaunch: () => void;
reload: (() => Promise<void>) | undefined;
restartElectronServer: () => void;
};

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3286.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [TimQuelch]
---

Forcibly reload app when API request is redirected. This fixes issue #2793
Loading