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

App Router in Next v14.1.0 slower than v13.4.6

Open amavisse opened this issue 1 year ago • 2 comments

Link to the code that reproduces this issue

https://github.com/amavisse/next14-test

To Reproduce

I reproduced the issue here: https://next13-test-teal.vercel.app/ - repo: https://github.com/amavisse/next13-test https://next14-test-delta.vercel.app/ - repo: https://github.com/amavisse/next14-test

  1. Visit both links
  2. Click on any of the top links to change route and notice the difference in loading page

Current vs. Expected behavior

Current: page loading is very slow on next v14.1.0

Expected: fast loading as in https://next13-test-teal.vercel.app/

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Home
Binaries:
  Node: 20.2.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 13.4.6
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3

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

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

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

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

next dev (local), Vercel (Deployed)

Additional context

In the next v13.4.6, everything is going smoothly, while the v14.1.0 one is very slow when changing routes Inspecting the dev console, inside the network tab, you can see the routes are correctly pre-fetched but when moving to the route, it's fetching it again, making it extremely slower.

Am I doing something wrong? bad practices? is it a bug?

amavisse avatar Jan 27 '24 16:01 amavisse

Could be related https://github.com/vercel/next.js/pull/61203

OlegLustenko avatar Jan 27 '24 22:01 OlegLustenko

14.1.0

https://github.com/vercel/next.js/assets/120719830/e0c36d51-c845-42f6-a9a1-408494b46b85


14.0.3.canarey-7:

https://github.com/vercel/next.js/assets/120719830/a62a99b9-7710-4c0c-8c7d-d15daeb39398


A bug with caching routes, when hovering over next/link, the route is loaded again. Slow 3g is selected in the video, but even with fast Internet, the behavior is exactly the same.

Prefetch also does not load css.

It took a lot of time to figure out what the problem was.

The biggest mistake is to believe that the new versions are stable.

repcolding avatar Jan 30 '24 19:01 repcolding

Related to #48748 and I believe it it related to optimizePackageImports not working properly (I'm compiling 10k+ modules per route aprox)

Yhozen avatar Feb 05 '24 19:02 Yhozen

Hi @amavisse -- thanks for raising this issue. Referencing the Prefetching Docs here, it seems like this is working as intended (in the latest version) and was actually a bug in the older version.

Specifically, note the following section of docs:

The Link's prefetching behavior is different for static and dynamic routes: Static Routes: prefetch defaults to true. The entire route is prefetched and cached. Dynamic Routes: prefetch default to automatic. Only the shared layout, down the rendered "tree" of components until the first loading.js file, is prefetched and cached for 30s. This reduces the cost of fetching an entire dynamic route, and it means you can show an instant loading state for better visual feedback to users.

In other words, if the prefetch argument to the Link tag is left unspecified, it will default to a partial prefetch (essentially up to the nearest loading boundary, if there is one). This is happening in your case, because each [...slug] page is considered a dynamic route.

Fortunately there's a way to opt-in to the behavior you're describing: if you pass prefetch={true} to your Link components, it will tell the router to perform a full prefetch of each of the pages (including the dynamic page data), and will instantly navigate without triggering a follow-up fetch.

In other words, update your Navigation component to look like this:

function Navigation() {
  const links = [
    { href: '/', name: 'Home' },
    { href: '/page1', name: 'Page1' },
    { href: '/page2', name: 'Page2' },
    { href: '/page3', name: 'Page3' },
  ]
  return (
    <nav className="flex flex-row justify-center gap-5 py-20">
      {links.map((link, i) => (
        <Link
          className="hover:underline"
          key={link.name + i}
          href={link.href}
          prefetch={true} // note the prefetch prop here
        >
          {link.name}
        </Link>
      ))}
    </nav>
  )
}

I'm going to close this as it seems to be working as designed, but if you have any questions about it, please don't hesitate to reply to this or raise in our Discussions forum.

ztanner avatar Feb 07 '24 15:02 ztanner

@ztanner Hi, if everything works as it should, that's suck

I checked the version [email protected] and 14.0.3-canary.7. Specifying prefetch={true}. And the result is terrible:

[email protected]: No preloading of css [email protected]: css is preloaded

const Folder: FC<IFolder> = ({ children, label, className, href = '#' }) => {
  return (
    <Link
      prefetch={true} // [email protected] and [email protected]
      scroll={false}
      href={href}
      className={cn(styles.folder, className)}
    >
      {children}

      <div className={styles.label}>{label}</div>
    </Link>
  )
}

[email protected]

next@14.0.3-canary.7

prefetch={true} preload css is working


[email protected]

next@14.1.0

prefetch={true} preload css doesn't working

repcolding avatar Feb 08 '24 08:02 repcolding

Hi @repcolding -- I didn't state that everything is working as expected, just that prefetching semantics in the version you mentioned were not the intended behavior. If there's a separate bug with CSS loading, we can look into that.

ztanner avatar Feb 08 '24 19:02 ztanner

I am facing the same issue , Next js 14 version Link component is terribly slow , navigating between pages takes hell out of you . @ztanner Please make sure higher versions are stable .

jinowac avatar Feb 19 '24 18:02 jinowac

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[bot] avatar Mar 05 '24 00:03 github-actions[bot]