diff --git a/web/src/helpers/api.js b/web/src/helpers/api.js index eb70cd27..aef01287 100644 --- a/web/src/helpers/api.js +++ b/web/src/helpers/api.js @@ -12,6 +12,36 @@ export let API = axios.create({ }, }); +function patchAPIInstance(instance) { + const originalGet = instance.get.bind(instance); + const inFlightGetRequests = new Map(); + + const genKey = (url, config = {}) => { + const params = config.params ? JSON.stringify(config.params) : '{}'; + return `${url}?${params}`; + }; + + instance.get = (url, config = {}) => { + if (config?.disableDuplicate) { + return originalGet(url, config); + } + + const key = genKey(url, config); + if (inFlightGetRequests.has(key)) { + return inFlightGetRequests.get(key); + } + + const reqPromise = originalGet(url, config).finally(() => { + inFlightGetRequests.delete(key); + }); + + inFlightGetRequests.set(key, reqPromise); + return reqPromise; + }; +} + +patchAPIInstance(API); + export function updateAPI() { API = axios.create({ baseURL: import.meta.env.VITE_REACT_APP_SERVER_URL @@ -22,6 +52,8 @@ export function updateAPI() { 'Cache-Control': 'no-store', }, }); + + patchAPIInstance(API); } API.interceptors.response.use(