Thanks to visit codestin.com
Credit goes to github.com

Skip to content

External library code gets loaded on blank page where library is not used #57337

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

Open
1 task done
smitbarmase opened this issue Oct 24, 2023 · 5 comments
Open
1 task done
Labels
bug Issue was opened via the bug report template. Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation. stale The issue has not seen recent activity.

Comments

@smitbarmase
Copy link
Contributor

Link to the code that reproduces this issue

https://github.com/smitbarmase/webpack-bundle-bug

To Reproduce

  1. Build the application (next build)
  2. Start the application (next start)
  3. Notice swiper is only used on / page and not on /about-us.
  4. Open network tab, navigate to /about-us, search for swiper keyword in chunks.
  5. Hard refresh /about-us multiple times, you will see swiper chunks still getting downloaded.
  • Turn off cache in network tab if not able to reproduce *

Screenshot of network tab of /about-us after searching swiper in chunks:
image

Current vs. Expected behavior

Currently, swiper chunks getting downloaded on /about-us page, which is empty.

Expected behavior would be swiper chunks gets downloaded only on home page / as swiper is used only on that page.

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 20.6.1
  npm: 9.8.1
  Yarn: N/A
  pnpm: 8.7.1
Relevant Packages:
  next: 13.5.7-canary.23
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.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)

Additional context

No response

@smitbarmase smitbarmase added the bug Issue was opened via the bug report template. label Oct 24, 2023
@github-actions github-actions bot added the Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation. label Oct 24, 2023
@smitbarmase smitbarmase reopened this Oct 28, 2023
@gnoff
Copy link
Contributor

gnoff commented Nov 2, 2023

Hi @smitbarmase Next.js has a prefetching mechanism that loads the instant-loading-state content of links on the page. Your /about-us page has a layout (in app/(nav)/) that has a link to /. Next is prefetching the / route when /about-us loads so that it can transition instantly if you click this link. When we prefetch the / page we also start loading the code associated with that page (otherwise the transition would be blocked on this code loading)

Aside from it being surprising if you aren't aware of the prefetching behavior is the the fetchign of swiper problematic?

@smitbarmase
Copy link
Contributor Author

Hey @gnoff , I am aware of the prefetching behavior of Next.js's Link component. I am using the <QueryLink> component, which is essentially a wrapper around <Link>. However, as you can see, I am using prefetch={false} on Link. So, why is it still prefetching?

Additionally, the Swiper library introduces 40-50 KB of redundant code to pages that do not use it.

@gnoff
Copy link
Contributor

gnoff commented Nov 6, 2023

@smitbarmase indeed I missed that :)

I've taken a closer look and there does seem to be something up with the dependencies between a client component and that chunk. Initially I thought it might be an instance where webpack makes fewer larger chunks to avoid having too many small ones and it just happens to include this chunk on the /about-us page because something QueryLinkClient depends on was also in that chunk. However that doesn't actually seem to be the case. I'll follow up with more info when I have it

@smitbarmase
Copy link
Contributor Author

smitbarmase commented Nov 7, 2023

Sure, @gnoff, it seems there's an issue with how Webpack bundles things. Also, the workaround I've found is to make a copy of the QueryLink and QueryLinkClient components, and then use that copy in the layout. This somehow prevents the issue.

@nextjs-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

3 participants