Nextjs reloads page when i go to not-found page
Link to the code that reproduces this issue
https://stackblitz.com/edit/stackblitz-starters-heagtm
To Reproduce
click button "Plus 1" click button "go to not found" page will be reloaded after reloaded page loose context state
Current vs. Expected behavior
Current behavior: reloads page Expected: just rendering it without reload page
'use client';
import { useContext } from './Context';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
export default function Page() {
const router = useRouter();
const { id, setId } = useContext();
return (
<>
<Link href="/page-not-found">page-not-found</Link> /
<button type="button" onClick={() => router.push('/page-not-found')}>
Go to not found
</button>{' '}
/
<button type="button" onClick={() => setId(id + 1)}>
Plus 1
</button>
<div>id: {id}</div>
</>
);
}
Provide environment information
stackblitz.com:
node: 18.20.3
next: 13.5.1
my host:
node: 20.11.0
next: 14.2.3
Which area(s) are affected? (Select all that apply)
Navigation, Pages Router
Which stage(s) are affected? (Select all that apply)
next dev (local), next start (local),
Additional context
No response
Whenever you use client side navigation like link, router push you create a new component tree on each transition and therefor you lose context or it resets to its initial value, which in your case would be 0.
iampoul thank you. but I think Context.Provider, which holds the shared context values, remains persistent across component instances. this demo without getting a page "not-found" work fine. I have read the documentation not-found.js , it does not say that there will be a page reload or loss of context, I apologize if I do not know something
This issue has been automatically marked as stale due to 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.
This issue has been automatically closed due to inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding!