Skip to content

Commit 5e39a3b

Browse files
committed
Prepend base path for local public images in <img> component to ensure correct URL handling
1 parent a712bf1 commit 5e39a3b

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

mdx-components.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,16 @@ const mdxComponents = {
222222
: parseInt(props.height) || defaultHeight
223223
: defaultHeight;
224224

225-
let url = props.src;
226-
227-
// Add base path for local public images if needed
228-
// If url starts with / and not //, prepend base path if in production mode
229-
// But we don't have access to runtime config easily here.
230-
// Ideally, use Next.js Image which handles basePath automatically.
231-
225+
// Prepend NEXT_PUBLIC_BASE_PATH for local public images (src starts with /).
226+
// Next.js does NOT automatically prepend basePath to plain <img> or ImageZoom src strings —
227+
// only its own <Image> component gets that treatment. Since fetch-docs writes
228+
// <img src="/filaletter/..." /> with absolute paths, we must add the prefix here.
229+
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";
230+
let url: string =
231+
typeof src === "string" && src.startsWith("/") && !src.startsWith("//")
232+
? basePath + src
233+
: (src as string);
234+
232235
return (
233236
<ImageZoom
234237
src={url}

0 commit comments

Comments
 (0)