Skip to content

Recommended way to pass an MDX file from getServerSideProps as a prop to a component? #47621

Answered by icyJoseph
lancejpollard asked this question in Help
Discussion options

You must be logged in to vote

I do it like this:

export const getStaticProps: GetStaticProps<MDXPost> = async (context) => {
  try {
    const { params } = context;
    const { slug } = params || {};

    if (typeof slug !== "string") return { notFound: true };

    const client = new MeiliSearch({
      host: process.env.MEILISEARCH_URL,
      apiKey: process.env.MEILISEARCH_KEY,
    });

    const index = await client.getIndex<IcyJoseph.Post>(
      process.env.MEILISEARCH_INDEX
    );

    const { content, ...rest } = await index.getDocument(slug);

    const source = await serialize(content || "");

    return { props: { ...rest, source }, revalidate: 360 };
  } catch (e) {
    return { notFound: true, revalidate: 3…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@lancejpollard
Comment options

@icyJoseph
Comment options

Answer selected by lancejpollard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants