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

router.back() not working when going back between two dynamic routes #25285

Closed
DaniAkash opened this issue May 20, 2021 · 6 comments · Fixed by #25459
Closed

router.back() not working when going back between two dynamic routes #25285

DaniAkash opened this issue May 20, 2021 · 6 comments · Fixed by #25459
Assignees

Comments

@DaniAkash
Copy link

What version of Next.js are you using?

10.2.2

What version of Node.js are you using?

14.15

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

vercel

Describe the Bug

I have a dynamic route in the following format:

/path/[type]

If i visit the following paths:
/path/custom1
/path/custom2

and then call router.back() from custom2 I get the following error message:

Unhandled Runtime Error
Error: The provided `as` value (/path/custom1) is incompatible with the `href` value (/h/[type]). Read more: https://nextjs.org/docs/messages/incompatible-href-as
Call Stack
Router._callee$
node_modules/next/dist/next-server/lib/router/router.js (1055:16)
tryCatch
node_modules/regenerator-runtime/runtime.js (63:14)
Generator.invoke [as _invoke]
node_modules/regenerator-runtime/runtime.js (293:0)
Generator.eval [as next]
node_modules/regenerator-runtime/runtime.js (118:0)
asyncGeneratorStep
node_modules/@babel/runtime/helpers/asyncToGenerator.js (3:0)
_next
node_modules/@babel/runtime/helpers/asyncToGenerator.js (25:0)

I use my app with a basepath. It is happening only when I have a basepath. My next.config.js file is:

module.exports = {
  basePath: "/app",
  future: {
    webpack5: true
  }
};

Given my application has lots of dynamic routes I'm unable to upgrade from 10.0 to 10.2.

Expected Behavior

The page should be able to go back from /path/custom2 to /path/custom1

To Reproduce

I have created a codesandbox with reproducable scenario:

https://znkfe.sse.codesandbox.io/app

If the buttons are followed, it will land on the issue.

Source: https://codesandbox.io/s/nextjs-routing-issue-znkfe?file=/pages/index.js

@DaniAkash DaniAkash added the bug Issue was opened via the bug report template. label May 20, 2021
@m235
Copy link

m235 commented May 21, 2021

same here in production since 10.1 or 10.2

The provided as value (/product/204714/34501608) is incompatible with the href value (/oduct/[saleId]/[itemId]). Read more: https://nextjs.org/docs/messages/incompatible-href-as

Not linked to a Link or router.push but this error is happening when browsing back the history (navigator back button for example)

@m235
Copy link

m235 commented May 21, 2021

BTW I also have a base path 3 chars long (/gr) look like a wrong splice somewhere because /product become /oduct

@TanDung2512
Copy link

Same here, It works well on 10.2 until I upgrade to 10.2.2. My case is that I have a rewrite path.
{ source: '/(.*)post:id.html', destination: '/content/detail/:id', }

Screen Shot 2021-05-21 at 15 32 55

@PaulSenon
Copy link

Same here, I was about to open an issue but I found yours.

The problem appears between v10.2.1-canary.6 and v10.2.1-canary.7.
It might be related to this commit: 5bff9ea

It breaks my website so I had to rollback to 10.2.1 :(

I also prepared something to reproduce the issue here:
https://stackblitz.com/edit/bug-nextjs-router-back-as-path?file=next.config.js
(everything is explained on landing page)

hope it will be fixed for 11.0.0, I'm so exited to try the incoming new features :D

@ijjk ijjk added kind: bug and removed bug Issue was opened via the bug report template. labels May 25, 2021
@ijjk ijjk self-assigned this May 25, 2021
@ijjk ijjk added the point: 2 label May 25, 2021
@kodiakhq kodiakhq bot closed this as completed in #25459 May 26, 2021
kodiakhq bot pushed a commit that referenced this issue May 26, 2021
…5459)

This ensures the `basePath` is correctly re-added to the `url` after resolving a dynamic route since the `url` stored in history is expected to already contain the `basePath`, an additional test has been added to ensure this is working correctly back/forward through history

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

Fixes: #25285
@PaulSenon
Copy link

PaulSenon commented May 31, 2021

I'm sorry for reopening this closed issue but it seems it hasn't totally fixed the problem :'(

I updated the reproduction url with latest v10.2.4-canary.0 containing this fix, but the bug is still here.

Here is the updated example project to reproduce the bug:
https://stackblitz.com/edit/bug-nextjs-router-back-as-path?file=next.config.js

I also forked and updated the example from @DaniAkash that still show the same issue:

If I can help providing some more infos, I'd love to.

[UPDATE] I suppose we need another ticket, not yet closed, so I commented under a new one I found mentionning this issue: #25490

flybayer pushed a commit to blitz-js/next.js that referenced this issue Jun 16, 2021
…rcel#25459)

This ensures the `basePath` is correctly re-added to the `url` after resolving a dynamic route since the `url` stored in history is expected to already contain the `basePath`, an additional test has been added to ensure this is working correctly back/forward through history

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

Fixes: vercel#25285
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants