urql icon indicating copy to clipboard operation
urql copied to clipboard

Bug with useRouter from next/router

Open DavoCg opened this issue 7 years ago • 6 comments

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 avatar Jun 11 '19 08:06 DavoCg

@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

kitten avatar Jun 11 '19 08:06 kitten

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 avatar Jun 11 '19 09:06 DavoCg

@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

kitten avatar Jun 11 '19 09:06 kitten

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

DavoCg avatar Jun 11 '19 09:06 DavoCg

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?

steviewonders3 avatar Aug 09 '19 19:08 steviewonders3

@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

steviewonders3 avatar Aug 09 '19 20:08 steviewonders3