Code Monkey home page Code Monkey logo

Comments (2)

zecka avatar zecka commented on August 19, 2024

I found a temp workaround that loose background cache rebuild for 404.

I create a custom cache handler that return null instead stale value for 404 page:

I create a "temp-fix" branch on my repo: https://github.com/zeckaissue/nextjs-cache-404-status-update/tree/temp-fix

Here is the cache handler get method

  async get(key) {
    // This could be stored anywhere, like durable storage
    const data =  cache.get(key)
    if(data?.value?.kind === 'PAGE' && data?.value?.status === 404 &&  data?.lastModified){
      let currentTime = Date.now();
      let cacheTime = parseInt(data.lastModified);
      let deltaTime = Math.round((currentTime - cacheTime) / 1000);
      if (deltaTime > parseInt(data.revalidate || 0)) {
        cache.delete(key);
        return null  // <----- here we return null value instead stale value
      }
    }

    return data
  }
Here is the complete cache handler
const cache = new Map()
 
module.exports = class CacheHandler {
  constructor(options) {
    this.options = options
  }
 
  async get(key) {
    // This could be stored anywhere, like durable storage
    const data =  cache.get(key)
    if(data?.value?.kind === 'PAGE' && data?.value?.status === 404 &&  data?.lastModified){
      let currentTime = Date.now();
      let cacheTime = parseInt(data.lastModified);
      let deltaTime = Math.round((currentTime - cacheTime) / 1000);
      if (deltaTime > parseInt(data.revalidate || 0)) {
        cache.delete(key);
        return null // <----- here we return null value instead stale value
      }
    }

    return data
  }
 
  async set(key, data, ctx) {
    // This could be stored anywhere, like durable storage
    cache.set(key, {
      value: data,
      lastModified: Date.now(),
      tags: ctx.tags,
      ravalidate: ctx.revalidate,
    })
  }
 
  async revalidateTag(tag) {
    // Iterate over all entries in the cache
    for (let [key, value] of cache) {
      // If the value's tags include the specified tag, delete this entry
      if (value.tags.includes(tag)) {
        cache.delete(key)
      }
    }
  }
}

from next.js.

albert-dev1 avatar albert-dev1 commented on August 19, 2024

I am facing the same issue. Tried to reproduce this bug with 15.0.0-canary.115 and it seems to be fixed there.
Unfortunately there is an unexpected behavior with 5xx response codes. In this case I would expect that stale cache data is used by nextjs from the cache.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.