Why next still fetch page on navigation? (with prefetch on links)
Unanswered
Common paper wasp posted this in #help-forum
Common paper waspOP
Hello,
Probably I miss something but couldn't figure out what exactly. I have a simple app (next@15.1.6, trpc@11.0.0-rc.730).
links in header:
Home page:
Settings page:
On first app load I see
the app folder structure:
Probably I miss something but couldn't figure out what exactly. I have a simple app (next@15.1.6, trpc@11.0.0-rc.730).
links in header:
<Link href="/" prefetch>
Home
</Link>
<Link href="/settings" prefetch>
Settings
</Link>
Home page:
export default async function Home() {
const session = await getSession();
if (!session) {
return redirect('/login');
}
api.bookmark.all.prefetch();
return (
<HydrateClient>
<div className="p-4 md:p-6">
<Suspense fallback={<BookmarkGridSkeleton />}>
<BookmarkGrid />
</Suspense>
</div>
</HydrateClient>
);
}
Settings page:
export default async function SettingsPage() {
const session = await getSession();
if (!session) {
return redirect('/login');
}
return (
<div>
<h1 className="text-lg font-semibold">Settings</h1>
</div>
);
}
On first app load I see
/settings?_rsc=1cj86
in network tab so it's prefetch and navigation works instantly. But when I click on home link the navigation is slow because it fetch /?_rsc=1vy31
everytime I go to home page. Why?the app folder structure:
14 Replies
Common paper waspOP
Asiatic Lion
I think I have the same issue. I have a page that prefetches some queries (using tanstack query), and when I navigate same page with a different search param. The page component renders and prefetches the queries again.
I found this repo that works like how I want it to work. And if I upgrade nextjs to 15, that app has the same problem that mine does now.
I'm tying to find what has changed between nextjs 14 and 15 for this to happen. Does anybody have an idea?
I found this repo that works like how I want it to work. And if I upgrade nextjs to 15, that app has the same problem that mine does now.
I'm tying to find what has changed between nextjs 14 and 15 for this to happen. Does anybody have an idea?
@Asiatic Lion I think I have the same issue. I have a page that prefetches some queries (using tanstack query), and when I navigate same page with a different search param. The page component renders and prefetches the queries again.
I found this repo that works like how I want it to work. And if I upgrade nextjs to 15, that app has the same problem that mine does now.
I'm tying to find what has changed between nextjs 14 and 15 for this to happen. Does anybody have an idea?
Black Turnstone
Next.js 15 changes how route handlers and client routes are cached. In nextjs 14 they are cached by default but in 15 the are no longer cached by default so you have to specify it to get the same behaviour as before.
@Black Turnstone Next.js 15 changes how route handlers and client routes are cached. In nextjs 14 they are cached by default but in 15 the are no longer cached by default so you have to specify it to get the same behaviour as before.
Asiatic Lion
Thank you. That’s what I found while digging into this. By specifying it, do you mean setting staleTimes in the nextjs config?
Black Turnstone
Yes, you can also use "use cache" with cacheLife and cacheTags
Refer: https://nextjs.org/docs/app/api-reference/functions/cacheLife
Refer: https://nextjs.org/docs/app/api-reference/functions/cacheLife
@Black Turnstone Yes, you can also use "use cache" with cacheLife and cacheTags
Refer: https://nextjs.org/docs/app/api-reference/functions/cacheLife
Common paper waspOP
but this is not applicable if I check auth session of every page right?
@Common paper wasp but this is not applicable if I check auth session of every page right?
No, page becomes dynamic as soon as you access cookies or any request-time APIs
@LuisLl No, page becomes dynamic as soon as you access cookies or any request-time APIs
Common paper waspOP
Yeah right. Is there a way to get rid of delay between navigating? Like in spa. Move checking session into middleware?
That will take time too, you'd be awaiting on middleware even before starting rendering your page but that would allow pages to be static if they don't depend on request-time data to work.
In those cases middleware is the only place you can check for the session before getting access to the (static) page.
In those cases middleware is the only place you can check for the session before getting access to the (static) page.
Not very sure if middleware will make waiting times more eficient (it should) since it runs on the edge instead of the Server
Common paper waspOP
I am self hosting app tho
@Common paper wasp Click to see attachment
This behavior is expected when routes are dynamic since they need the latest request-data
@Common paper wasp I am self hosting app tho
I'm not suitable for that usecase tbh, I've never self-hosted :c