#getServerSideProps
1 messages · Page 1 of 1 (latest)
Hi @distant folio. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js 14+, the traditional data fetching methods like getServerSideProps and getStaticProps used in the pages directory have been replaced with a simpler API built on top of fetch() and async React Server Components inside the app directory.
For example, the equivalent of getServerSideProps would be a fetch request with the cache option set to no-store. This indicates that the fetched data should never be cached and should be refetched on every request, similar to how getServerSideProps works.
Here's an example:
export default async function Page() {
// This request should be refetched on every request.
// Similar to `getServerSideProps`.
const dynamicData = await fetch(`<https://...`,> { cache: 'no-store' })
return <div>...</div>
}
You can find more information about this in the Next.js documentation (https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#step-6-migrating-data-fetching-methods).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@distant folio Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@winged nest So fetch requests are cached by default? Or do I have to enable it, I want heavy catching for fetch requests
Let me check the knowledge base for you again. Give me one moment! :books:
Yes, by default, Next.js automatically caches the returned values of fetch in the Data Cache on the server. This means that the data can be fetched at build time or request time, cached, and reused on each data request. Here's an example:
// 'force-cache' is the default, and can be omitted
fetch('<https://...',> { cache: 'force-cache' })
However, there are exceptions. fetch requests are not cached when:
- Used inside a Server Action.
- Used inside a Route Handler that uses the
POSTmethod.
You can also control the caching behavior using the fetchCache option in your layout or page. Here are some options:
'auto'(default): The default option to cachefetchrequests before dynamic functions with thecacheoption they provide and not cachefetchrequests after dynamic functions.'default-cache': Allow anycacheoption to be passed tofetchbut if no option is provided then set thecacheoption to'force-cache'. This means that evenfetchrequests after dynamic functions are considered static.'only-cache': Ensure allfetchrequests opt into caching by changing the default tocache: 'force-cache'if no option is provided and causing an error if anyfetchrequests usecache: 'no-store'.'force-cache': Ensure allfetchrequests opt into caching by setting thecacheoption of allfetchrequests to'force-cache'.