Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  28] [ 4]  / answers: 1 / hits: 6686  / 2 Years ago, thu, march 3, 2022, 12:00:00

We have few pages and components as server side rendering.


We were trying to use cache for few API responses.


export async function getServerSideProps(context) {
const res = await getRequest(API.home)
return {
props: {
"home": res?.data?.result
},
}
}

Next.js version is 11.1.


Here can someone please suggest how can we implement cache?


More From » next.js

 Answers
2

You can set the Cache-Control header inside getServerSideProps using res.setHeader.


export async function getServerSideProps(context) {
// Add whatever `Cache-Control` value you want here
context.res.setHeader(
'Cache-Control',
'public, s-maxage=10, stale-while-revalidate=59'
)
const res = await getRequest(API.home)
return {
props: {
home: res?.data?.result
}
}
}

Setting a Cache-Control value only works in production mode, as the header will be overwritten in development mode.


See Caching with Server-Side Rendering documentation for more details.


[#308] Sunday, February 20, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayaw

Total Points: 749
Total Questions: 88
Total Answers: 86

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;