Skip to content

Commit 34a94d5

Browse files
committed
Fix redirect when mounted at basePath.
Related to: #153
1 parent cbb8799 commit 34a94d5

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/middleware/files.js

+36-11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ module.exports = function () {
4242
const pathname = pathutils.normalizeMultiSlashes(parsedUrl.pathname);
4343
const search = parsedUrl.search || "";
4444

45+
const parsedOriginalUrl = url.parse(req.originalUrl);
46+
const originalPathname = pathutils.normalizeMultiSlashes(
47+
parsedOriginalUrl.pathname
48+
);
49+
4550
const cleanUrlRules = !!_.get(req, "superstatic.cleanUrls");
4651

4752
// Exact file always wins.
@@ -50,8 +55,11 @@ module.exports = function () {
5055
if (result) {
5156
// If we are using cleanURLs, we'll trim off any `.html` (or `/index.html`), if it exists.
5257
if (cleanUrlRules) {
53-
if (_.endsWith(pathname, ".html")) {
54-
let redirPath = pathutils.removeTrailingString(pathname, ".html");
58+
if (_.endsWith(originalPathname, ".html")) {
59+
let redirPath = pathutils.removeTrailingString(
60+
originalPathname,
61+
".html"
62+
);
5563
if (_.endsWith(redirPath, "/index")) {
5664
redirPath = pathutils.removeTrailingString(redirPath, "/index");
5765
}
@@ -67,7 +75,7 @@ module.exports = function () {
6775
}
6876

6977
// Now, let's consider the trailing slash.
70-
const hasTrailingSlash = pathutils.hasTrailingSlash(pathname);
78+
const hasTrailingSlash = pathutils.hasTrailingSlash(originalPathname);
7179

7280
// We want to check for some other files, namely an `index.html` if this were a directory.
7381
const pathAsDirectoryWithIndex = pathutils.asDirectoryIndex(
@@ -83,7 +91,8 @@ module.exports = function () {
8391
!cleanUrlRules
8492
) {
8593
return res.superstatic.handle({
86-
redirect: pathutils.addTrailingSlash(pathname) + search,
94+
redirect:
95+
pathutils.addTrailingSlash(originalPathname) + search,
8796
});
8897
}
8998
if (
@@ -94,13 +103,14 @@ module.exports = function () {
94103
// No infinite redirects
95104
return res.superstatic.handle({
96105
redirect: normalizeRedirectPath(
97-
pathutils.removeTrailingSlash(pathname) + search
106+
pathutils.removeTrailingSlash(originalPathname) + search
98107
),
99108
});
100109
}
101110
if (trailingSlashBehavior === true && !hasTrailingSlash) {
102111
return res.superstatic.handle({
103-
redirect: pathutils.addTrailingSlash(pathname) + search,
112+
redirect:
113+
pathutils.addTrailingSlash(originalPathname) + search,
104114
});
105115
}
106116
// If we haven't returned yet, our path is "correct" and we should be serving a file, not redirecting.
@@ -114,15 +124,20 @@ module.exports = function () {
114124
// We want to know if a specific mutation of the path exists.
115125
if (cleanUrlRules) {
116126
let appendedPath = pathname;
127+
let appendedOriginalPath = originalPathname;
117128
if (hasTrailingSlash) {
118129
if (trailingSlashBehavior !== undefined) {
119130
// We want to remove the trailing slash and see if a file exists with an .html attached.
120131
appendedPath =
121-
pathutils.removeTrailingString(pathname, "/") + ".html";
132+
pathutils.removeTrailingString(appendedPath, "/") + ".html";
133+
appendedOriginalPath =
134+
pathutils.removeTrailingString(appendedOriginalPath, "/") +
135+
".html";
122136
}
123137
} else {
124138
// Let's see if our path is a simple clean URL missing a .HTML5
125139
appendedPath += ".html";
140+
appendedOriginalPath += ".html";
126141
}
127142

128143
return providerResult(req, res, appendedPath).then(
@@ -134,7 +149,8 @@ module.exports = function () {
134149
// (This works because we are in the cleanURL block.)
135150
return res.superstatic.handle({
136151
redirect: normalizeRedirectPath(
137-
pathutils.removeTrailingSlash(pathname) + search
152+
pathutils.removeTrailingSlash(originalPathname) +
153+
search
138154
),
139155
});
140156
}
@@ -148,17 +164,26 @@ module.exports = function () {
148164
appendedPath,
149165
"/index"
150166
);
167+
appendedOriginalPath = pathutils.removeTrailingString(
168+
appendedOriginalPath,
169+
".html"
170+
);
171+
appendedOriginalPath = pathutils.removeTrailingString(
172+
appendedOriginalPath,
173+
"/index"
174+
);
151175
return res.superstatic.handle({
152176
redirect:
153-
pathutils.addTrailingSlash(appendedPath) + search,
177+
pathutils.addTrailingSlash(appendedOriginalPath) +
178+
search,
154179
});
155180
}
156181
// If we've gotten this far and still have `/index.html` on the end, we want to remove it from the URL.
157-
if (_.endsWith(appendedPath, "/index.html")) {
182+
if (_.endsWith(appendedOriginalPath, "/index.html")) {
158183
return res.superstatic.handle({
159184
redirect: normalizeRedirectPath(
160185
pathutils.removeTrailingString(
161-
appendedPath,
186+
appendedOriginalPath,
162187
"/index.html"
163188
) + search
164189
),

0 commit comments

Comments
 (0)