Skip to content

Commit

Permalink
Fix redirect when mounted at basePath.
Browse files Browse the repository at this point in the history
Related to: #153
  • Loading branch information
hugojosefson committed Oct 16, 2020
1 parent 9c8fc49 commit 57c4de9
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions lib/middleware/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ module.exports = function() {
const pathname = pathutils.normalizeMultiSlashes(parsedUrl.pathname);
const search = parsedUrl.search || "";

const parsedOriginalUrl = url.parse(req.originalUrl);
const originalPathname = pathutils.normalizeMultiSlashes(
parsedOriginalUrl.pathname
);

const cleanUrlRules = !!_.get(req, "superstatic.cleanUrls");

// Exact file always wins.
Expand All @@ -32,8 +37,11 @@ module.exports = function() {
if (result) {
// If we are using cleanURLs, we'll trim off any `.html` (or `/index.html`), if it exists.
if (cleanUrlRules) {
if (_.endsWith(pathname, ".html")) {
let redirPath = pathutils.removeTrailingString(pathname, ".html");
if (_.endsWith(originalPathname, ".html")) {
let redirPath = pathutils.removeTrailingString(
originalPathname,
".html"
);
if (_.endsWith(redirPath, "/index")) {
redirPath = pathutils.removeTrailingString(redirPath, "/index");
}
Expand All @@ -50,7 +58,7 @@ module.exports = function() {
}

// Now, let's consider the trailing slash.
const hasTrailingSlash = pathutils.hasTrailingSlash(pathname);
const hasTrailingSlash = pathutils.hasTrailingSlash(originalPathname);

// We want to check for some other files, namely an `index.html` if this were a directory.
const pathAsDirectoryWithIndex = pathutils.asDirectoryIndex(
Expand All @@ -67,7 +75,8 @@ module.exports = function() {
!cleanUrlRules
) {
return res.superstatic.handle({
redirect: pathutils.addTrailingSlash(pathname) + search
redirect:
pathutils.addTrailingSlash(originalPathname) + search
});
}
if (
Expand All @@ -78,13 +87,14 @@ module.exports = function() {
// No infinite redirects
return res.superstatic.handle({
redirect: normalizeRedirectPath(
pathutils.removeTrailingSlash(pathname) + search
pathutils.removeTrailingSlash(originalPathname) + search
)
});
}
if (trailingSlashBehavior === true && !hasTrailingSlash) {
return res.superstatic.handle({
redirect: pathutils.addTrailingSlash(pathname) + search
redirect:
pathutils.addTrailingSlash(originalPathname) + search
});
}
// If we haven't returned yet, our path is "correct" and we should be serving a file, not redirecting.
Expand All @@ -98,15 +108,20 @@ module.exports = function() {
// We want to know if a specific mutation of the path exists.
if (cleanUrlRules) {
let appendedPath = pathname;
let appendedOriginalPath = originalPathname;
if (hasTrailingSlash) {
if (trailingSlashBehavior !== undefined) {
// We want to remove the trailing slash and see if a file exists with an .html attached.
appendedPath =
pathutils.removeTrailingString(pathname, "/") + ".html";
pathutils.removeTrailingString(appendedPath, "/") + ".html";
appendedOriginalPath =
pathutils.removeTrailingString(appendedOriginalPath, "/") +
".html";
}
} else {
// Let's see if our path is a simple clean URL missing a .HTML5
appendedPath += ".html";
appendedOriginalPath += ".html";
}

return res.superstatic
Expand All @@ -119,7 +134,8 @@ module.exports = function() {
// (This works because we are in the cleanURL block.)
return res.superstatic.handle({
redirect: normalizeRedirectPath(
pathutils.removeTrailingSlash(pathname) + search
pathutils.removeTrailingSlash(originalPathname) +
search
)
});
}
Expand All @@ -133,17 +149,26 @@ module.exports = function() {
appendedPath,
"/index"
);
appendedOriginalPath = pathutils.removeTrailingString(
appendedOriginalPath,
".html"
);
appendedOriginalPath = pathutils.removeTrailingString(
appendedOriginalPath,
"/index"
);
return res.superstatic.handle({
redirect:
pathutils.addTrailingSlash(appendedPath) + search
pathutils.addTrailingSlash(appendedOriginalPath) +
search
});
}
// If we've gotten this far and still have `/index.html` on the end, we want to remove it from the URL.
if (_.endsWith(appendedPath, "/index.html")) {
if (_.endsWith(appendedOriginalPath, "/index.html")) {
return res.superstatic.handle({
redirect: normalizeRedirectPath(
pathutils.removeTrailingString(
appendedPath,
appendedOriginalPath,
"/index.html"
) + search
)
Expand Down

0 comments on commit 57c4de9

Please sign in to comment.