Skip to content

Commit e0af6e5

Browse files
authored
fix: improve redirect logic for URLs ending with '/' in getFilenameFromUrl
1 parent 56bbb8b commit e0af6e5

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

src/utils/getFilenameFromUrl.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,7 @@ function decode(input) {
1717
return querystring.unescape(input);
1818
}
1919

20-
const memoizedParseWithIndex = memorize(parse, undefined, (value) => {
21-
if (value.pathname) {
22-
value.pathname = decode(value.pathname);
23-
if (value.pathname[value.pathname.length - 1] === "/") {
24-
value.pathname = path.join(value.pathname, "index.html");
25-
}
26-
}
27-
28-
return value;
29-
});
30-
31-
const memoizedParseWithoutIndex = memorize(parse, undefined, (value) => {
20+
const memoizedParse = memorize(parse, undefined, (value) => {
3221
if (value.pathname) {
3322
value.pathname = decode(value.pathname);
3423
}
@@ -54,7 +43,6 @@ const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
5443
*/
5544

5645
// TODO refactor me in the next major release, this function should return `{ filename, stats, error }`
57-
// TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/121dda811bae6420ce08171b10685d19c4e53059/index.js#L426
5846
/**
5947
* @template {IncomingMessage} Request
6048
* @template {ServerResponse} Response
@@ -74,19 +62,20 @@ function getFilenameFromUrl(context, url, extra = {}) {
7462

7563
try {
7664
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
77-
urlObject = memoizedParseWithIndex(url, false, true);
65+
urlObject = memoizedParse(url, false, true);
7866
} catch {
7967
return;
8068
}
8169

8270
for (const { publicPath, outputPath, assetsInfo } of paths) {
8371
/** @type {string | undefined} */
8472
let filename;
73+
8574
/** @type {import("node:url").Url} */
8675
let publicPathObject;
8776

8877
try {
89-
publicPathObject = memoizedParseWithoutIndex(
78+
publicPathObject = memoizedParse(
9079
publicPath !== "auto" && publicPath ? publicPath : "/",
9180
false,
9281
true,
@@ -127,11 +116,20 @@ function getFilenameFromUrl(context, url, extra = {}) {
127116
);
128117

129118
try {
119+
if (filename[filename.length - 1] === "/") {
120+
if (options.index === false) {
121+
return;
122+
} else if (options.index === "string") {
123+
filename = path.join(filename, options.index);
124+
} else {
125+
filename = path.join(filename, "index.html");
126+
}
127+
}
130128
extra.stats = context.outputFileSystem.statSync(filename);
131129
} catch {
132130
continue;
133131
}
134-
// console.log({stats: extra.stats});
132+
135133
if (extra.stats.isFile()) {
136134
foundFilename = filename;
137135

0 commit comments

Comments
 (0)