Skip to content

Commit

Permalink
fix: cache not invalidated on API POST
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai committed Nov 16, 2024
1 parent faca3d5 commit aef560e
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions app/utils/useFetchCacheControlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,25 @@ export function useFetchCacheControlHelper() {
const isInitialRequest = ref(true);
const forceRefreshTargets = ref<InvalidateTarget[]>([]);

const onRequest = (context: FetchContext): void => {
const onRequest = ({ options }: FetchContext): void => {
if (isInitialRequest.value) {
isInitialRequest.value = false;
return;
}

const headers = new Headers(
typeof context.request === "object" ? context.request.headers : {}
);

if (forceRefreshTargets.value.length) {
headers.set(
options.headers.set(
"luonto-invalidate-cache",
forceRefreshTargets.value.join(", ")
);
forceRefreshTargets.value = [];
} else {
// for scheduled updates, get the latest results, even if it takes longer, since they have already been rendered and user interaction is not relevant
// if caching is disabled for certain APIs (that is, due to user interaction), allow stale responses for other APIs to get results quickly
headers.set("luonto-no-stale-cache", "?1");
options.headers.set("luonto-no-stale-cache", "?1");
}

context.request = new Request(context.request, {
cache: "reload",
headers,
});
options.cache = "reload";
};

return {
Expand Down

0 comments on commit aef560e

Please sign in to comment.