bloggy/dev

Fix Next Link Scroll Up in production environment

https://images.ctfassets.net/aa2x2q37oe7p/39crTJo7vx5L1GL2vn3S9n/7f38e51382093019686b8ed8f01d37f7/Chris.jpeg
Christian Schuller28.11.2023 - 1 min read
https://images.ctfassets.net/aa2x2q37oe7p/50uQ569ZnL1R1HRFRBF5Jm/0f8701b5b71af06ab48f7899888e24e5/DALL_E_2023-11-28_16.35.12_-_A_contemporary_and_minimalistic_article_preview_image_in_a_4_3_aspect_ratio__highlighting_the_N.png

The problem

I recently encountered an issue in NextJS 13, when using the Next Link component. I have an article page that links to other article pages using ArticleCards at its end. When reading a blog entry and scrolling down to the "Recommended Articles" section with the ArticleCards, upon touching the ArticleCard with the mouse, the application would instantly and unexpectedly scroll up to the top of the page. This behavior would only occur in the production environment.

Reproduction Environment

  • Next 13

  • Emotion v11

  • production environment (npm run build && npm run start)

How to fix

I found a quick fix by adding the prefetch={false} flag to the Next Link component like this.

Code Snippet: Fix NextJs Link scrolling

I spent some time finding similar issues online to understand why this was happening. But in the end, I just went with this quick fix. If you know why this is happening, feel free to contact me.

I hope this helped!

Thank you for reading :)

Recommended Articles