Next.js Discord

Discord Forum

next/link jumps down the page with no heading set on the href

Unanswered
Common paper wasp posted this in #help-forum
Open in Discord
Common paper waspOP
When I click on one of my links for my blog articles, it jumps down the page and I cannot figure out why. You'll notice in the gif that it doesn't do it on the first time I click the first article but it does it the second time. Let me know what code you want to see and I'll be happy to share. Don't want to bloat the post with something that isn't needed

58 Replies

Common paper waspOP
Here is my Layout for my /blog/[slug]/layout.tsx
Common paper waspOP
It happens if I click any blog post link from anywhere too, not just inside a post
Common paper waspOP
Does it in all browsers, and incognito
Common paper waspOP
Lol this is killing me. It didn't do this until I made some changes to the page and layout and what was rendered where but I don't think that would matter. I was just refactoring
Chinese Chongqing Dog
I think it is, because at the first render the page is smaller (you are not showing the whole post), but on the second render you are showing the full post and that messes up the browser scrollposition somehow
Common paper waspOP
Ahhhhh that makes sense. Then I probably need to put all of this into the page then?
The reason I moved it out of the page was because I had everything in the section twice. I needed it twice for the flag check that I'm doing
Chinese Chongqing Dog
In my understanding getPostsBySlug and getPreviousPost return different Pages (on where the Blog Post is cropped, because you are not allowed to show the solution and the other where the whole post is revealed)

Are you checking on the Server Side that the content of the blog is showing? Or on the client Side?

What do you mean with moved it out of the page?
Common paper waspOP
I'll have to take a look. But I moved everything in the layout above out from the page
Into the layout
Because I had the entire section twice
Chinese Chongqing Dog
Ok, but keep in mind the browser keeps track of the scrollposition of pages and when you load a Page and somehow it has more content than before (which you should prevent on static pages) than this messes up the scroll position
Common paper waspOP
Idk how to fix that though
Chinese Chongqing Dog
Well first of all where do you check that it should show the full content?
Common paper waspOP
The page
Chinese Chongqing Dog
Ok and how, because on the second render it shows the full blog
Common paper waspOP
What do you mean?
Chinese Chongqing Dog
can you show me the code of the page not the layout.tsx?
Chinese Chongqing Dog
Hmm I don't know but in the Video you are showing the full Blog Post even though isMachineActive should be true
Common paper waspOP
Two different posts
Chinese Chongqing Dog
In your Code both have isMachineActive on "true"
Common paper waspOP
Now they do
Common paper waspOP
Not sure where to look lol
Common paper waspOP
I can try that. Not sure why I all of a sudden need to though. I think that's what I'm trying to figure out
@!=tgt try setting the `scroll` prop on the link component to false
Common paper waspOP
I'll give this a try tonight once the kids go to sleep
Common paper waspOP
Still does it
why tf is it scrolling
this is driving me insane
Here's an updated page

import { getPostBySlug } from "@/lib/utils";
import { Metadata } from "next";
import { notFound } from "next/navigation";
import { cache } from "react";

type Props = {
  params: {
    slug: string;
  };
};

const getPostBySlugInPage = cache(async (slug: any) => {
  const post = await getPostBySlug(slug);
  return {
    title: post?.title,
    description: post?.description,
  };
});

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const data = await getPostBySlugInPage(params.slug);
  return {
    title: data?.title,
    description: data?.description,
    openGraph: {
      title: data?.title,
      description: data?.description,
    },
  };
}

export default function BlogPost({ params }: { params: { slug: string } }) {
  const post = getPostBySlug(params.slug);

  // If the blog post requested does not exist, throw a 404 using Next's notFound function - neat!
  // https://nextjs.org/docs/app/api-reference/functions/not-found
  if (post == null) notFound();

  return post.isMachineActive ? (
    <section>
      <p>こんにちわ! Hello!👋</p>
      <p>
        This machine is still active. Per Hack The Box rules, this writeup will
        be released once the machine is retired and writeups are allowed to be
        posted.
      </p>
    </section>
  ) : (
    <div dangerouslySetInnerHTML={{ __html: post.content }}></div>
  );
}
Updated layout
Common paper waspOP
I don't think I'm doing anything unique or wrong with this layout and page combination
The layout determines what should go into the page and then the page is the content that is added into the children slot
And in this case, the content is determined by a flag in the markdown/post
Doesn't seem super complex or unusual
Common paper waspOP
It seems to only be the links to the posts now
And they always jump to the Toc
Common paper waspOP
literally no idea why this is happening all of a sudden
Common paper waspOP
a cache refresh (Ctrl+Shift+R) refreshes it to the top of the page
a regular F5 does not
and it keeps position
Common paper waspOP
Still looking for help
Common paper waspOP
I'll see if it does it on a new post today
Common paper waspOP
Otherwise I got nothing
Common paper waspOP
IM SO CONFUSED lol
I'm testing it again now just my two posts and now only ONE of them jumps instead of both
Well, kinda I guess?
If I scroll up enough to just barely see the "Next Article" link and click it, it stays at the same scroll position and doesn't scroll to the top like id expect
WHY
Common paper waspOP
Analyzing the Response payload to a request to one of my posts I see these weird numbered keys with json values that I've never seen before
Like

1:D{"name":"", "env":"Server"}
2:D{"name":"BlogPost, "env":"Server"}
3:T2fde, ...post html starts here
Common paper waspOP
In fact actually, none of my Links are scrolling to the top like they should by default
Common paper waspOP
I give up
Common paper waspOP
Explicit prefetch doesn't help either. Wtf