Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctx is not added to props on the initial render #151

Open
tomanagle opened this issue Dec 27, 2020 · 2 comments
Open

ctx is not added to props on the initial render #151

tomanagle opened this issue Dec 27, 2020 · 2 comments

Comments

@tomanagle
Copy link

tomanagle commented Dec 27, 2020

When using withApollo on individual pages, the query object is undefined twice, before it is defined. This means that if you need to query based on props.query, it will only run the query on the client-side.

For example: https://github.com/tomanagle/NextJS-NestJS-GraphQL-Starter/blob/master/client/pages/user/%5Bpermalink%5D.tsx#L13

I added a workaround to my project by adding ctx to defaultOptions, then spreading that on the page object: https://github.com/tomanagle/NextJS-NestJS-GraphQL-Starter/blob/master/client/lib/withApollo.tsx#L29

A cleaner solution would be for withApollo to return ctx to it is spread on to the page object with the apollo client.

Please let me know if you'd like me to work on this solution, or if you have a better solution.

@rafaelpizzo
Copy link

I'm having exactly same issue, props.query is undefined on the first two renders, making the served SSR content wrong.
Our client side is conditionally loading different components on query param, so nothing renders.

We ended up getting props.query once in getServerSideProps for that part of logic, and then again on client side with Router hooks... But I'm not really happy with this approach.

I couldn't manage to make the workaround @tomanagle suggested to work on our setup though.

@tomanagle
Copy link
Author

@rafaelpizzo a workout for query props would be to import {useRouters} from 'next/router'. Then do something like this:

const router = useRouter();
<ApolloProvider client={props.apollo}>

   <Page {...props} {...router}  />  
<ApolloProvider>

I made a video using this client and you can see the solution here: https://youtu.be/4yDrtFUbUzo?t=422

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants