首页 > 解决方案 > getStaticPaths() 返回 404 Page Not Found

问题描述

getStaticPaths()不断抛出 404 页面,我猜这是因为该函数旨在getAllPostIds()返回一个对象数组。我正在使用打字稿,我已经声明了对象数组,但我不确定如何在id

interface ArrObj {
    params: {
        id: string;
    }
}
let arrObj: ArrObj[];

export function getAllPostIds() {
    const fileNames = fs.readdirSync(postsDirectory);

    return fileNames.map(fileName => {
        return {
            params: {
                id: fileName.replace(/\.md$/, '')
            }
        }
    })
}

export function getPostData(id) {
  const fullPath = path.join(postsDirectory, `${id}.md`)
  const fileContents = fs.readFileSync(fullPath, 'utf8')

  const matterResult = matter(fileContents)

  return {
    id,
    ...matterResult.data
  }
}
export async function getStaticPaths(){
    const paths = getAllPostIds();
    return{
        paths,
        fallback: false
    }
}

export async function getStaticProps({ params }) {
  const postData = getPostData(params.id)
  return {
    props: {
      postData
    }
  }
}

标签: javascriptarraystypescriptnext.js

解决方案


推荐阅读