lisaogren / axios-cache-adapter

Caching adapter for axios. Store request results in a configurable store to prevent unneeded network requests.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cache forever, If the header of server response has no `cache-control`

ttkien opened this issue · comments

If the header of server response has no cache-control field, the response will be cached forever. Because the expires field will be undefined.

My suggestion is should treat the cases as maxAge:0 (no-cache)

axios-cache-adapter version: 2.5.0

Same problem. cache age becomes uncontrolled

async function write (config, req, res) {
  try {
    const entry = {
      expires: config.expires || Date.now(), // "|| Date.now()" can help
      data: serialize(config, req, res)
    }

Same problem. cache age becomes uncontrolled


async function write (config, req, res) {

  try {

    const entry = {

      expires: config.expires || Date.now(), // "|| Date.now()" can help

      data: serialize(config, req, res)

    }

@DmitriyNikolenko I created Pull Request here. Please help me approve or vote it #169

+1 experiencing this issue.

My temp work around for anyone else depending on this lib:

// A response may be stored without an expiry if it's reading headers but none are present
// Use this custom invalidate to fix that issue until the related PR is merged
// https://github.com/RasCarlito/axios-cache-adapter/issues/168
const invalidate = async (config: any, request: AxiosRequestConfig): Promise<void> => {
  if(isUndefined(request.url) || isUndefined(request.method)) {
    return;
  }
  const method = request.method.toLowerCase();
  const item = await config.store.getItem(request.url);

  const shouldInvalidate = method !== "get" || (!isNull(item) && isUndefined(item.expires));
  if (!shouldInvalidate) {
    return;
  }

  await config.store.removeItem(config.uuid);
}