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

[v13 + appDir] prefetch={false} breaks client-side navigation

Open apostolos opened this issue 4 years ago • 2 comments

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Wed Aug 24 22:24:20 UTC 2022
Binaries:
  Node: 19.0.0
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: 7.14.1
Relevant packages:
  next: 13.0.1-canary.1
  eslint-config-next: 13.0.1-canary.1
  react: 0.0.0-experimental-cf3932be5-20221027
  react-dom: 0.0.0-experimental-cf3932be5-20221027

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Adding prefetch={false} to a link breaks client-side navigation. Clicking on the link causes a full page navigation (no error is thrown).

IMPORTANT: If there is another for the same path with prefetch enabled, then the Link with prefetch=false also works correctly. You can uncomment the 2nd link in the StackBlitz to reproduce that behavior as well.

Expected Behavior

Clicking on a link with prefetch={false} should perform a client-side navigation.

Link to reproduction

https://stackblitz.com/edit/vercel-next-js-no-client-side-nav?file=app%2Fpage.tsx

To Reproduce

Open homepage and click on the link. You can confirm the full page navigation via the browser's console.

apostolos avatar Oct 30 '22 12:10 apostolos

I can confirm. I am seeing similar issues when trying to prefetch dynamic routes like app/test/[testId]/leaf/page.tsx where the prefetching systematically fails on Vercel (but works fine with next dev & start) and leads to a complete page reload. I am also seeing the same issue when setting headers to the request in the middleware (as allowed in Next 13 https://nextjs.org/docs/advanced-features/middleware), prefetching systematically fails/halt.

Smirow avatar Oct 30 '22 17:10 Smirow

So the behavior you described is due to forcing OptimisticNavigation when calling the router in link.tsx when prefetching is disabled. Not sure it was the intended behavior, but seems like it.

https://github.com/vercel/next.js/blob/0affdebf67b0b94d409285906e250acb19b2cca1/packages/next/client/link.tsx#L186-L188

https://github.com/vercel/next.js/blob/4316d3884e1ab7c4cec94c21e4b4928d33eeb079/packages/next/client/components/reducer.ts#L835-L839

The issue on Vercel does not seem to be related to this.

Smirow avatar Oct 31 '22 07:10 Smirow

Same issue with prefetch={false}

jackypan1989 avatar Jan 09 '23 12:01 jackypan1989

Any news on this?

Hanskrogh avatar Feb 10 '23 08:02 Hanskrogh

I think that @timneutkens told before in another issue that is currently the excepted behavior. Can you confirm?

Smirow avatar Feb 10 '23 09:02 Smirow

Let's say we have a page with 50 visible links (let's name it contest) to other pages (contest_result/[id]). Contest results is a page that has let's say 500kb of data that needs to be loaded using SSR. Right now the behavior is either 1 - prefetch is enabled - all links are prefetched but in this case it is 25mb of prefetched data (500kb*50links) and client navigation is working 2 - prefetch is disabled - we don't prefetch 25mb of data but if we follow the link client side navigation is broken and we have full page refresh which is missing the whole point of using react/nextjs

i don't understand how this would be expected behavior. so what should we do in above case? don't use app dir and go back to old/current aproach with pages and "api pages"?

isilweo avatar Feb 10 '23 09:02 isilweo

see https://github.com/vercel/next.js/pull/43730#issuecomment-1357362975

Smirow avatar Feb 10 '23 11:02 Smirow

And probably linked to soft/hard navigation: On navigation, Next.js will use soft navigation if the route you are navigating to has been prefetched, and either doesn't include dynamic segments or has the same dynamic parameters as the current route.

https://beta.nextjs.org/docs/routing/linking-and-navigating#conditions-for-soft-navigation

Smirow avatar Feb 10 '23 17:02 Smirow

I can confirm with an internationalized appDir (i.e. a root [locale] folder in appDir) that every single with prefetch={false} does not work. I need to set prefetch={true} (default behaviour) in order that all links are working. This cannot possibly be expected behaviour as linking with internationalization via pages directory was working perfectly.

astateful avatar Apr 20 '23 15:04 astateful

Verified the issue that was originally reported with the provided reproduction has been fixed a while back.

@astateful please open a new issue with a full reproduction, it's not expected that it would hang.

timneutkens avatar Apr 21 '23 11:04 timneutkens

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

github-actions[bot] avatar May 21 '23 12:05 github-actions[bot]