本文介绍了错误:动态 SSG 页面需要 getStaticPaths,“xxx"缺少 getStaticPaths.NextJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 NextJS 中创建我的页面时,我收到此错误错误:动态 SSG 页面需要 getStaticPaths,而 'xxx' 缺少 getStaticPaths".

I am getting this error "Error: getStaticPaths is required for dynamic SSG pages and is missing for 'xxx'" when I try to create my page in NextJS.

我不想在构建时生成任何静态页面.那么为什么我需要创建一个'getStaticPaths'函数呢?

I don't want to generate any static page on build time. So why do I need to create a 'getStaticPaths' function?

推荐答案

如果你正在创建一个动态页面 eg: product/[slug].tsx 那么即使你不想创建在构建时需要创建一个 getStaticPaths 方法来设置 fallback 属性的任何页面,并让 NextJS 知道当您尝试获取的页面不存在时该怎么做.

If you are creating a dynamic page eg: product/[slug].tsx then even if you don't want to create any page on build time you need to create a getStaticPaths method to set the fallback property and let NextJS know what to do when the page you are trying to get doesn't exist.

export const getStaticPaths: GetStaticPaths<{ slug: string }> = async () => {

    return {
        paths: [], //indicates that no page needs be created at build time
        fallback: 'blocking' //indicates the type of fallback
    }
}

getStaticPaths 主要做两件事:

  • 指示应在构建时创建哪些路径(返回 paths 数组)

指出当某个页面例如:product/myProduct123"时要做什么NextJS 缓存中不存在(返回 fallback 类型)

Indicate what to do when a certain page eg: "product/myProduct123" doesn't exist in the NextJS Cache (returning a fallback type)

这篇关于错误:动态 SSG 页面需要 getStaticPaths,“xxx"缺少 getStaticPaths.NextJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 04:16