Skip to content

Commit 3f1dd18

Browse files
authored
Elaborate on renderToString without hydration (fixes #61)
1 parent 258c37e commit 3f1dd18

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,30 @@ Hopefully this makes sense, since in order to work, imports must be relative to
171171

172172
This library evaluates a string of JavaScript on the client side, which is how it hydrates the MDX content. Evaluating a string into javascript can be a dangerous practice if not done carefully, as it can enable XSS attacks. It's important to make sure that you are only passing the `mdxSource` input generated by the `render-to-string` function to `hydrate`, as instructed in the documentation. **Do not pass user input into `hydrate`.**
173173

174-
If you have a CSP on your website that disallows code evaluation via `eval` or `new Function()`, you will need to loosen that restriction in order to utilize the `hydrate` function, which can be done using [`unsafe-eval`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#common_sources). It's also worth noting that you do not _have_ to use `hydrate` on the client side, but without it, you will get a server-rendered result, meaning no ability to react to user input, etc.
174+
If you have a CSP on your website that disallows code evaluation via `eval` or `new Function()`, you will need to loosen that restriction in order to utilize the `hydrate` function, which can be done using [`unsafe-eval`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#common_sources).
175+
176+
### Usage Without Hydration
177+
178+
It's also worth noting that you do not _have_ to use `hydrate` on the client side — but without it, you will get a server-rendered result, meaning no ability to react to user input, etc. To do this, pass the `renderedOutput` prop of the object returned by `renderToString` to [`dangerouslySetInnerHTML`](https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml):
179+
180+
```jsx
181+
import renderToString from 'next-mdx-remote/render-to-string'
182+
183+
import Test from '../components/test'
184+
185+
const components = { Test }
186+
187+
export default function TestPage({ renderedOutput }) {
188+
return <div className="wrapper" dangerouslySetInnerHTML={{ __html: renderedOutput }} />
189+
}
190+
191+
export async function getStaticProps() {
192+
// <Test /> will be rendered to static markup, but will be non-interactive!
193+
const source = 'Some **mdx** text, with a component <Test />'
194+
const { renderedOutput } = await renderToString(source, { components })
195+
return { props: { renderedOutput } }
196+
}
197+
```
175198

176199
## License
177200

0 commit comments

Comments
 (0)