Next.js SSG
使用 Next.js,您可以使用 靜態生成 (SSG) 預先渲染您的頁面。您的頁面將在建置時生成,並以靜態方式提供給訪客。它也可以由 CDN 快取以最大化效能。
Nextra 也支援此功能。以下是一個範例
Nextra 在 GitHub 上有 11740 顆星!
上面的數字是在建置時透過 getStaticProps
生成的。啟用 增量靜態再生 後,它將保持最新狀態。
以下是上述範例的 MDX 程式碼
MDX
import { useData } from 'nextra/hooks'
export function getStaticProps() {
return fetch('https://api.github.com/repos/shuding/nextra')
.then(res => res.json())
.then(repo => ({
props: {
// We add an `ssg` field to the page props,
// which will be provided to the Nextra `useData` hook.
ssg: {
stars: repo.stargazers_count
}
},
// The page will be considered as stale and regenerated every 60 seconds.
revalidate: 60
}))
}
export function Stars() {
// Get the data from SSG, and render it as a component.
const { stars } = useData()
return <strong>{stars}</strong>
}
Nextra has <Stars /> stars on GitHub!