Next.js Discord

Discord Forum

why this cache dont work?

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Answered by Ray
because it only works in app router
View full answer

31 Replies

@Asiatic Lion Click to see attachment
because it only works in app router
Answer
@Ray because it only works in app router
Asiatic LionOP
oh
How can I have the same effect in getServerSideProps?
@Ray ISR?
Asiatic LionOP
what is isr?
export async function getStaticProps() {
  // If this request throws an uncaught error, Next.js will
  // not invalidate the currently shown page and
  // retry getStaticProps on the next request.
  const res = await fetch('https://.../posts')
  const posts = await res.json()
 
  if (!res.ok) {
    // If there is a server error, you might want to
    // throw an error instead of returning so that the cache is not updated
    // until the next successful request.
    throw new Error(`Failed to fetch posts, received status ${res.status}`)
  }
 
  // If the request was successful, return the posts
  // and revalidate every 10 seconds.
  return {
    props: {
      posts,
    },
    revalidate: 10,
  }
}
no
I have a guild and user request
I imagine that by putting this in place the user will be the same for whoever accesses it, which is a problem
I need only the server fetch to have cache
either use app router or use redis
@Ray use `cache-control` header instead if you want to use `getServerSideProps` https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props#caching-with-server-side-rendering-ssr
Asiatic LionOP
export async function getServerSideProps({ query, req, res }) {

  res.setHeader(
    'Cache-Control',
    'public, s-maxage=600, stale-while-revalidate=1000'
  )

  try {
    const request = await nodefetch(`${settings.apiURL}/guilds/${query.guildId}`);
    if (request.status != 200) return {
      props: {
        error: true
      }
    }

    const response = await request.json();

    return {
      props: {
        guild: response,
      }
    }
  } catch (e) {
    return {
      props: {
        apierror: true
      }
    }
  }

}
I put this, but dont work
why?
@Asiatic Lion Click to see attachment
are you on dev mode?
@Ray are you on dev mode?
Asiatic LionOP
no
i commit to vercel
domain
don't see you have correct header
@Ray don't see you have correct header
Asiatic LionOP
yeah, but is here 😢
@Asiatic Lion Click to see attachment
you render the date on page?
Asiatic LionOP
no
from api
the api send the date
build your app locally and check the header
@Ray build your app locally and check the header
Asiatic LionOP
I managed to solve the problem, mysteriously it started working
I have no idea what it was