Skip to content

Commit

Permalink
fix(headerinterpreter): use custom interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
seaerchin committed May 18, 2023
1 parent ae3f22f commit 78928ef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/services/api/AxiosInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import logger from "@logger/logger"
import { getAccessToken } from "@utils/token-retrieval-utils"
import tracer from "@utils/tracer"

import { headerInterpreter } from "@root/utils/headerInterpreter"

// Env vars
const GITHUB_ORG_NAME = config.get("github.orgName")

Expand Down Expand Up @@ -79,6 +81,7 @@ const isomerRepoAxiosInstance = setupCache(
{
interpretHeader: true,
etag: true,
headerInterpreter,
}
)
isomerRepoAxiosInstance.interceptors.request.use(requestFormatter)
Expand Down
47 changes: 47 additions & 0 deletions src/utils/headerInterpreter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Header, HeadersInterpreter } from "axios-cache-interceptor"
import { parse } from "cache-parser"

// NOTE: Taken with reference to https://github.com/arthurfiorette/axios-cache-interceptor/blob/v0.9.2/src/header/interpreter.ts
// The change made is to remove the `maxAge` condition, which is not suitable because
// we want to revalidate on update.
// An alternative is to invalidate the cache on `update` but
// this requires an exhaustive search through our codebase.
// This incurs an extra network call on every update, which is not ideal but in 304
// this is an empty request.
// eslint-disable-next-line import/prefer-default-export
export const headerInterpreter: HeadersInterpreter = (headers) => {
if (!headers) return "not enough headers"

const cacheControl = headers[Header.CacheControl]

if (cacheControl) {
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(
String(cacheControl)
)

// Header told that this response should not be cached.
if (noCache || noStore) {
return "dont cache"
}

if (immutable) {
// 1 year is sufficient, as Infinity may cause more problems.
// It might not be the best way, but a year is better than none.
return 1000 * 60 * 60 * 24 * 365
}

// Already out of date, for cache can be saved, but must be requested again
if (mustRevalidate || maxAge) {
return 0
}
}

const expires = headers[Header.Expires]

if (expires) {
const milliseconds = Date.parse(String(expires)) - Date.now()
return milliseconds >= 0 ? milliseconds : "dont cache"
}

return "not enough headers"
}

0 comments on commit 78928ef

Please sign in to comment.