diff --git a/service/http-config/axios-interceptor-instance.ts b/service/http-config/axios-interceptor-instance.ts index cbcfa878..d2a6be3d 100644 --- a/service/http-config/axios-interceptor-instance.ts +++ b/service/http-config/axios-interceptor-instance.ts @@ -6,6 +6,13 @@ const baseURL = "https://mediahub.polri.go.id/api/v2/"; const refreshToken = Cookies.get("refresh_token"); +// Helper function to append timestamp to URL to prevent caching +function addTimestampToUrl(url: string): string { + const separator = url.includes('?') ? '&' : '?'; + const timestamp = Date.now(); + return `${url}${separator}timemilis=${timestamp}`; +} + const axiosInterceptorInstance = axios.create({ baseURL, headers: { @@ -22,6 +29,12 @@ axiosInterceptorInstance.interceptors.request.use( if (config.headers) config.headers.Authorization = "Bearer " + accessToken; } + + // Add timestamp to URL to prevent caching + if (config.url) { + config.url = addTimestampToUrl(config.url); + } + return config; }, (error) => { diff --git a/service/http-config/http-base-service.ts b/service/http-config/http-base-service.ts index d2b191f0..2123511d 100644 --- a/service/http-config/http-base-service.ts +++ b/service/http-config/http-base-service.ts @@ -9,6 +9,13 @@ let csrfTokenCache: string | null = null; let csrfTokenExpiry: number = 0; const CSRF_CACHE_DURATION = 5 * 60 * 1000; // 5 minutes +// Helper function to append timestamp to URL to prevent caching +function addTimestampToUrl(url: string): string { + const separator = url.includes('?') ? '&' : '?'; + const timestamp = Date.now(); + return `${url}${separator}timemilis=${timestamp}`; +} + async function getCachedCsrfToken() { const now = Date.now(); @@ -51,8 +58,11 @@ export async function httpPost(pathUrl: any, data?: any, headers?: any,) { ...headers, }; + // Add timestamp to URL to prevent caching + const urlWithTimestamp = addTimestampToUrl(pathUrl); + const response = await axiosBaseInstance - .post(pathUrl, data, { headers: mergedHeaders }) + .post(urlWithTimestamp, data, { headers: mergedHeaders }) .catch(function (error: any) { console.log(error); return error.response; @@ -74,8 +84,11 @@ export async function httpPost(pathUrl: any, data?: any, headers?: any,) { } export async function httpGet(pathUrl: any, headers: any) { + // Add timestamp to URL to prevent caching + const urlWithTimestamp = addTimestampToUrl(pathUrl); + const response = await axiosBaseInstance - .get(pathUrl, { headers }) + .get(urlWithTimestamp, { headers }) .catch(function (error: any) { console.log(error); return error.response; @@ -110,8 +123,11 @@ export async function httpPut(pathUrl: any, headers: any, data?: any) { ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), }; + // Add timestamp to URL to prevent caching + const urlWithTimestamp = addTimestampToUrl(pathUrl); + const response = await axiosBaseInstance - .put(pathUrl, data, { headers: mergedHeaders }) + .put(urlWithTimestamp, data, { headers: mergedHeaders }) .catch(function (error: any) { console.log(error); return error.response;