Skip to content

Commit b5f4e70

Browse files
committed
feat: axios-cache-interceptor
Signed-off-by: Adam Setch <[email protected]>
1 parent b931935 commit b5f4e70

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/renderer/components/Sidebar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { APPLICATION } from '../../shared/constants';
1616

1717
import { Constants } from '../constants';
1818
import { AppContext } from '../context/App';
19-
import { clearApiCache } from '../utils/api/request';
19+
import { clearFullApiCache } from '../utils/api/request';
2020
import { quitApp } from '../utils/comms';
2121
import {
2222
openGitHubIssues,
@@ -65,7 +65,7 @@ export const Sidebar: FC = () => {
6565
navigate('/', { replace: true });
6666

6767
// Clear client cache so we fetch fresh data when user manually refreshes
68-
clearApiCache();
68+
clearFullApiCache();
6969
fetchNotifications();
7070
};
7171

src/renderer/context/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type {
2828
import { FetchType } from '../types';
2929
import type { Notification } from '../typesGitHub';
3030
import { headNotifications } from '../utils/api/client';
31-
import { clearApiCache } from '../utils/api/request';
31+
import { clearFullApiCache } from '../utils/api/request';
3232
import type {
3333
LoginOAuthAppOptions,
3434
LoginPersonalAccessTokenOptions,
@@ -136,7 +136,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
136136

137137
// biome-ignore lint/correctness/useExhaustiveDependencies: We only want fetchNotifications to be called for particular state changes
138138
useEffect(() => {
139-
clearApiCache();
139+
clearFullApiCache();
140140
fetchNotifications({ auth, settings });
141141
}, [
142142
auth.accounts,

src/renderer/utils/api/request.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@ import { buildKeyGenerator, setupCache } from 'axios-cache-interceptor';
88

99
import type { Account, Link, Token } from '../../types';
1010
import { decryptValue } from '../comms';
11-
import { rendererLogError } from '../logger';
11+
import { rendererLogError } from '../logger';
1212
import { getNextURLFromLinkHeader } from './utils';
1313

1414
type AxiosRequestConfigWithAccount = AxiosRequestConfig & { account: Account };
1515

16+
// const MUTATION_HTTP_METHODS: Set<Method> = new Set(['PATCH', 'PUT', 'DELETE']);
17+
1618
const instance = Axios.create();
1719
const axios = setupCache(instance, {
1820
location: 'client',
1921

22+
// Respect ETags and cache headers from GitHub API
23+
interpretHeader: true,
24+
25+
// Set a reasonable TTL to ensure cache freshness (60 seconds)
26+
// This ensures external changes (GitHub web/mobile) are picked up periodically
27+
// ttl: 1000 * 60, // 60 seconds
28+
2029
cachePredicate: {
21-
ignoreUrls: [
22-
'/login/oauth/access_token',
23-
// '/notifications',
24-
// '/api/v3/notifications',
25-
],
30+
ignoreUrls: ['/login/oauth/access_token'],
2631
},
2732

33+
methods: ['get'],
34+
2835
generateKey: buildKeyGenerator((request: AxiosRequestConfigWithAccount) => {
2936
return {
3037
method: request.method,
@@ -34,6 +41,24 @@ const axios = setupCache(instance, {
3441
}),
3542
});
3643

44+
// Invalidate cache on mutating requests (PATCH, PUT, DELETE)
45+
// Only clears cache entries for the same account that made the mutation
46+
// axios.interceptors.response.use(
47+
// async (response) => {
48+
// const method = response.config.method?.toUpperCase() as Method;
49+
// const config = response.config as AxiosRequestConfigWithAccount;
50+
51+
// if (MUTATION_HTTP_METHODS.has(method) && config.account) {
52+
// await clearFullApiCache();
53+
// }
54+
// return response;
55+
// },
56+
// (error: Error) => {
57+
// // Pass through errors without clearing cache
58+
// return Promise.reject(error);
59+
// },
60+
// );
61+
3762
/**
3863
* Perform an unauthenticated API request
3964
*
@@ -139,10 +164,10 @@ async function getHeaders(token?: Token) {
139164
return headers;
140165
}
141166

142-
export async function clearApiCache(): Promise<void> {
143-
try {
144-
axios.storage.clear();
145-
} catch (err) {
146-
rendererLogError('clearApiCache', 'Failed to clear API cache', err);
147-
}
167+
export async function clearFullApiCache(): Promise<void> {
168+
// try {
169+
// axios.storage.clear();
170+
// } catch (err) {
171+
// rendererLogError('clearFullApiCache', 'Failed to clear API cache', err);
172+
// }
148173
}

0 commit comments

Comments
 (0)