From 57c4de92342307e77b1888297af70521192e43cd Mon Sep 17 00:00:00 2001 From: Hugo Josefson Date: Fri, 16 Oct 2020 11:45:57 +0000 Subject: [PATCH] Fix redirect when mounted at basePath. Related to: https://github.com/firebase/superstatic/issues/153 --- lib/middleware/files.js | 47 +++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/middleware/files.js b/lib/middleware/files.js index 9abf6cde..182e6f6e 100644 --- a/lib/middleware/files.js +++ b/lib/middleware/files.js @@ -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. @@ -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"); } @@ -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( @@ -67,7 +75,8 @@ module.exports = function() { !cleanUrlRules ) { return res.superstatic.handle({ - redirect: pathutils.addTrailingSlash(pathname) + search + redirect: + pathutils.addTrailingSlash(originalPathname) + search }); } if ( @@ -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. @@ -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 @@ -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 ) }); } @@ -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 )