Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs: context undefined in getStaticProps

the context from getStaticProps is undefined. If I console.log the context I get: { locales: undefined, locale: undefined }

I need the information from the url... If I try the same with getServerSideProps it is working. I'm using apollo with nextjs like the example: https://github.com/vercel/next.js/tree/canary/examples/with-apollo

export async function getStaticProps(context) {
    const apolloClient = initializeApollo();

    await apolloClient.query({
        query: PAGE,
        variables: variables,
    });

    console.log(context);
    // { locales: undefined, locale: undefined }
    // !!! need the info from the URL !!!

    return {
        props: {
            initialApolloState: apolloClient.cache.extract(),
        },
        revalidate: 1,
    };
}

Thx for any help.

like image 795
Tölz Avatar asked May 20 '26 03:05

Tölz


1 Answers

These params doesn’t exist on the context Object that’s why you get undefined. I assume that you pass them as query parameters to the url. In that case you can get them from the context query param like this:

const { locales } = context.query

Read more about the params of the context Object in the next docs https://nextjs.org/docs/api-reference/data-fetching/getInitialProps

like image 85
tobzilla90 Avatar answered May 24 '26 20:05

tobzilla90



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!