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

问题描述

当我尝试在 NextJS 中创建我的页面时,我收到此错误 "Error: getStaticPaths is required for dynamic SSG pages and is missing for 'xxx'"

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

解决方案

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

export const getStaticPaths: GetStaticPaths= 异步 () =>{返回 {path: [],//表示构建时不需要创建页面fallback: 'blocking'//表示回退的类型}}

getStaticPaths 主要做两件事:

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

  • 指示当某个页面(例如:product/myProduct123")出现时要执行的操作.NextJS 缓存中不存在(返回 fallback 类型)

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.

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

解决方案

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 does mainly two things:

  • Indicate which paths should be created on build time (returning a paths array)

  • 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.下一个JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 04:14