pathpida
pathpida copied to clipboard
Get url string of pagesPath
Description
Hi, I many times we need get final url of pagesPath Example:
const urlObject= pagesPath.dashboard.project._projectId("abc").$url();
// return { pathname: '/dashboard/project/[projectId]' as const, query: { projectId }, hash: url?.hash }
// But many times we will need the final string, like use in window.open
window.open("Need an url string here, not object")
Describe the solution you'd like
This will be good, i think
pagesPath.dashboard.project._projectId("abc").$urlString;
Additional context
Next.js new AppRouterInstance
's push , replace and prefetch methods accept href:string
. So, this feature is needed.
https://github.com/vercel/next.js/blob/b8ae447b0fdc12553b4eacd26595e4054ffcf415/packages/next/shared/lib/app-router-context.ts#L68-L95
@solufa any updates on this?
A bit old but a workaround is to use nextjs resolveHref function you will get the full href path string https://github.com/vercel/next.js/blob/b8ae447b0fdc12553b4eacd26595e4054ffcf415/packages/next/shared/lib/router/router.ts#L223
import { resolveHref } from 'next/dist/shared/lib/router/router'
const paths = resolveHref(route, $pagesPath.post._pid(1).$url(), true)[1] // you need to pass router of useRouter