Skip to content

Commit 894e58e

Browse files
committed
fix(website): fix image paths in React preview for subpath deployments
Images like /images/sap-logo-svg.svg resolve against the domain root, which breaks when deployed under a subpath (e.g. /webcomponents/nightly/). Now prefixes /images/ paths with the Docusaurus baseUrl before transpilation.
1 parent 7d8eef4 commit 894e58e

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

packages/website/src/components/Editor/ReactPlayground.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
import React, { useContext, useState, useEffect, useCallback, useMemo, useRef, forwardRef } from "react";
66
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
7+
import useBaseUrl from "@docusaurus/useBaseUrl";
78
import { ThemeContext, ContentDensityContext, TextDirectionContext } from "@site/src/theme/Root";
89
import { useColorMode } from "@docusaurus/theme-common";
910
import styles from "./ReactPlayground.module.css";
@@ -457,6 +458,7 @@ export default function ReactPlayground({ code, editorVisible = false, onCodeCha
457458
const densityCtx = useContext(ContentDensityContext);
458459
const directionCtx = useContext(TextDirectionContext);
459460
const { colorMode } = useColorMode();
461+
const baseUrl = useBaseUrl("/");
460462

461463
const theme = themeCtx?.theme || "sap_horizon";
462464
const contentDensity = densityCtx?.contentDensity || "Cozy";
@@ -513,7 +515,13 @@ export default function ReactPlayground({ code, editorVisible = false, onCodeCha
513515
return <div style={{ padding: "1rem", color: "var(--sapNeutralTextColor)" }}>Loading...</div>;
514516
}
515517

516-
const transpileResult = transpileCode(editorCode);
518+
// Fix asset paths: replace "/images/" with baseUrl-prefixed paths
519+
// so images resolve correctly when deployed under a subpath (e.g. /webcomponents/nightly/)
520+
const codeWithFixedPaths = baseUrl === "/"
521+
? editorCode
522+
: editorCode.replace(/"\/images\//g, `"${baseUrl}images/`);
523+
524+
const transpileResult = transpileCode(codeWithFixedPaths);
517525
if (transpileResult.error) {
518526
// Show transpilation error inline, don't crash
519527
return (
@@ -538,7 +546,7 @@ export default function ReactPlayground({ code, editorVisible = false, onCodeCha
538546
}
539547

540548
return execResult.element;
541-
}, [editorCode, babelReady]);
549+
}, [editorCode, babelReady, baseUrl]);
542550

543551
const monacoTheme = colorMode === "dark" ? "vs-dark" : "light";
544552

0 commit comments

Comments
 (0)