connected-next-router icon indicating copy to clipboard operation
connected-next-router copied to clipboard

Doesn't consider basePath?

Open laggingreflex opened this issue 2 years ago • 3 comments

Does this not take basePath into consideration?

I have "/frontend" as my basePath and I'm seeing this in the logs (using redux-logger):

CHANGED: router.location.href /frontend/auth/login → /auth/login
CHANGED: router.location.pathname /frontend/auth/login → /auth/login

Not sure what the expected behaviour should be, but:

  • Should this route change even register, since they're basically the same paths?

  • Should "/frontend" either be added to, or omitted from both before-and after-states?

Great module BTW!

laggingreflex avatar Dec 14 '21 18:12 laggingreflex

I can't reproduce it in the examples given, but it's definitely causing weird artifacts in my app. Like redundantly duplicating basePath "/frontend/frontend/...", and subsequently redirecting to it which obviously results in 404. I can't yet figure out why or where this is happening, or whether its my own app or this library...

But I've found a workaround that seems to avoid the issue of unnecessary redirection and path changes.

By patching next/router:

import Router from 'next/router'
import { createRouterMiddleware, ConnectedRouter } from 'connected-next-router';

/* Make `Router.asPath` return with `basePath` prefixed (if absent) */
const patchedRouter = new Proxy(Router, {
  get: (Router, key, receiver) => {
    if (key === 'asPath') {
      const { basePath, asPath } = Router
      const replaced = asPath.includes(basePath) ? asPath : basePath + asPath
      return replaced
    } else {
      return Reflect.get(Router, key, receiver)
    }
  }
})

createRouterMiddleware({ Router: patchedRouter })

<ConnectedRouter Router={patchedRouter}/>

laggingreflex avatar Dec 16 '21 09:12 laggingreflex

Hi @laggingreflex, I'm currently sick and can't investigate the issue at the moment. I should be able to next week.

danielr18 avatar Dec 16 '21 09:12 danielr18

I'm also seeing this behavior, but only when using query parameters. Otherwise great work!

polaroidkidd avatar Mar 10 '22 14:03 polaroidkidd