Tuvshuu Erdenebileg
Tuvshuu Erdenebileg

Reputation: 21

Next.js catch all route not working as expected with getStaticPaths

Next.js catch all route not working with getStaticPaths. According to documentation, I want to navigate to t/a.cat and t/a.cat/a.id, but it only works for t/a.cat/a.id.

export const getStaticPaths = async () => {
    await fetch(`http://localhost:8000/foods`).then((res) => {
        const data = res;
        const paths = data.map((a) => ({
            params: {
                t: [a.cat, a.id],
            },
        }));
        return {
            paths,
            fallback: false,
        };
    });
};

Upvotes: 2

Views: 1451

Answers (1)

juliomalves
juliomalves

Reputation: 50328

That's because you're only returning t: [a.cat, a.id] in your paths. You'll need to return t: [a.cat] in the paths as well.

const paths = data.map((a) => ([
    { params: { t: [a.cat] } }
    { params: { t: [a.cat, a.id] } }
])).flat();

Upvotes: 4

Related Questions