pathpida icon indicating copy to clipboard operation
pathpida copied to clipboard

Get url string of pagesPath

Open IRediTOTO opened this issue 2 years ago • 3 comments

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

image

IRediTOTO avatar Aug 31 '22 21:08 IRediTOTO

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

matamatanot avatar Dec 19 '22 06:12 matamatanot

@solufa any updates on this?

qwexvf avatar Mar 17 '23 06:03 qwexvf

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

qwexvf avatar Mar 18 '23 06:03 qwexvf