Loading...
Loading...
Compare original and translation side by side
revalidategetStaticPropsfallback: truegetStaticPathsrevalidatePathrevalidateTaggenerateStaticParamsgetStaticPropsrevalidategetStaticPathsfallback: truerevalidatePathrevalidateTaggenerateStaticParamsexport async function getStaticPaths() {
const products = await getProductsFromDatabase();
const paths = products.map((product) => ({
params: { id: product.id }
}));
// fallback: true means that the missing pages
// will not 404, and instead can render a fallback.
return { paths, fallback: true };
}
// params will contain the id for each generated page.
export async function getStaticProps({ params }) {
return {
props: {
product: await getProductFromDatabase(params.id)
}
}
}
export default function Product({ product }) {
const router = useRouter();
if (router.isFallback) {
return <div>Loading...</div>;
}
// Render product
}fallback: trueexport async function getStaticPaths() {
const products = await getProductsFromDatabase();
const paths = products.map((product) => ({
params: { id: product.id }
}));
// fallback: true means that the missing pages
// will not 404, and instead can render a fallback.
return { paths, fallback: true };
}
// params will contain the id for each generated page.
export async function getStaticProps({ params }) {
return {
props: {
product: await getProductFromDatabase(params.id)
}
}
}
export default function Product({ product }) {
const router = useRouter();
if (router.isFallback) {
return <div>Loading...</div>;
}
// Render product
}fallback: true// This function runs at build time on the build server
export async function getStaticProps() {
return {
props: {
products: await getProductsFromDatabase(),
revalidate: 60, // This will force the page to revalidate after 60 seconds
}
}
}
// The page component receives products prop from getStaticProps at build time
export default function Products({ products }) {
return (
<>
<h1>Products</h1>
<ul>
{products.map((product) => (
<li key={product.id}>{product.name}</li>
))}
</ul>
</>
)
}getStaticProps()// This function runs at build time on the build server
export async function getStaticProps() {
return {
props: {
products: await getProductsFromDatabase(),
revalidate: 60, // This will force the page to revalidate after 60 seconds
}
}
}
// The page component receives products prop from getStaticProps at build time
export default function Products({ products }) {
return (
<>
<h1>Products</h1>
<ul>
{products.map((product) => (
<li key={product.id}>{product.name}</li>
))}
</ul>
</>
)
}getStaticProps()Note (React 18+ / Next.js 13+): On-Demand RevalidationA new best practice is to use On-Demand Revalidation when possible. Next.js provides APIs (e.g.,in API routes, orres.revalidateandrevalidatePathin App Router) to trigger revalidation of specific pages immediately after content changes, rather than waiting for the next timed interval.revalidateTagFor fallback pages, the pattern of usinginfallback: trueremains valid. In Next.js 13 App Router, fallback behavior is handled by thegetStaticPathsconventions automatically.loading.jsEdge caching and ISR: Platforms like Vercel and Cloudflare let you run ISR on the edge. ISR is now a standard practice for large sites—refine it with targeted revalidation and good loading states.
注意(React 18+ / Next.js 13+):按需重新验证一个新的最佳实践是尽可能使用按需重新验证。Next.js提供了API(例如API路由中的,或App Router中的res.revalidate和revalidatePath),可以在内容变更后立即触发特定页面的重新验证,而无需等待下一个定时间隔。revalidateTag对于回退页面,在中使用getStaticPaths的模式仍然有效。在Next.js 13的App Router中,回退行为由fallback: true约定自动处理。loading.js边缘缓存与ISR:像Vercel和Cloudflare这样的平台允许你在边缘运行ISR。如今,ISR已成为大型站点的标准实践——通过针对性的重新验证和良好的加载状态来优化它。