I have a page that uses router.query to determine what content to render.
However, it looks like the page will always render without router.query first before loading the router.query code in resulting in the page "flickering"
Here is a simple stackblitz to show what I mean: https://stackblitz.com/edit/nextjs-jvtyrm?file=pages/index.js
Relevant code bits:
export default function Home() {
const router = useRouter();
const isRouterQuery = router.query.foo ? true : false;
return (
<div className={styles.container}>
<main className={styles.main}>
<button
onClick={() => {
if (isRouterQuery) {
window.location.href = '/';
} else {
window.location.href = '/?foo=bar';
}
}}
>
Toggle between homepage and homepage with router.query
</button>
{isRouterQuery ? (
<h1 className={styles.title}>Router Header</h1>
) : (
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
)}
// ...
If you click the button to toggle from / to /?foo=bar, you'll see that the initial render shows Welcome to Next.js for a split second before re-rendering to show the Router Header content.
How do I get rid of this initial flicker?