If you export a function called getStaticProps
(Static Site Generation) from a page, Next.js will pre-render this page at build time using the props returned by getStaticProps
.
export async function getStaticProps(context) {
return {
props: {}, // will be passed to the page component as props
}
}
You should use getStaticProps
if:
getStaticProps
generates HTML
and JSON
files, both of which can be cached by a CDN for performanceWhen combined with Incremental Static Regeneration, getStaticProps
will run in the background while the stale page is being revalidated, and the fresh page served to the browser.
Because getStaticProps
runs at build time, it does not have access to the incoming request (such as query parameters or HTTP
headers) as it generates static HTML
. If you need access to the request for your page, consider using Middleware in addition to getStaticProps
.
The following example shows how you can fetch a list of blog posts from a CMS.
// posts will be populated at build time by getStaticProps()
function Blog({ posts }) {
return (
<ul>
{posts.map((post) => (
<li>{post.title}</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() {
// Call an external API endpoint to get posts.
// You can use any data fetching library
const res = await fetch('https://.../posts')
const posts = await res.json()
// By returning { props: { posts } }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts,
},
}
}
export default Blog
The getStaticProps
API reference covers all parameters and props that can be used with getStaticProps
.
As getStaticProps
runs only on the server-side, it will never run on the client-side. It won’t even be included in the JS bundle for the browser, so you can write direct database queries without them being sent to browsers.
This means that instead of fetching an API route from getStaticProps
(that itself fetches data from an external source), you can write the server-side code directly in getStaticProps
.
Take the following example. An API route is used to fetch some data from a CMS. That API route is then called directly from getStaticProps
. This produces an additional call, reducing performance. Instead, the logic for fetching the data from the CMS can be moved to getStaticProps
.
Alternatively, if you are not using API routes to fetch data, then the fetch()
API can be used directly in getStaticProps
to fetch data.
To verify what Next.js eliminates from the client-side bundle, you can use the next-code-elimination tool.
When a page with getStaticProps
is pre-rendered at build time, in addition to the page HTML file, Next.js generates a JSON file holding the result of running getStaticProps
.
This JSON file will be used in client-side routing through next/link
or next/router
. When you navigate to a page that’s pre-rendered using getStaticProps
, Next.js fetches this JSON file (pre-computed at build time) and uses it as the props for the page component. This means that client-side page transitions will not call getStaticProps
as only the exported JSON is used.