Just getting started with NextJS.
I am trying to determine if the current URL is the home page.
When I use:
import { useRouter } from "next/router";
const router = useRouter();
const is_home = (router.pathname === '');
An error pops up on the frontend stating:
You have a Server Component that imports next/router. Use next/navigation instead.
If I switch from using:
import { useRouter } from "next/router";
To:
import { useRouter } from "next/navigation";
A terminal error occurs:
Error: useRouter only works in Client Components. Add the "use client" directive at the top of the file to use it.
Adding "use client"
removes the terminal error, but then pathname
in router.pathname
displays as red in VS Code, showing the hover error as:
Property 'pathname' does not exist on type 'AppRouterInstance'.ts(2339)
I couldn't find any documentation for next/navigation
like there is for next/router
.
Is there a simpler method to check for the home page?
My goal is to set a default meta description for the home page and use the post excerpt as the meta description if the current URL is not the home page initially.