getStaticPaths()
一直抛出404页面,我猜这是因为函数getAllPostIds()
意味着返回一个对象数组。我正在使用typescript,我已经声明了对象数组但我不确定如何在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
}
}
}
添加fallback
道具为true
或blocking
。将其设置为false将返回404页面,因为所请求的路径没有在构建时生成。
return{
paths,
fallback: true
}