I am currently developing an application using nextjs that retrieves data from a firebase firestore. The issue I am facing is that the application makes these requests on every page load, even when the data does not need to be completely up to date. To address this, I am looking to incorporate some form of caching mechanism for certain pages.
I initially tried utilizing the node-cache
package, but it appears that each new HTTP request clears the cache since it is considered a new environment. Additionally, I explored adding cache headers in the getServerSideProps()
function, but it did not work as expected because the getPropertyPreviewData()
function was still being called on every page request.
At this point, I am uncertain if there is something essential that I am overlooking or if there might be a more straightforward solution that has eluded me. Any assistance on this matter would be greatly appreciated.
export const getServerSideProps = async (context) => {
const { res } = context;
res.setHeader(
'Cache-Control',
'public, s-maxage=600, stale-while-revalidate=1200'
);
const properties = await getPropertyPreviewData();
return ({
props: {
properties
}
});
};