#Router object (pathname, query) to string

9 messages · Page 1 of 1 (latest)

coarse lily
#

Hey all! Does anyone know how I can convert a router object like this, into the string path?

const obj = {
  pathname: '/blog/[slug]',
  query: { slug: 'hello' },
}
const path = asPathString(obj)
// "blog/hello"
console.log(path)
woven pawn
coarse lily
#

This is for server side

woven pawn
#

I was going to recommend useRouter but that's for client side so the best way is to use getStaticPaths

coarse lily
#

How do you do this via useRouter?

woven pawn
#

const route = useRouter()

It will give you an object with all of the values from the URL. I'm sure slug would be there...

coarse lily
#

I don't think you understood my question. Basically I'm looking for some logic for the asPathString function that I put above

dapper umbra
#

I dont think there is an util function for that, you would need to do it yourself

pallid cloak