Skip to content

Commit ee8725d

Browse files
authored
refactor: logic
1 parent 8f5f508 commit ee8725d

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

src/utils/getFilenameFromUrl.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,29 @@ function getFilenameFromUrl(context, url) {
109109
throw new FilenameError("Forbidden", 403);
110110
}
111111

112-
// Strip the `pathname` property from the `publicPath` option from the start of requested url
113-
// `/complex/foo.js` => `foo.js`
114-
// and add outputPath
115-
// `foo.js` => `/home/user/my-project/dist/foo.js`
112+
let index;
113+
114+
if (pathname && pathname.endsWith("/")) {
115+
if (options.index === false) {
116+
return;
117+
}
118+
index =
119+
typeof options.index === "string" ? options.index : "index.html";
120+
}
121+
122+
// Builds the absolute path of the file to serve:
123+
// - If the URL ends with '/', appends the index file (index.html or custom) to the directory path.
124+
// - If the URL does not end with '/', only joins the relative path to outputPath.
125+
// Example:
126+
// URL: /complex/foo.js => outputPath/complex/foo.js
127+
// URL: /complex/ => outputPath/complex/index.html (or the configured index file)
116128
filename = path.join(
117129
outputPath,
118130
pathname.slice(publicPathPathname.length),
131+
index || "",
119132
);
120133

121134
try {
122-
if (pathname[pathname.length - 1] === "/") {
123-
if (options.index === false) {
124-
return;
125-
} else if (typeof options.index === "string") {
126-
filename = path.join(filename, options.index);
127-
} else {
128-
filename = path.join(filename, "index.html");
129-
}
130-
}
131-
132135
extra.stats = context.outputFileSystem.statSync(filename);
133136
} catch {
134137
continue;

0 commit comments

Comments
 (0)