Skip to content

Commit cd0b369

Browse files
committed
feat: enhance getRelativePath function to handle default path and localhost scenarios
1 parent 786ed1b commit cd0b369

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,23 @@
4141
}
4242

4343
function getRelativePath(path) {
44+
// If no path provided, use window.location.pathname
4445
if (!path && isBrowser) {
4546
path = window.location.pathname.replace(/\/[^\/]*$/, ""); // Remove file from path
4647
}
48+
49+
// For localhost/127.0.0.1, trim everything after '/src'
50+
if (
51+
isBrowser &&
52+
(location.hostname === "localhost" ||
53+
location.hostname === "127.0.0.1")
54+
) {
55+
const srcIndex = path.indexOf("/src");
56+
if (srcIndex !== -1) {
57+
path = path.substring(0, srcIndex + 4); // keep '/src'
58+
}
59+
}
60+
4761
if (!path.endsWith("/")) {
4862
path += "/";
4963
}
@@ -1281,4 +1295,4 @@
12811295
setAttributeNames,
12821296
getAttributeNames
12831297
};
1284-
});
1298+
});

0 commit comments

Comments
 (0)