getStaticProps
导出名为 getStaticProps 的函数将使用该函数返回的 props 在构建时预渲染页面
import type { InferGetStaticPropsType, GetStaticProps } from 'next'
type Repo = {
name: string
stargazers_count: number
}
export const getStaticProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}) satisfies GetStaticProps<{
repo: Repo
}>
export default function Page({
repo,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return repo.stargazers_count
}你可以在顶层作用域中导入模块以在 getStaticProps 中使用。所使用的导入将不会为客户端打包。这意味着你可以在 getStaticProps 中直接编写服务器端代码,包括从数据库获取数据。
Context 参数
context 参数是一个包含以下键的对象
| 名称 | 描述 |
|---|---|
params | 包含使用动态路由的页面的路由参数。例如,如果页面名称是 [id].js,那么 params 将看起来像 { id: ... }。你应该将其与 getStaticPaths 一起使用,我们稍后会解释。 |
preview | (已废弃,改用 draftMode) 如果页面处于预览模式,则 preview 为 true,否则为 false。 |
previewData | (已废弃,改用 draftMode) 由 setPreviewData 设置的预览数据。 |
draftMode | 如果页面处于草稿模式,则 draftMode 为 true,否则为 false。 |
locale | 包含活动区域设置(如果已启用)。 |
locales | 包含所有支持的区域设置(如果已启用)。 |
defaultLocale | 包含配置的默认区域设置(如果已启用)。 |
revalidateReason | 提供函数被调用的原因。可以是以下之一:“build”(在构建时运行)、“stale”(重新验证周期过期,或在开发模式下运行)、“on-demand”(通过按需重新验证触发) |
getStaticProps 返回值
getStaticProps 函数应返回一个对象,其中包含 props、redirect 或 notFound,后跟一个可选的 revalidate 属性。
props
props 对象是键值对,其中每个值都由页面组件接收。它应该是一个可序列化对象,以便任何传递的 props 都可以通过 JSON.stringify 序列化。
export async function getStaticProps(context) {
return {
props: { message: `Next.js is awesome` }, // will be passed to the page component as props
}
}`revalidate`
revalidate 属性是页面重新生成可以发生的秒数(默认为 false 或不重新验证)。
// 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://.../posts')
const posts = await res.json()
return {
props: {
posts,
},
// Next.js will attempt to re-generate the page:
// - When a request comes in
// - At most once every 10 seconds
revalidate: 10, // In seconds
}
}了解更多关于增量静态生成。
利用 ISR 的页面的缓存状态可以通过读取 x-nextjs-cache 响应头的值来确定。可能的值如下:
MISS- 路径不在缓存中(最多发生一次,在首次访问时)STALE- 路径在缓存中但超过了重新验证时间,因此它将在后台更新HIT- 路径在缓存中且未超过重新验证时间
notFound
notFound 布尔值允许页面返回 404 状态和404 页面。如果设置 notFound: true,即使之前成功生成了页面,该页面也会返回 404。这旨在支持用户生成的内容被其作者删除等用例。请注意,notFound 遵循此处所述的相同 revalidate 行为。
export async function getStaticProps(context) {
const res = await fetch(`https://.../data`)
const data = await res.json()
if (!data) {
return {
notFound: true,
}
}
return {
props: { data }, // will be passed to the page component as props
}
}须知:对于
fallback: false模式,不需要notFound,因为只有getStaticPaths返回的路径才会被预渲染。
redirect
redirect 对象允许重定向到内部或外部资源。它应该匹配 { destination: string, permanent: boolean } 的格式。
在某些罕见情况下,你可能需要为旧的 HTTP 客户端分配自定义状态码才能正确重定向。在这些情况下,你可以使用 statusCode 属性而不是 permanent 属性,但不能同时使用两者。你还可以像在 next.config.js 中重定向一样设置 basePath: false。
export async function getStaticProps(context) {
const res = await fetch(`https://...`)
const data = await res.json()
if (!data) {
return {
redirect: {
destination: '/',
permanent: false,
// statusCode: 301
},
}
}
return {
props: { data }, // will be passed to the page component as props
}
}如果重定向在构建时已知,则应将其添加到next.config.js中。
读取文件:使用 process.cwd()
可以直接从文件系统中在 getStaticProps 中读取文件。
为此,你必须获取文件的完整路径。
由于 Next.js 将你的代码编译到单独的目录中,因此你不能使用 __dirname,因为它返回的路径将与 Pages Router 不同。
相反,你可以使用 process.cwd(),它为你提供了 Next.js 正在执行的目录。
import { promises as fs } from 'fs'
import path from 'path'
// posts will be populated at build time by getStaticProps()
function Blog({ posts }) {
return (
<ul>
{posts.map((post) => (
<li>
<h3>{post.filename}</h3>
<p>{post.content}</p>
</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 postsDirectory = path.join(process.cwd(), 'posts')
const filenames = await fs.readdir(postsDirectory)
const posts = filenames.map(async (filename) => {
const filePath = path.join(postsDirectory, filename)
const fileContents = await fs.readFile(filePath, 'utf8')
// Generally you would parse/transform the contents
// For example you can transform markdown to HTML here
return {
filename,
content: fileContents,
}
})
// By returning { props: { posts } }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts: await Promise.all(posts),
},
}
}
export default Blog版本历史
| 版本 | 更改 |
|---|---|
v13.4.0 | App Router 现已稳定,数据获取已简化 |
v12.2.0 | 按需增量静态生成已稳定。 |
v12.1.0 | 按需增量静态生成已添加(beta)。 |
v10.0.0 | 添加了 locale、locales、defaultLocale 和 notFound 选项。 |
v10.0.0 | 添加了 fallback: 'blocking' 返回选项。 |
v9.5.0 | 稳定的增量静态生成 |
v9.3.0 | 引入了 getStaticProps。 |
这有帮助吗?