Skip to content

(next-urql) Handling difference between static and client fetch (due to auth) and static site generation #1771

Answered by zachasme
zachasme asked this question in Q&A
Discussion options

You must be logged in to vote

The solution we found was to not populate cache data unless we find data:

export async function getStaticProps({ params }) {
  const ssrCache = ssrExchange({ isClient: false });
  const client = initUrqlClient(getConfig(ssrCache), false);

  // This query is used to populate the cache for the query
  // used on this page.
  const { data } = await client
    .query(POKEMON_QUERY, { id: params.id })
    .toPromise();

  // Do not extract and pass urql state unless we found data
  if (!data?.pokemon) return { props: {} };

  return {
    props: {
      // urqlState is a keyword here so withUrqlClient can pick it up.
      urqlState: ssrCache.extractData(),
    },
  };
}

That is, skip passing s…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zachasme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant