Next.js Discord

Discord Forum

String with query parameters in generateStaticParams

Answered
Korean bullhead posted this in #help-forum
Open in Discord
Korean bullheadOP
Could you please advise me, I have a following page.tsx which gets the data from the server and delivers it to the Component:
export async function generateStaticParams ({ params: { product } }: IParamsProps) {
    switch (product) {
        case 'first':
            return [
                { doc: `doc?name=${product}&version=1.4&language=de` },
                { doc: `doc?name=${product}&version=1.3&language=de` },

                { doc: `doc?name=${product}&version=1.4&language=en` },
                { doc: `doc?name=${product}&version=1.3&language=en` },
            ];

        case 'second':
            return [
                { doc: `doc?name=${product}&version=1.6&language=de` },
                { doc: `doc?name=${product}&version=1.6&language=en` },
            ];

        default:
            return [];
    };
};

async function getData ({ params }: IPageProps & IParamsProps) {
    const {
        doc,
        product,
    } = params;

    const res = await fetch(
        `http://localhost:3000/${product}${doc.slice(3)}`,
        {
            method: 'GET',
            cache: 'no-store',
        }
    );
    const data = await res.json();

    return data;
}

const Page = async (props) => {
    const {
        params 
    } = props;

    const data = await getData({ params });

    return <Component data={data} />
}


app directory: [lang]/web/[product]/[doc]
next.js version: 14.0.2

After executing the npm run build, the question mark is replaced by %3F. So that instead of doc?name=product_name&version=1.4&language=de I got doc%3Fname=product_name&version=1.4&language=de.

How can this be fixed? And is it even normal to generate query strings as static paths?
Answered by not-milo.tsx
Ok, sorry but I think I misread your first message. And no, it's not good practice to generate static params with query strings in them.

generateStaticParams should return only an array of path names and nothing more. What you can do though, is create sub-paths for each one of the parameters and generate pages that way. So you would end up with a url that looks like this: https://yourdomain.com/en/web/product-name/version/
View full answer

4 Replies

It's not an error, but rather how your browser encodes URIs. You can use the [decodeURI()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI) js function to get a correct representation of your string.
@not-milo.tsx It's not an error, but rather how your browser encodes URIs. You can use the [`decodeURI()`](<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI>) js function to get a correct representation of your string.
Korean bullheadOP
Thanks for the reply! Yes, you are right. However, the problem I described is that I get such paths after the page build by Next.js itself. I need this pages to be SSG with query params in the URL. And something tells me that the coding logic is embedded in the Next.js engine, which is frustrating, because in this case, at least, I can't process query params via useSearchParams() (because of %3F string)
Ok, sorry but I think I misread your first message. And no, it's not good practice to generate static params with query strings in them.

generateStaticParams should return only an array of path names and nothing more. What you can do though, is create sub-paths for each one of the parameters and generate pages that way. So you would end up with a url that looks like this: https://yourdomain.com/en/web/product-name/version/
Answer
Korean bullheadOP
Got it, thanks so much for your help 🤝