141
loading...
This website collects cookies to deliver better user experience
getStaticProps()
or getStaticPaths()
.// Next.js libraries
import Head from 'next/head'
// Custom Components
import BackToHome from 'components/BackToHome'
// Page component
export default function StaticSideGeneration({ jsonData }) {
return (
<>
<Head>
<title>Static-Site Generation (SSG) • Guy Dumais</title>
<meta name="description" content="Example page using Static-Site Generation (SSG) with Next.js 11 and React 17"/>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<BackToHome/>
<h1>Static-Site Generation (SSG)</h1>
<p>Data fetched at build-time on the server-side before sending to the client.</p>
<ul>
{
jsonData.data.map((e) => (
<li key={e.id}>{e.email}</li>
))
}
</ul>
</>
)
}
// This function gets called at build time on server-side.
// It won't be called on client-side, so you can even do
// direct database queries.
export async function getStaticProps() {
const res = await fetch('https://reqres.in/api/users?page=2')
const jsonData = await res.json()
return {
props: {
jsonData, // will be passed to the page component as props
},
}
}
getServerSideProps()
data fetching method.// Next.js libraries
import Head from 'next/head'
// Custom Components
import BackToHome from 'components/BackToHome'
// Page component
export default function ServerSideRendering({ jsonData }) {
return (
<>
<Head>
<title>Server-Side Rendering (SSR) • Guy Dumais</title>
<meta name="description" content="Example page using Server-Side Rendering (SSR) with Next.js 11 and React 17"/>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<BackToHome/>
<h1>Server-Side Rendering (SSR)</h1>
<p>Data fetched on the server-side at <b>each</b> request before sending to the client.</p>
<ul>
{
jsonData.data.map((e) => (
<li key={e.id}>{e.email}</li>
))
}
</ul>
</>
)
}
export async function getServerSideProps() {
const res = await fetch('https://reqres.in/api/users?page=2')
const jsonData = await res.json()
return {
props: {
jsonData, // will be passed to the page component as props
},
}
}
// Next.js libraries
import Head from 'next/head'
// Custom Components
import BackToHome from 'components/BackToHome'
// Page component
export default function IncrementalStaticGeneration({ jsonData }) {
return (
<>
<Head>
<title>Incremental Static Regeneration (ISR) • Guy Dumais</title>
<meta name="description" content="Example page using Incremental Static Regeneration (ISR) with Next.js 11 and React 17"/>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<BackToHome/>
<h1>Incremental Static Regeneration (ISR)</h1>
<p>Data fetched at build-time on the server-side and rebuilt when data updated.</p>
<ul>
{
jsonData.data.map((e) => (
<li key={e.id}>{e.email}</li>
))
}
</ul>
</>
)
}
// This function gets called at build time on server-side.
// It may be called again, on a serverless function, if
// revalidation is enabled and a new request comes in
export async function getStaticProps() {
const res = await fetch('https://reqres.in/api/users?page=2')
const jsonData = await res.json()
return {
props: {
jsonData, // will be passed to the page component as props
},
// Next.js will attempt to re-generate the page:
// - When a request comes in
// - At most once every second
revalidate: 100, // In seconds
}
}
// React
import { useEffect, useState } from 'react'
// Next.js
import Head from 'next/head'
import Link from 'next/link'
// Custom Components
import BackToHome from 'components/BackToHome'
// Page component
export default function ClientSideRendered() {
const [state, setState] = useState([] as any)
const getData = async () => {
const res = await fetch('https://reqres.in/api/users?page=2')
const jsonData = await res.json()
setState(jsonData)
}
useEffect(() => {
getData()
}, [])
return (
<>
<Head>
<title>Client-Side Rendering (CSR) • Guy Dumais</title>
<meta name="description" content="Example page using Client-Side Rendering (CSR) with Next.js 11 and React 17"/>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<BackToHome/>
<h1>Client-Side Rendering (CSR)</h1>
<p>Data fetched on the client-side only.</p>
<ul>
{
state.data?.map((e) => (
<li key={e.id}>{e.email}</li>
))
}
</ul>
</>
)
}