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

Server rendered routes with hashes do not work with browser back/forward navigation #56112

Closed
1 task done
benhonda opened this issue Sep 27, 2023 · 12 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template. locked please add a complete reproduction Please add a complete reproduction.

Comments

@benhonda
Copy link

Link to the code that reproduces this issue

https://github.com/benhonda/nextjs-router-bug

To Reproduce

  1. Go to https://nextjs.org/docs
  2. Click on any of the "hashed" routes (located in the right-hand sidebar on desktop screens)
  3. Navigate to a different page by clicking on a page route (located in the left-hand sidebar on desktop screens)
  4. Click the back button on your browser. The URL changes and the page may scroll, but the page content remains from Step 3.

Current vs. Expected behavior

Currently, the URL changes and the page may scroll, but the page content does not change.

I expect the page to navigate back to the previous content (as specified in the URL) and for my scroll position to be at the specified hash.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

OS:
MacOS Monterey 12.6.8
Intel chip


Tested browsers:
Firefox 117.0.1, Chrome 117.0.5938.92, Safari 16.5.2

Which area(s) are affected? (Select all that apply)

App Router

Additional context

The "Link to the code that reproduces this issue" just leads to an empty template but I have a good excuse - I swear!
I came across this issue in my local development, where I needed to set hash links to footnotes for A11y purposes, however since the issue also appears to be present on the nextjs docs site, I decided to just use that as my example for convenience.

@benhonda benhonda added the bug Issue was opened via the bug report template. label Sep 27, 2023
@stijnwesterhof
Copy link

I don't see this issue happening on the docs, but I do experience this in my app. Currently also looking for a solution...

@evoX0
Copy link

evoX0 commented Dec 22, 2023

Same here
next 14.0.1

@evoX0
Copy link

evoX0 commented Jan 22, 2024

Is there any workaround or smth we can use to avoid this issue? thanks

@bircha
Copy link

bircha commented Feb 13, 2024

I can reproduce the bug in our project. Any plans for a fix?

@itsjavi
Copy link

itsjavi commented Mar 30, 2024

I cannot reproduce it anymore on the docs page.
My problem is similar (using Next.js 14 app router):

  • Change the hash in the client e.g. window.location.hash = '#l=es'
  • Reload the page with that hash (e.g. '/pokemon/#l=es').
  • Change the hash in the client to something else e.g. #l=it
  • Navigate to a parallel+intercepted route, but keeping the hash (e.g. '/pokemon/bulbasaur/#l=it')
  • Try to call router.back() (the router from next/navigation)

Behavior:

  • The page goes back to /pokemon/#l=it, but the intercepted view is still shown
  • After calling router.back() again it goes to '/pokemon/#l=es' and the intercepted view disappears

Desired behavior:

  • The page goes back to /pokemon/#l=it and the intercepted view is removed from the dom

@arshseraphic
Copy link

arshseraphic commented May 15, 2024

I have similar issue, using next 14.2.3 App Router.

I am on page

https://www.bettingtipsafrica.com/betting-sites/

then i click Countries in right sidebar which take me to hash link

https://www.bettingtipsafrica.com/betting-sites/#other-countries

When i choose some country or click any link in footer or header , for example i click Tanzania

https://www.bettingtipsafrica.com/betting-sites/tanzania/

it take me to page and show correct content, which is good, now when i click back button in browser,

url changed to https://www.bettingtipsafrica.com/betting-sites/#other-countries.

which is also fine. but content did not change

content still showing of page https://www.bettingtipsafrica.com/betting-sites/tanzania/

It should be showing content of

https://www.bettingtipsafrica.com/betting-sites/#other-countries

you can verify content by going to top of page and see main headin

azraftaohid added a commit to azraftaohid/get-link that referenced this issue Jun 13, 2024
Update hash based list to query based in dashboard to mitigate Nextjs 14 issue vercel/next.js#56112
@stefan-girlich
Copy link

Confirming the behavior described by @arshseraphic using "next": "14.2.4" with app router.

@Maxnikit
Copy link

Having same issue using "next": "14.2.5" with app router. Has anybody found a workaround?

@bootleq
Copy link

bootleq commented Nov 25, 2024

Same behavior with "next": "14.2.16" app router.

As a workaround, I add below component to global layout, hold pathname internally, listen to popstate event, force a router.replace when detecting the pathname didn't follow URL change.

"use client"

import { useState, useEffect, useCallback } from 'react';
import { useRouter, usePathname } from 'next/navigation';

export default function FollowHashRoute() {
  const [latestPath, setLatestPath] = useState('');
  const pathname = usePathname();
  const router = useRouter();

  const onPopState = useCallback((e: PopStateEvent) => {
    const newPathname = new URL(document.location.href).pathname;

    if (newPathname !== latestPath) {
      setLatestPath(newPathname);
      router.replace(document.location.href);
    }
  }, [router, latestPath]);

  useEffect(() => {
    window.addEventListener('popstate', onPopState);

    return () => {
      window.removeEventListener('popstate', onPopState);
    };
  }, [onPopState]);

  if (pathname !== latestPath) {
    setLatestPath(pathname);
  }

  return null;
}

@Casal0x
Copy link

Casal0x commented Nov 27, 2024

Hi all, has anyone found a workaround to this? I just experiencing the same thing.

@ztanner ztanner added the please add a complete reproduction Please add a complete reproduction. label Nov 27, 2024
Copy link
Contributor

We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate.

Why was this issue marked with the please add a complete reproduction label?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository (template for App Router, template for Pages Router), but you can also use these templates: CodeSandbox: App Router or CodeSandbox: Pages Router.

To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.

Please test your reproduction against the latest version of Next.js (next@canary) to make sure your issue has not already been fixed.

If you cannot create a clean reproduction, another way you can help the maintainers' job is to pinpoint the canary version of next that introduced the issue. Check out our releases, and try to find the first canary release that introduced the issue. This will help us narrow down the scope of the issue, and possibly point to the PR/code change that introduced it. You can install a specific version of next by running npm install next@<version>.

I added a link, why was it still marked?

Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

What happens if I don't provide a sufficient minimal reproduction?

Issues with the please add a complete reproduction label that receives no meaningful activity (e.g. new comments with a reproduction link) are automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue with the required reproduction.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without reproduction steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

@samcx samcx closed this as completed Dec 5, 2024
bootleq added a commit to bootleq/feeders that referenced this issue Dec 15, 2024
There is a problem when history going BACK from e.g.,
    /world/area/@xxx
  to
    /facts#fact-2016-05-05_120

the location does change, while the page stayed the same.

Similar issue report of next.js:
vercel/next.js#56112

As a workaround, add a global FollowHashRoute component to take record
of pathname changes, maintain an internal `latestPath` state.

Listen to `popstate` event (which fires during history BACK/FORWARD),
force a router navigation when detect bad case (URL pathname not match
latest known pathname).
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked please add a complete reproduction Please add a complete reproduction.
Projects
None yet
Development

No branches or pull requests