I want to be able to customize the HTML based on the current locale. I have created a _document.js
inside pages
directory. And I used this code:
import { Html, Head, Main, NextScript } from 'next/document'
import { useRouter } from 'next/router'
export default function Document() {
const { locale } = useRouter();
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
But I get this error:
Error: Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
When I go to the URL specified, I see them saying that I'm using logic outside <Main />
. Thus, how can I access locale
inside _document.js
?
I found NextRouter was not mounted Next.JS but even using next/navigation
did not work for me, and I get this error:
Error: invariant expected app router to be mounted