next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Navigation issue with Browser Back Button Click

Open ninjawhocodes opened this issue 7 years ago • 49 comments

Issue :

  1. From http://localhost:3000 navigate to http://localhost:3000/p/hello using Link.
  2. Refresh the page.
  3. Click back button : It goes to http://localhost:3000 but still renders the content of http://localhost:3000/p/hello it is not loading the content of http://localhost:3000.

I am using "next": "^8.1.0".

ninjawhocodes avatar Apr 20 '19 21:04 ninjawhocodes

@ninjawhocodes I can't reproduce the problem, can you post a reproduction repo and the version of your browser

nblthree avatar Apr 22 '19 11:04 nblthree

Thanks @MarchWorks

timneutkens avatar Apr 22 '19 11:04 timneutkens

https://github.com/ninjawhocodes/react-with-next

I have upload the code on above repo.

Steps to reproduce the issue :

  1. npm install.
  2. npm run dev.
  3. Once page is up click on "here". 4 . You will be on "http://localhost:3000/p/hello"
  4. Do a refresh from browser. 6 Click browser back button.
  5. It will go back to "http://localhost:3000" but it will still show the content of "http://localhost:3000/p/hello".

image

image

Aftet Clicking Back image

ninjawhocodes avatar Apr 22 '19 19:04 ninjawhocodes

@ninjawhocodes I can't reproduce the problem, can you post a reproduction repo and the version of your browser

https://github.com/zeit/next.js/issues/7091#issuecomment-485517546 I have uploaded the code on above and sharing the details in below in fresh comment. Please suggest if this issue can be fixed by doing some workaround.

ninjawhocodes avatar Apr 22 '19 19:04 ninjawhocodes

@timneutkens there is an issue caused by importing css files. I will open a new issue with what I found

nblthree avatar Apr 23 '19 10:04 nblthree

@timneutkens there is an issue caused by importing css files. I will open a new issue with what I found

okay so for time being could you please suggest a workaround ?

ninjawhocodes avatar Apr 23 '19 10:04 ninjawhocodes

@ninjawhocodes use styled-jsx https://github.com/zeit/next.js#built-in-css-support or import an empty css file or even the same file that you have imported in your index page in all your pages

nblthree avatar Apr 23 '19 10:04 nblthree

@ninjawhocodes personally I will recommend you to use styled-jsx but if you want to use css file then make sure that you are importing one in every page it can be the same or a different one it doesn't matter

nblthree avatar Apr 23 '19 10:04 nblthree

@ninjawhocodes personally I will recommend you to use styled-jsx but if you want to use css file then make sure that you are importing one in every page it can be the same or a different one it doesn't matter

Yeah, I have to add style.css for each page then only it will work.

ninjawhocodes avatar Apr 23 '19 10:04 ninjawhocodes

@ninjawhocodes you should also know that if you import b.css in b.js and a.css in a.js and you go from a.js page to b.js page you will have a.css loaded in b.js too

nblthree avatar Apr 23 '19 10:04 nblthree

New distinct issue that occurs even without a page refresh: https://github.com/zeit/next.js/issues/9989

iMerica avatar Jan 08 '20 04:01 iMerica

Every time I hit the back button on mobile the site I'm working on loads the page for a second but then the whole page goes blank, and I get the message "An unexpected error occurred."

Screen Shot 2020-02-29 at 10 59 00 PM

Only happens in mobile (on both Chrome and Safari) but not on desktop.

My code:

Screen Shot 2020-02-29 at 11 01 20 PM

To reproduce it you just visit the site on mobile and then hit the back button on your browser.

zachwhitedev avatar Mar 01 '20 07:03 zachwhitedev

@zachwhitedev I have the same problem. Do you find some solution?

stikler51 avatar Apr 23 '20 19:04 stikler51

The same thing happens to me in production and development, the second I return to the home page, I put an error capture:

Shit Markdown

I see this is closed, I have to reopen a new issue?

chiqui3d avatar Jul 23 '20 23:07 chiqui3d

I have also added shallow to the Link component, as in this example, and the error still appears:

<Link as={`/blog/${post.slug}`} href="/blog/[slug]" shallow={true}>

chiqui3d avatar Jul 23 '20 23:07 chiqui3d

I have a similar issue:

https://github.com/vercel/next.js/issues/9989#issuecomment-663958815

collegewap avatar Jul 26 '20 08:07 collegewap

@timneutkens Any update on this? This is a pretty massive issue. Is there an open issue to follow here at least?

JStein92 avatar Aug 10 '20 19:08 JStein92

@timneutkens Any update here?

Skarian avatar Aug 22 '20 15:08 Skarian

I have this issue navigating between two static pages. I have to click back twice.

  1. Start on /home
  2. Click /work_orders, navigates properly to Work Orders
  3. Back Button once, see tab title change to "Home" but page content stays as Work Orders content
  4. Back Button again, see URL and page content change back to Home content

We use a mix of:

  • CSS-in-JS (via Chakra-UI)
  • CSS Module imports (aka import styles from './SomeStyles.module.scss')
  • Global CSS imports for legacy components in _app.js

Not sure how CSS is causing the issue but wanted to share what we do.

I've tried on both v9.4.0 and v9.5.3-canary.27 and v9.5.3

timkindberg avatar Sep 05 '20 02:09 timkindberg

We're seeing this issue too - navigating forward works OK but navigating backwards doesn't work.

Tested with Next version 9.5.1 and 9.5.3

Also using Chakra, as well as global CSS import.

Link (from /reports/individual, a very large page):

<Link href="/schedule-appointment">{...}</Link>

Clicking the link once takes me to /schedule-appointment, as expected.

/schedule-appointment:

import React, { useEffect } from "react";
import { string } from "prop-types";
import { getUserFromCtx } from "../utils/auth";

function Appointment({ email }) {
  // Embed Calendly inline widget
  useEffect(function addCalendly() {
    const head = document.querySelector("head");
    const script = document.createElement("script");
    script.setAttribute(
      "src",
      "https://assets.calendly.com/assets/external/widget.js"
    );
    head.appendChild(script);
  }, []); // second param causes this to only run once

  const calendlyLink = "...";
  return (
    <div>
      <div id="schedule_form">
        <div
          className="calendly-inline-widget"
          data-url={calendlyLink + "?email=" + email}
          style={{ minWidth: "320px", height: "580px" }}
        />
      </div>
    </div>
  );
}

Appointment.propTypes = {
  email: string.isRequired,
};

export async function getServerSideProps(context) {
  const user = await getUserFromCtx(context);

  // TODO: once calendly adds "Extra Guests" prefill,
  // prefill the extra guests field with all the emails
  // associated with a user's Account
  return {
    props: {
      email: user.user.email,
    },
  };
}

export default Appointment;

Clicking the browser back button from schedule-appointment updates the route in the URL bar to /reports/individual but does not load /reports/individual's content.

Even weirder, if I click back twice (taking me to the page before I clicked the link, in this case /signin), it renders OK, then if I click forward once, I get the /schedule-appointment content rendered in /reports/individual!

If I refresh the page, I get the content for /reports/individual rendered as I expect.

Does anyone know if there's a workaround, even if it forces a hard reload of the page?

After some further investigation, I think this is caused by having a hash in the URL when navigating back. If I remove the hash, the navigation works correctly.

tomlagier avatar Sep 08 '20 03:09 tomlagier