Given the following pages folder structure
- pages
|- [slug].js
and [slug].js has the following code:
const Page = ({ slug }) => (
<div>{slug}</div>
);
Page.getInitialProps = ({ slug )} => {
console.log(slug); // this gets called twice
return { slug }
}
Whenever we go to a page such as /hello-world, getInitialProps gets called twice. On the first call, slug is favicon.ico, and the second call slug is hello-world.
Steps to reproduce the behavior, please provide code snippets or a repository:
/pages/[slug].js with the code as explained above/hello-worldfavicon.ico and hello-worldgetInitialProps should only be called once with hello-world as the slug value.
If we add a <link /> component to load a favicon inside pages/_document.js, this issue goes away.
getInitialProps should only be called once with hello-world as the slug value.
This is already the case. What you're seeing is default browser behavior of trying to load favicon.ico when you open a page. This then causes the page to be rendered. You can add an empty file to public/favicon.ico to provide one for example.
Most helpful comment
This is already the case. What you're seeing is default browser behavior of trying to load favicon.ico when you open a page. This then causes the page to be rendered. You can add an empty file to
public/favicon.icoto provide one for example.