New to NextJS.
I want to check if the current URL is the home page.
If I use
import { useRouter } from "next/router";
const router = useRouter();
const is_home = (router.pathname === '');
I get a front end error
You have a Server Component that imports next/router. Use next/navigation instead.
If I change
import { useRouter } from "next/router";
to
import { useRouter } from "next/navigation";
I get a terminal error
Error: useRouter only works in Client Components. Add the "use client" directive at the top of the file to use it.
If I add "use client"
to the top of the file, the terminal error goes away, but pathname
in router.pathname
is highlighted red in VS Code, and the hover error is
Property 'pathname' does not exist on type 'AppRouterInstance'.ts(2339)..
I can't find any documentation for next/navigation
like there is for next/router
.
Is there a simpler way to check for the home page?
I want to assign a default meta description for the home page, and assign the post excerpt (initally) as the meta description if the current URL is not the home page.