0
0
NextJSframework~10 mins

Shallow routing concept in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shallow routing concept
User clicks link or calls router.push
Check if shallow routing is requested
Update URL only
Component stays
State preserved
Render updated URL
Shallow routing updates the URL without running full page data fetching or reloading, preserving component state.
Execution Sample
NextJS
import { useRouter } from 'next/router';

const router = useRouter();

router.push('/about', undefined, { shallow: true });
This code changes the URL to '/about' using shallow routing, so the page does not fully reload.
Execution Table
StepActionURL BeforeURL AfterShallow OptionComponent ReloadState Change
1Initial page load/home/homefalseYesState initialized
2User triggers router.push('/about', undefined, { shallow: true })/home/abouttrueNoState preserved
3Component renders with new URL/about/abouttrueNoState unchanged
4User triggers router.push('/contact') without shallow/about/contactfalseYesState reset
5Component reloads fully/contact/contactfalseYesState reinitialized
💡 Execution stops after full reload or when user stops navigation.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
URL/home/about/contact/contact
Component ReloadedYesNoYesYes
StateInitializedPreservedResetReinitialized
Key Moments - 2 Insights
Why does the component not reload when shallow routing is used?
Because shallow routing updates the URL without triggering Next.js data fetching methods or a full page reload, as shown in execution_table step 2 and 3.
What happens to component state when shallow routing is false?
The component fully reloads and resets its state, as seen in execution_table step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the URL after step 2?
A/home
B/about
C/contact
D/
💡 Hint
Check the 'URL After' column in row for step 2.
At which step does the component reload fully?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Component Reload' column for each step.
If shallow routing was not used at step 2, what would happen to the state?
AState would be preserved
BState would be partially preserved
CState would reset
DState would be undefined
💡 Hint
Compare state changes between steps 2 and 4 in variable_tracker.
Concept Snapshot
Shallow Routing in Next.js:
- Updates URL without full page reload
- Use router.push(url, undefined, { shallow: true })
- Preserves component state and avoids data fetching
- Without shallow, page reloads and state resets
- Useful for UI state changes reflected in URL
Full Transcript
Shallow routing in Next.js lets you change the URL without reloading the whole page or running data fetching again. When you use router.push with the shallow option set to true, the URL updates but the component stays mounted and keeps its state. This is different from normal routing where the page reloads and the component resets. The execution table shows how the URL changes and whether the component reloads or not. This helps keep UI state like form inputs or scroll position intact while changing the URL. If shallow routing is not used, the component reloads and state resets. This concept is useful for smoother user experiences when only the URL needs to change.