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

Next/link is very slow sometimes it doesn't work #53624

Closed
1 task done
AsitDixit opened this issue Aug 6, 2023 · 6 comments
Closed
1 task done

Next/link is very slow sometimes it doesn't work #53624

AsitDixit opened this issue Aug 6, 2023 · 6 comments
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation. please add a complete reproduction The issue lacks information for further investigation

Comments

@AsitDixit
Copy link

Verify canary release

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

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Home Single Language
    Binaries:
      Node: 18.16.1
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.12
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.5
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

--

To Reproduce

create a dashboard file inside app create layout.tsx in the dashboard add side nav component in it. now add the tab1,2,3,4,5 file inside dashboard and add page.tsx to each tab file.

Now add the react-apexcharts and react-table( install it).

create random table & charts inside the page.tsx of tabs (requires use client).

Now add the Link of all tabs in the side nav components:

Side nav:

export default function SideNavigation() {
  const pathname = usePathname();

  const { isCollapsed, toggleSidebarcollapse } = useContext(SidebarContext);

  return (
    <div className="flex flex-col gap-3 relative ml-3">
      <div className="flex justify-center items-center py-1 rounded-md mt-2">
        <Image src={logo} alt={""} width={28} height={28} />
        <b className={`text-xl ml-3 ${isCollapsed ? "hidden" : "visible"}`}>
          APP NAME
        </b>
      </div>

      <div
        className="absolute rounded-r-full w-5 h-8 -right-[21px] top-20 bg-black flex justify-center items-center"
        onClick={toggleSidebarcollapse}
      >
        {isCollapsed ? (
          <AiFillCaretRight color="white" />
        ) : (
          <AiFillCaretLeft color="white" />
        )}
      </div>

      <Link href={"/dashboard/tab1"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          }  items-center rounded-lg py-2 ${
            pathname == "/dashboard/home"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <BiSolidHomeSmile size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Tab1
          </h1>
        </div>
      </Link>

      <div
        className={`h-[0.5px] py-1 border-t-[0.5px] border-zinc-600 ${
          !isCollapsed ? "hidden" : "visible"
        }`}
      ></div>
      <h1
        className={`pl-8 text-sm text-zinc-400 ${
          isCollapsed ? "hidden" : "visible"
        }`}
      >
        SERVICES
      </h1>

      <Link href={"/dashboard/tab2"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          } items-center rounded-lg py-2 ${
            pathname == "/dashboard/analytics"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <IoMdAnalytics size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Tab2
          </h1>
        </div>
      </Link>

      <Link href={"/dashboard/tab3"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          } items-center rounded-lg py-2 ${
            pathname == "/dashboard/financial"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <MdPayments size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Tab 3
          </h1>
        </div>
      </Link>
      <Link href={"/dashboard/tab4"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          } items-center rounded-lg py-2 ${
            pathname == "/dashboard/payroll"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <MdOutlineCurrencyExchange size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Tab 4
          </h1>
        </div>
      </Link>

      <Link href={"/dashboard/tab5"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          } items-center rounded-lg py-2 ${
            pathname == "/dashboard/legal-consulting"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <FaHammer size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Tab 5
          </h1>
        </div>
      </Link>
      <div
        className={`h-[0.5px] py-1 border-t-[0.5px] border-zinc-600 ${
          !isCollapsed ? "hidden" : "visible"
        }`}
      ></div>
      <h1
        className={`pl-8 text-sm text-zinc-400 ${
          isCollapsed ? "hidden" : "visible"
        }`}
      >
        GENERAL
      </h1>

      <Link href={"/dashboard/tab6"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          } items-center rounded-lg py-2 ${
            pathname == "/dashboard/customization"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <MdDashboardCustomize size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Tab 6
          </h1>
        </div>
      </Link>

      <Link href={"/dashboard/tab7"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          } items-center rounded-lg py-2 ${
            pathname == "/dashboard/settings"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <AiTwotoneSetting size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Tab 7
          </h1>
        </div>
      </Link>
    </div>
  );
}

Describe the Bug

react-apexcharts & react-table make next/link navigation slow, when I removed all the codes of react-apexcharts & react-table form the pages and added simple div tag with a text, the next/link navigation worked very fast. also removed the "use client"

I figured out it is showing 5-10 second delay sometimes it does not load the page, and when click back the URL is changing but the content are not re-rendering. and when I tried to remove the react-apexcharts & react-table code it worked as expected. I don't know, is this a bug of Next/link ?

and I am sure I have implemented it properly because when I hover over the Link wrapped div I see the URL in the left-bottom. the reason might be chart and table making the code heavy and render takes time in client side.

My code:

Layout:

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body className= 'bg-black'>
        <div className="flex flex-row relative overflow-hidden">

          <div className="sticky top-0 left-0 bottom-0">
            <SidebarProvider>
              <SideNavigation />
            </SidebarProvider>
          </div>

          <div className="flex-1 bg-white overflow-scroll m-2 overflow-x-hidden rounded-xl h-[97.8vh]">
            {children}
          </div>
        </div>
      </body>
    </html>
  );
}

Sidebar Link tag:

      <Link href={"/dashboard/analytics"}>
        <div
          className={`flex pl-4 pr-4 gap-2 text-sm ${
            isCollapsed ? "justify-center" : "justify-start"
          } items-center rounded-lg py-2 ${
            pathname == "/testboard/analytics"
              ? "bg-green-600 text-zinc-950"
              : "hover:bg-zinc-800"
          }`}
        >
          <IoMdAnalytics size={22} />
          <h1
            className={`text-sm font-semibold ml-1 ${
              isCollapsed ? "hidden" : "visible"
            }`}
          >
            Analytics
          </h1>
        </div>
      </Link>

Analytics page:

"use client";

import dynamic from "next/dynamic";

const Chart = dynamic(() => import("react-apexcharts"), { ssr: false });

import { StackedBarOptions } from "@/graph-style/StackedBarOptions";
import { LineBarOptions } from "@/graph-style/LineBarOptions";
import { Horizontal100Options } from "@/graph-style/Horizontal100Options";
import FinanceDetailsCard from "@/components/FinanceDetailsCard";
import CompactViewCard from "@/components/CompactViewCard";
import CommonHeader from "@/components/CommonHeader";

export default function Analytics() {
  const data = [
    {
      name: "Previous week",
      data: [44, 55, 41, 67, 22, 43],
    },
    {
      name: "This week",
      data: [13, 23, 20, 8, 13, 27],
    },
  ];

  const tree = [
    {
      data: [
        {
          x: "New Delhi",
          y: 218,
        },
        {
          x: "Kolkata",
          y: 149,
        },
        {
          x: "Mumbai",
          y: 184,
        },
        {
          x: "Ahmedabad",
          y: 55,
        },
        {
          x: "Bangaluru",
          y: 84,
        },
        {
          x: "Pune",
          y: 31,
        },
        {
          x: "Chennai",
          y: 70,
        },
        {
          x: "Jaipur",
          y: 30,
        },
        {
          x: "Surat",
          y: 44,
        },
        {
          x: "Hyderabad",
          y: 68,
        },
        {
          x: "Lucknow",
          y: 28,
        },
        {
          x: "Indore",
          y: 19,
        },
        {
          x: "Kanpur",
          y: 29,
        },
      ],
    },
  ];

  return (
    <>
      <CommonHeader heading={"Analytics"} />

      <div className="flex h-[300px] text-black justify-center  items-center px-2 pt-2 gap-4">
        <div className="grid grid-cols-2 h-full w-[35%] gap-4">
          <CompactViewCard
            heading="Customers"
            amount="3,781"
            growth="+5.27%"
            status="rise"
            bgColor="bg-emerald-400"
          />
          <CompactViewCard
            heading="Order"
            amount="3,219"
            growth="+1.78%"
            status="rise"
            bgColor="bg-red-700"
          />
          <CompactViewCard
            heading="Revenue"
            amount="$695"
            growth="-0.56%"
            status="fall"
            bgColor="bg-sky-400"
          />
          <CompactViewCard
            heading="Growth"
            amount="30.1%"
            growth="+1.48%"
            status="rise"
            bgColor="bg-purple-800"
          />
        </div>
        <div className="flex justify-center items-center w-[65%] h-full bg-zinc-100 rounded-2xl">

      
          <Chart
            // @ts-ignore:next-line
            options={StackedBarOptions}
            series={data}
            type="bar"
            width={800}
            height={250}
          />
   



        </div>
      </div>

      <div className="flex h-[34%] text-black justify-center items-center p-2 gap-4">
        <div className="flex justify-center items-center w-[75%] h-full bg-zinc-100 rounded-2xl">
          <Chart
            // @ts-ignore:next-line
            options={LineBarOptions}
            series={data}
            type="line"
            width={900}
            height={200}
          />
        </div>
        <div className="w-[25%] h-full bg-zinc-100 rounded-2xl flex justify-center items-center">
          <Chart
            options={Horizontal100Options}
            series={tree}
            type="treemap"
            width={300}
            height={200}
          />
        </div>
      </div>

      <div className=" flex h-[20%] gap-3 px-2 pb-2">
        <FinanceDetailsCard
          heading="International Merchant Account"
          net_earning="$6840"
          change="80%"
          fees="$1240"
          message="Last 30 day earnings calculated. Apart from arranging the order of topics."
        />

        <FinanceDetailsCard
          heading="International Merchant Account"
          net_earning="$6840"
          change="80%"
          fees="$1240"
          message="Last 30 day earnings calculated. Apart from arranging the order of topics."
        />

        <FinanceDetailsCard
          heading="International Merchant Account"
          net_earning="$6840"
          change="80%"
          fees="$1240"
          message="Last 30 day earnings calculated. Apart from arranging the order of topics."
        />
      </div>
    </>
  );
}

Expected Behavior

I was expecting smooth delay free navigation.

Which browser are you using? (if relevant)

Google Chrome Version 115.0.5790.110 (Official Build) (64-bit)

How are you deploying your application? (if relevant)

npm run dev

@AsitDixit AsitDixit added the bug Issue was opened via the bug report template. label Aug 6, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Aug 6, 2023
@balazsorban44 balazsorban44 added the please add a complete reproduction The issue lacks information for further investigation label Aug 7, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2023

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

@mareksuscak
Copy link

mareksuscak commented Sep 9, 2023

I think we're currently experiencing this outside of Vercel, in standalone mode where Next is deployed in a Docker container running on Cloud Run (in CPU always allocated configuration with minimum number of instances set to 1 so cold starts should be minimal). There's got to be something else that's causing this - either the number of dependencies or getInitialProps in the custom Document? Could that be the culprit. We are pulling some data but navigating between pages takes 2-4s when clicking a Next Link whereas reloading the page directly takes a lot less time (~1s). We tried the latest Canary release (13.4.20) to no avail. We use Pages Router, not the App Router. This app uses CSS-in-JS (via Emotion) and Mantine UI (+SSR). We use Next Auth for authentication. This is a fairly complex and robust app and I don't think I'd be able to provide a minimum reproducible example but I can provide a Trace from Chrome and a screenshot that shows Next.js-render phase taking over 5s.

CleanShot 2023-09-09 at 04 56 37

Trace-20230909T045419.json.zip

@monolithed
Copy link

monolithed commented Sep 11, 2023

mareksuscak, I have a similar issue where my page is mostly static, and the initial load takes 2-5 seconds. I can't reproduce it locally; it only occurs on the Vercel server.

@mareksuscak
Copy link

mareksuscak commented Sep 11, 2023

We just figured it out - we were using mobx v6 and mobx-react-lite v4 w/ Server-Side State Hydration on the client that's based on the official example. We determined a difference in behavior between v3 and v4 of mobx-react-lite where the latter causes this siginificant performance issue when hydrating the server-side state on the client. A workaround was to downgrade to v3. The mobx team doesn't seem to be aware of this issue so we'll be reporting this. My recommendation to you @monolithed would be to profile your app and check for things in the critical rendering path (custom document, custom app, pages) that might be causing a similar performance issue.

That said, navigation still isn't instant but it's many times faster and the time it takes to navigate now more or less reflects the time it takes to pull the data on the server and hydrate on the client.

@balazsorban44
Copy link
Member

I'm going to close this for now. Investigating slow cases is different from codebase to codebase, so we really need a reproduction to be able to effectively find the root cause. If you are having this issue, we ask you to open a new bug report with a provided code example, and any trace information you might have. Thanks!

(Feel free to reach out to me with private examples/repros if that's a blocker)

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 Mar 13, 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 Navigation Related to Next.js linking (e.g., <Link>) and navigation. please add a complete reproduction The issue lacks information for further investigation
Projects
None yet
Development

No branches or pull requests

4 participants