Next.js Discord

Discord Forum

How to pass custom data for use in MDX using next-mdx-remote/rsc in Nextjs 13.4

Unanswered
Baldfaced hornet posted this in #help-forum
Open in Discord
Baldfaced hornetOP
In the non RSC version of next-mdx-remote, I could pass custom data for use inside .mdx by passing the data to a scope prop like so:

<MDXRemote {...source} components={components} scope={data} />

But with next-mdx-remote/rsc the scope prop doesn't seem to be working:

const source = fs.readFileSync(mdxPath, 'utf8')
const {content, data} = matter(source)
const customData = { product: 'next' }


return (
<MDXRemote source={content} components={...components} scope={customData} />
)


When I try to access {product} inside mdx it says product is not defined

What's the correct way to send custom data through next-mdx-remote/rsc? Also is there a way to combine scope into the source like how you would do by passing scope to the serialize method in the past?







Tried passing scope into <MDXRemote source={source} components={...components} scope={customData} />

Tried merging custom data to the source

13 Replies

Golden-winged Warbler
have you tried accessing it via {scope.product}?

(I haven't tried, but extra props might be accessible by their name, i.e. why would scope be merged at top name level rather than inserted)
Baldfaced hornetOP
Yep! I did try {scope.product} and it says Error: scope is not defined
Golden-winged Warbler
hmm, try {props.scope.product}?

https://mdxjs.com/docs/using-mdx/#props
Baldfaced hornetOP
Still doesn't work.. now says Error: Cannot read properties of undefined (reading 'product')
Is there a way to find out if the RSC MDXRemote even accepts scope as a valid prop?
the section about Next.js app support (at the very end) is so sparse: https://github.com/hashicorp/next-mdx-remote
Golden-winged Warbler
hmm, maybe don't put it under "scope"

I think the scope prop has special meaning to the remote-mdx
iirc, it passes the "scope" prop to the child components
i.e. in your example, scope -> custom (or whatever name you'd like), and see if that works
Baldfaced hornetOP
hmm no not working unforunately. What would a barebones solution look like, if I want to pass const data = {product: 'next'} ?
We used to be able to pass scope into serialize and have the data available that way, but the new MDXRemote expects the raw unserialized source now
Also thanks for the help!
Golden-winged Warbler
yw, I just tested that the props works with regular MDX with something like { props.foo }, so seems this is specific to mdx-remote