In building out this beautiful website, and the equally beautiful tamagui.dev, the surprising most difficult part was getting MDX to work well.
Since many blogs, apps, and documentation sites want to display Markdown content, we figured a guide showing how we did it will be useful.
We think long-term someone should write a much better mdx package, as ours is simply pieced together from a bunch of packages and code we ported long ago to Next.js, but nonetheless it works. It uses a hodge-podge of Rehype plugins to do the trick, alongside mdx-bundler.
The first step is to create a directory for your mdx files to live:
Throw some stuff in that file:
Terminal
@vxrn/mdx
Next, we add our mdx dependencies:
npm install @vxrn/mdx mdx-bundler
We set up our MDX components (using Tamagui in this example):
features/MDXComponents.tsx
And then all we need is a new route:
app/docs/[slug].tsx
Note that we await import
the @vxrn/mdx
library - this is because One by default builds out your server
and API routes to CommonJS, but some of the rehype/remak dependencies are ESM. Long story short - this makes it work.
To ensure that MDX works correctly with your One project, you'll need to configure Vite. Update your vite.config.ts
file with the following content:
vite.config.ts
This configuration does a few important things:
noExternal: true
in the SSR options, which tells Vite to bundle all dependencies for server-side rendering.@vxrn/mdx
as an external dependency, which is necessary because it contains ESM modules that shouldn't be bundled.That should do it. We say should, because there's a lot going on here, and many sub-deps inside @vxrn/mdx
that need to work together. Let us know if this works for you!
Edit this page on GitHub.