Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Make remarkCodeHike work at Next.js edge runtime #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
micooz wants to merge 3 commits into code-hike:next
base: next
Choose a base branch
Loading
from micooz:next

Conversation

Copy link

@micooz micooz commented Dec 9, 2024
edited
Loading

The issue

I am trying to integrate Code Hike into a Next.js project using the Edge Runtime. I referred to this project:

https://github.com/code-hike/examples/tree/next-mdx-remote/with-next-mdx-remote-rsc

and added this line of code to page.tsx:

export const runtime = "edge"

I noticed that page.tsx import fs from "fs", which is not available in edge. So I just hard code source to test if Code Hike works.

- import { promises as fs } from "fs"
+ export const runtime = "edge"
export default async function Home() {
- const source = await fs.readFile("./content/index.md", "utf-8")
+ const source = "# title";
 const { content } = await compileMDX({
 source,
 components,
 options: {

Unfortunately, it threw an compile error caused by import "fs" module inside of codehike/dist/mdx.js.

image

Then I found the problematic code:

let fs, path
try {
fs = (await import("fs")).default
path = (await import("path")).default
if (!fs || !fs.readFileSync || !path || !path.resolve) {
throw new Error("fs or path not found")
}
} catch (e: any) {

When using Webpack, dynamic imports with import() can prevent exceptions from being caught by try-catch.

Even if I didn’t use Code Hike’s !from feature to import external code.

The solution

Changing import to require resolves this issue. Because require imports modules at runtime, while import does so at compile time.

Copy link

vercel bot commented Dec 9, 2024
edited
Loading

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
codehike.org ❌ Failed (Inspect) Dec 9, 2024 7:00am

Copy link

vercel bot commented Dec 23, 2024

@pomber is attempting to deploy a commit to the codehike Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added the changeset PR including changesets label Dec 23, 2024
Copy link
Contributor

Try codehike from this pull request in your project with:

npm i https://pkg.pr.new/codehike@489

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
changeset PR including changesets
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /