Bug with useRouter from next/router
Hi guys,
After following the tutorial for next everything work as expected except when you reload a page which use next/router (using useRouter hook or withRouter HOC) there is an error which seems to be related to react-ssr-prepass where the router is null.
Does anybody encounter the same problem ?
We are using urql: ^1.1.1 and next: ^8.1.0
Thank you all
@DavoCg can you maybe check the App component in the example? We've just copied the with-apollo example but this just sounds like that's missing a prop or provider
Hi @kitten I had a second look and I think my setup is correct.
I made a repository based on your 3-ssr-with-nextjs example to reproduce the error. If you add useRouter in the index page like this
import { useRouter } from 'next/router';
import Submit from '../components/Submit';
import PostList from '../components/PostList';
export default () => {
const { query } = useRouter();
return (
<App>
<Submit />
<PostList />
</App>
);
};
You have this error from react-ssr-prepass TypeError: Cannot read property 'query' of null
Repo is here : https://github.com/DavoCg/urql-demo
Let me know if I'm missing something and thank you this great project !
Davo
@DavoCg yea I think you're doing everything right but our example is slightly buggy. I'll see what prop we're missing but we're probably just not passing the router prop on correctly
Thank you, after deeper investigation it seems to work well with withRouter HOC but not with the useRouter hook from next/router.
Thank you for your answers
I'd like to reopen this issue @kitten, I'm experiencing the same problem. withRouter works fine, useRouter does not. The pageProps always resolves correctly on the first pass, but it looks like there are a couple of more passes that happen before the page is rendered, where { pageProps: { statusCode: 404 } }.
using withRouter is a bandaid and goes against our codebase standards that prefers hooks over HOCs, so it would be a big help to have the hook working as expected. You mentioned that the example is slightly buggy, is this something that could be addressed there, or is it a deeper problem with react-ssr-prepass?
@DavoCg @kitten ~I managed to fix it, I don't know how good of a fix this is but it makes sense to me - I just wrapped the App passed in to withUrqlClient with a withRouter before calling sssPrepass~
I thought it was working but that didn't actually do anything :( back to the drawing board