danielr18 / connected-next-router

A Redux binding for Next.js Router compatible with Next.js.
MIT License
109 stars 30 forks source link

Requests with query parameters are being redirected to / #52

Closed hacktisch closed 4 years ago

hacktisch commented 4 years ago

There is a bit of code in ConnectedRouter.js that checks for "locationMismatch" on store changes. This mismatch apparently also happens when a request contains query parameters. Maybe it doesn't happen in every use case of connected-next-router, so I'll describe my setup:

I have one Next.js page that catches all routes ([[...slug]].js) It uses getStaticPaths for a list of default paths to make static pages, and has fallback: true to ensure catching all routes.

ConnectedRouter is wrapper around the Component in _app.js

DZakh commented 4 years ago

It's a problem with unsync of initialState and actual location. So you always have to set a proper initialState, or else it'll redirect you during the next request. In my case it was even a backend request.

DZakh commented 4 years ago

@hacktisch You can try my fork

npm i github:DZakh-forks/connected-next-router#a5c507b3e41091f2a6f53abee33be2fefd1b3355

And add the clientSideAutosync flag to the ConnectedRouter. It's not an ideal solution but it works for me.

DZakh commented 4 years ago

And if you do a redirect inside of useEffect, you need to wrap it in:

Router.ready(() => {
        setTimeout(() => { 
          // ...
        });
      });

Meh...

danielr18 commented 4 years ago

@hacktisch This should be fixed in V4. Please reopen this issue if you notice it still happens