Last active
February 21, 2023 09:03
-
-
Save Kola92/d567e9ef15e213d6cb79c76620419245 to your computer and use it in GitHub Desktop.
Data Fetching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This request should be cached until manually invalidated. | |
// Similar to `getStaticProps`. | |
// `force-cache` is the default and can be omitted. | |
// This is how deduplication is activated, though it is enabled by default | |
async function fetchData() { | |
const res = await fetch("https://reqres.in/api/users", { | |
cache: "force-cache", | |
}); | |
return res.json(); | |
} | |
// This request should be refetched on every request. | |
// Similar to `getServerSideProps`. | |
// Removing deduplication | |
async function fetchData() { | |
const res = await fetch("https://reqres.in/api/users", { cache: "no-store" }); | |
return res.json(); | |
} | |
// This request should be cached with a lifetime of 10 seconds. | |
// Similar to `getStaticProps` with the `revalidate` option. | |
async function fetchData() { | |
const res = await fetch("https://reqres.in/api/users", { | |
next: { revalidate: 10 }, | |
}); | |
return res.json(); | |
} | |
// This is an async Server Component | |
export default async function Page() { | |
const data = await fetchData(); | |
return <main>{/* Content */}</main>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment