What if you could change the URL without making your users wait for a full page reload?
Why Shallow routing concept in NextJS? - Purpose & Use Cases
Imagine you have a website where users can filter products by category. Every time they click a filter, the whole page reloads, making them wait and losing their scroll position.
Reloading the entire page for small changes is slow and frustrating. It also resets the page state, like scroll position or form inputs, causing a poor user experience.
Shallow routing lets you change the URL and update parts of the page without a full reload. This keeps the page state intact and makes navigation feel instant and smooth.
router.push('/products?category=shoes') // full reloadrouter.push('/products?category=shoes', undefined, { shallow: true }) // no reloadIt enables fast, seamless navigation that feels like a single-page app while keeping URLs in sync.
On an online store, users can switch filters or sort options quickly without losing their place or waiting for the page to reload.
Manual full page reloads cause delays and lose page state.
Shallow routing updates the URL without reloading the page.
This creates a smoother, faster user experience with preserved state.
Practice
Solution
Step 1: Understand shallow routing purpose
Shallow routing updates the URL but keeps the current page component and state intact without a full reload.Step 2: Compare options with shallow routing behavior
Only Changes the URL without reloading the page or losing component state describes changing the URL without reloading or losing state, matching shallow routing.Final Answer:
Changes the URL without reloading the page or losing component state -> Option CQuick Check:
Shallow routing = URL change without reload [OK]
- Thinking shallow routing reloads the page
- Assuming shallow routing blocks URL changes
- Confusing shallow routing with full page reload
push method?Solution
Step 1: Recall router.push syntax for shallow routing
The third argument torouter.pushis an options object whereshallow: trueenables shallow routing.Step 2: Match options with correct syntax
router.push('/page?tab=1', undefined, { shallow: true }) correctly passes{ shallow: true }as the third argument; others are incorrect or incomplete.Final Answer:
router.push('/page?tab=1', undefined, { shallow: true }) -> Option AQuick Check:
Shallow routing option is third argument object [OK]
- Passing shallow option as second argument
- Using boolean instead of options object
- Confusing reload option with shallow
const router = useRouter();
useEffect(() => {
console.log('Query changed:', router.query.tab);
}, [router.query.tab]);
const changeTab = () => {
router.push('/dashboard?tab=2', undefined, { shallow: true });
};
changeTab();What will be logged to the console after
changeTab() is called?Solution
Step 1: Understand effect dependency on router.query.tab
The effect runs whenrouter.query.tabchanges, logging its value.Step 2: Analyze shallow routing effect on query
Shallow routing updates the URL and query parameters without full reload, sorouter.query.tabupdates to '2'.Final Answer:
Query changed: 2 -> Option AQuick Check:
Shallow routing updates query and triggers effects [OK]
- Assuming shallow routing blocks query updates
- Expecting no console output
- Thinking shallow routing causes errors
const router = useRouter();
const handleClick = () => {
router.push('/profile?section=info', undefined, { shallow: true });
router.push('/profile?section=details');
};
handleClick();What is the problem with this code regarding shallow routing?
Solution
Step 1: Examine the two router.push calls
The first call uses shallow routing, the second does not and triggers a full reload.Step 2: Understand effect of second push on routing
The second push overrides the first and causes a full page reload, negating shallow routing.Final Answer:
The second push overrides shallow routing, causing a full reload -> Option DQuick Check:
Non-shallow push cancels shallow routing [OK]
- Assuming shallow routing applies to all pushes automatically
- Thinking shallow routing must be first push only
- Ignoring that non-shallow push triggers reload
Solution
Step 1: Identify shallow routing usage for tab navigation
Usingrouter.pushwith{ shallow: true }updates URL query without full reload, preserving component state.Step 2: Confirm UI updates from router.query changes
Listening torouter.queryallows UI to react to tab changes smoothly without losing state.Final Answer:
Use router.push with { shallow: true } and listen to router.query -> Option BQuick Check:
Shallow routing + query listening = smooth tab navigation [OK]
- Reloading page loses component state
- Using router.replace without shallow resets state
- Manipulating window.location breaks SPA behavior
Ready to prove it?
Pass the test and this pattern moves unknown
