Nextjs:在getStaticProps

时间:2020-10-21 16:30:25

标签: next.js apollo-client

getStaticProps中的上下文未定义。 如果我console.log上下文,我得到: { locales: undefined, locale: undefined }

我需要url中的信息... 如果我对getServerSideProps尝试相同的方法,则可以正常工作。 我将apollo与nextjs一起使用,例如: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,
    };
}

寻求帮助。

1 个答案:

答案 0 :(得分:0)

context对象上不存在这些参数,这就是为什么您获得undefined的原因。我假设您将它们作为查询参数传递给url。在这种情况下,您可以像这样从上下文查询参数中获取它们:

const { locales } = context.query

在下一个文档中详细了解context对象的参数 https://nextjs.org/docs/api-reference/data-fetching/getInitialProps

相关问题