Performance: Shallow routing concept
Shallow routing affects page navigation speed by updating the URL without a full page reload or data fetching, improving user experience during route changes.
import { useRouter } from 'next/router'; const router = useRouter(); // Navigating with shallow routing router.push('/about', undefined, { shallow: true });
import { useRouter } from 'next/router'; const router = useRouter(); // Navigating with full reload router.push('/about');
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page navigation | Many DOM updates due to reload | Multiple reflows triggered | High paint cost due to full reload | [X] Bad |
| Shallow routing | Minimal DOM updates, URL change only | Single reflow or none | Low paint cost, smooth transition | [OK] Good |
push method?router.push is an options object where shallow: true enables shallow routing.{ shallow: true } as the third argument; others are incorrect or incomplete.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();changeTab() is called?router.query.tab changes, logging its value.router.query.tab updates to '2'.const router = useRouter();
const handleClick = () => {
router.push('/profile?section=info', undefined, { shallow: true });
router.push('/profile?section=details');
};
handleClick();router.push with { shallow: true } updates URL query without full reload, preserving component state.router.query allows UI to react to tab changes smoothly without losing state.Pass the test and this pattern moves unknown