#how can I get the entire location URL, including hostname and search params?
1 messages · Page 1 of 1 (latest)
Hi @ashen jay. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js, you can access different parts of the URL using various methods.
To get the pathname, you can use the usePathname hook. This hook does not take any parameters and returns the pathname of the current URL.
const pathname = usePathname()
usePathname Documentation (https://nextjs.org/docs/app/api-reference/functions/use-pathname#parameters)
To get the search parameters, you can use the useSearchParams hook.
const searchParams = useSearchParams()
useSearchParams Documentation (https://nextjs.org/docs/app/api-reference/functions/use-search-params)
To get the hostname, you can use the req.nextUrl object in a server-side context. This object extends the native URL API and includes Next.js specific properties.
const hostname = req.nextUrl.hostname
NextRequest Documentation (https://nextjs.org/docs/pages/api-reference/functions/next-request#nexturl)
To construct the full URL, you can concatenate these parts together:
const fullUrl = `${hostname}${pathname}${searchParams}`