#MDX access variable for dynamic props or params from getStaticPaths

1 messages · Page 1 of 1 (latest)

craggy rover
#

I have a case where a .mdx file needs to access a prop/param that is generated as part of getStaticPaths. The documentation outlines how to access frontmatter and additional exported properties, but I don't see how to access the dynamic slug/path in markdown. Is it possible?

autumn wren
#

Hey, can you share your setup? Where are you calling getStaticPaths?

craggy rover
#

I'm calling getStaticPaths in pages/[slug].mdx file. I would like to use slug in my MDX as a variable

autumn wren
#

@runic storm, any idea? You can't access the Astro global, so is this unsupported?

runic storm
autumn wren
#

For SSR, can you get the param?

runic storm
#

I'd recommend refactoring this to a pages/[slug].astro file, and splitting out your MDX content to a separate MDX file that receives data as props. Here's a snippet:

---
import Hero from '../components/Hero.mdx'
export function getStaticPaths() {
  return [{ params: { slug: 'post-1' } }...]
}
const heading = "My site"
---

<Hero heading={heading} />

And in your MDX file (Hero.mdx in this case) can retrieved props using props. like so:

# {props.heading}

My content!
runic storm