首页 > 解决方案 > Nextjs using multiple parameters per slug

问题描述

    export async function getStaticPaths() {
    const posts = getPagesToRender();
    const postPaths = posts.map((post, i) => {
        return {
            params: { page: post[1].replace(".md", ""), pageId: post[0] }
        }
    })

    return {
        paths: postPaths,
        fallback: false
    }
}

File names are like this: [1]page1.md I pass it to getStaticPaths as

page: post[1].replace(".md", ""), pageId: post[0]

So they are like page: page1 pageId: 1

But It does not work because nextjs only expects 1 parameter. So I can only send the page parameter as nextjs ignores the second. Solution is to use 2 url slugs like this : [pageId]/[page] But I don't want to pollute the url if possible.

Can you send 2 parameters from 1 slug?

标签: next.js

解决方案


不,您应该使用[...slug].js而不是[slug].js. 这使参数成为一个数组!


推荐阅读