- やりたいこと
/blog/[$category]/[$index]
みたいなページをビルド時に生成したい
- やったこと
/blog/category/[...id].js
のgetStaticPathsでparamsのidにパスのリストを入れる
- つまずいたこと
/blog/category/[id].js
ではネストしたパスのページは生成できない
- 気になること
getStaticPaths
とgetStaticProps
で二回ポスト一覧を取得している
- 関数の外で書けばいいだけ?今気づいたのであとでためす
export function getStaticPaths() {
let categorizedPosts = getCategorizedPosts()
let paths = []
for (let category in categorizedPosts) {
const pageSliceLength = Math.ceil(categorizedPosts[category].length / 10);
for (let index = 0; index < pageSliceLength; index++) {
const id = [category.toString(),index.toString()]
paths.push({params: {id}})
}
}
return {paths, fallback: false}
}
export function getStaticProps({params}) {
const [category, index] = params.id
const ps = getCategorizedPosts()
const posts = ps[category].slice(index * 10, index * 10 + 10)
return {
props: { posts, category, index }
}
}