Une collection organisée de snippets de code pour accélérer votre développement. Parcourez, recherchez et copiez en un clic.
async function getData() {
const res = await fetch('...');
return res.json();
}
export default async function Page() {
const data = await getData();
return <main>{/* ... */}</main>;
}
fetch('https://...', { cache: 'no-store' });
fetch('https://...', { cache: 'force-cache' }); // Comportement par défaut
fetch('https://...', { next: { revalidate: 3600 } });
import useSWR from 'swr';
const fetcher = (...args) => fetch(...args).then(res => res.json());
function Profile() {
const { data, error } = useSWR('/api/user', fetcher);
// ...
}
export async function getServerSideProps(context) {
const res = await fetch(`...`);
const data = await res.json();
return { props: { data } };
}
export async function getStaticPaths() {
return {
paths: [{ params: { id: '1' } }, { params: { id: '2' } }],
fallback: false, // can also be true or 'blocking'
};
}
export async function getStaticProps(context) {
const res = await fetch(`...`);
const data = await res.json();
return {
props: { data }, // will be passed to the page component as props
};
}
export async function getStaticProps() {
const res = await fetch('...');
const data = await res.json();
return {
props: { data },
revalidate: 10, // Re-generate the page at most once every 10 seconds
};
}