Skip to content

How to dynamically prerender blog post routes from MDX files? #13849

Closed Answered by nikolailehbrink
nikolailehbrink asked this question in Q&A
Discussion options

You must be logged in to vote

I just realized that you can access the static paths via getStaticPaths inside the prerender function. This allows me to dynamically filter out draft posts and prerender only the published ones:

// react-router.config.ts
import type { Config } from "@react-router/dev/config";
import { href } from "react-router";
import { isDraft } from "./app/lib/utils";

export default {
  prerender({ getStaticPaths }) {
    const paths = getStaticPaths();
    const publishedPosts = paths.filter(
      (path) => path.startsWith("/blog/") && !isDraft(path),
    );
    return [href("/privacy-policy"), href("/legal-notice"), ...publishedPosts];
  },
} satisfies Config;

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nikolailehbrink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant