-
SummaryI have a Hugo site that I am converting to Next.js, so I have a bunch of Markdown content files for all my blog posts. Eventually, I plan to migrate those posts to a CMS like Contentful, but for the immediate future, the plan is to put all my Markdown content in I want my Blog page to load the right Markdown content dynamically, using information from the slug, so my page is: I followed the instructions in the MDX Guide, specifically for doing dynamic imports, and I got it working. Here is my import React from "react";
import dynamic from "next/dynamic";
export default async function Blog({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
// either of these import methods work
const Markdown1 = dynamic(() => import(`@/content/blog/${slug}.mdx`))
const {default:Markdown2} = await import(`@/content/blog/${slug}.mdx`)
return (
<article>
<Markdown1 />
<Markdown2 />
</article>
)
}
export function generateStaticParams() {
return [{ slug: 'first-post' }]
} Since I want to eventually move my content to a CMS, I'd like to encapsulate the content-loading logic here into a library function, so that when the time comes to migrate to Contentful, I don't need to update several dozen So I created a utility helper: // ./src/lib/markdown.ts
import React from "react";
import dynamic from "next/dynamic";
export async function getMarkdown(slug: string) {
const {default: Markdown} = await import(`@/content/${slug}.mdx`)
return Markdown as React.ComponentType
}
export function getMarkdownDynamic(slug: string) {
return dynamic(() => import(`@/content/${slug}.mdx`))
} I updated my // const Markdown1 = dynamic(() => import(`@/content/blog/${slug}.mdx`))
// const {default:Markdown2} = await import(`@/content/blog/${slug}.mdx`)
const Markdown1 = await getMarkdown(`blog/${slug}`)
const Markdown2 = getMarkdownDynamic(`blog/${slug}`) The page no longer works. Neither This is the error I get when trying to dynamically load MDX outside the
Is what I'm trying to do just not possible? My next step was going to be ditching the Any suggestions? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Does it work if you add your library to https://nextjs.org/docs/app/api-reference/config/next-config-js/transpilePackages? |
Beta Was this translation helpful? Give feedback.
-
ChatGPT suggested the following:
I moved my utility helper to ChatGPT also suggested replacing the aliased import path with a relative path, so I gave that a whirl: // ./src/lib/markdown.ts
import React from "react";
import dynamic from "next/dynamic";
export async function getMarkdown(slug: string) {
const {default: Markdown} = await import(`../../content/${slug}.mdx`)
return Markdown as React.ComponentType
}
export function getMarkdownDynamic(slug: string) {
return dynamic(() => import(`../../content/${slug}.mdx`))
} That does seem to work, so the issue seems to revolve around the use of |
Beta Was this translation helpful? Give feedback.
ChatGPT suggested the following:
I moved my utility helper to
./src/app/lib/markdown.ts
, and that still didn't work, so I moved it back to./src/lib/markdown.ts
.ChatGPT also suggested replacing the aliased import path with a relative path, so I gave that a whirl: