Next.js Discord

Discord Forum

Getting rewritten path value during build

Unanswered
Bullmastiff posted this in #help-forum
Open in Discord
BullmastiffOP
I'm using rewrites to map friendly path names e.g. /about to a document ID from my headless cms e.g. /fa093j094jfaj00934, and I'm trying to generate metadata that uses the path of the page to fetch the data from an API

In development, this works fine, but when building the application locally and on vercel, the rewrites are not considered and the indicated path of the request has the original name, i.e. /about

How do i go about fixing this?

2 Replies

BullmastiffOP
export const generateMetadata = async ({ params }: { params: { slug: string[] } }): Promise<Metadata> => {
    const { slug } = params
    const client = getClient()
    const page = await fetch(
        `${process.env.CMS_API}/pages/${slug[0]}`,
    ).then((res) => res.json())`
// in development returns /pages/09j390f90f, but during build is /pages/about
...
}
BullmastiffOP
and my next.config.js:


const nextConfig = {
    reactStrictMode: true,
    async rewrites () {
        const pages = await fetch(
            `${process.env.CMS_API}/pages?where[_status][equals]=published&limit=1000`,
        ).then((res) => res.json())
        const paths = pages.docs.map((page) => ({
            source: page.breadcrumbs[page.breadcrumbs.length - 1].url,
            destination: `/${page.id}`,
        }))
        return paths
    },
}