@@ -23,6 +23,11 @@ module.exports = function() {
2323 const pathname = pathutils . normalizeMultiSlashes ( parsedUrl . pathname ) ;
2424 const search = parsedUrl . search || "" ;
2525
26+ const parsedOriginalUrl = url . parse ( req . originalUrl ) ;
27+ const originalPathname = pathutils . normalizeMultiSlashes (
28+ parsedOriginalUrl . pathname
29+ ) ;
30+
2631 const cleanUrlRules = ! ! _ . get ( req , "superstatic.cleanUrls" ) ;
2732
2833 // Exact file always wins.
@@ -32,8 +37,11 @@ module.exports = function() {
3237 if ( result ) {
3338 // If we are using cleanURLs, we'll trim off any `.html` (or `/index.html`), if it exists.
3439 if ( cleanUrlRules ) {
35- if ( _ . endsWith ( pathname , ".html" ) ) {
36- let redirPath = pathutils . removeTrailingString ( pathname , ".html" ) ;
40+ if ( _ . endsWith ( originalPathname , ".html" ) ) {
41+ let redirPath = pathutils . removeTrailingString (
42+ originalPathname ,
43+ ".html"
44+ ) ;
3745 if ( _ . endsWith ( redirPath , "/index" ) ) {
3846 redirPath = pathutils . removeTrailingString ( redirPath , "/index" ) ;
3947 }
@@ -50,7 +58,7 @@ module.exports = function() {
5058 }
5159
5260 // Now, let's consider the trailing slash.
53- const hasTrailingSlash = pathutils . hasTrailingSlash ( pathname ) ;
61+ const hasTrailingSlash = pathutils . hasTrailingSlash ( originalPathname ) ;
5462
5563 // We want to check for some other files, namely an `index.html` if this were a directory.
5664 const pathAsDirectoryWithIndex = pathutils . asDirectoryIndex (
@@ -67,7 +75,8 @@ module.exports = function() {
6775 ! cleanUrlRules
6876 ) {
6977 return res . superstatic . handle ( {
70- redirect : pathutils . addTrailingSlash ( pathname ) + search
78+ redirect :
79+ pathutils . addTrailingSlash ( originalPathname ) + search
7180 } ) ;
7281 }
7382 if (
@@ -78,13 +87,14 @@ module.exports = function() {
7887 // No infinite redirects
7988 return res . superstatic . handle ( {
8089 redirect : normalizeRedirectPath (
81- pathutils . removeTrailingSlash ( pathname ) + search
90+ pathutils . removeTrailingSlash ( originalPathname ) + search
8291 )
8392 } ) ;
8493 }
8594 if ( trailingSlashBehavior === true && ! hasTrailingSlash ) {
8695 return res . superstatic . handle ( {
87- redirect : pathutils . addTrailingSlash ( pathname ) + search
96+ redirect :
97+ pathutils . addTrailingSlash ( originalPathname ) + search
8898 } ) ;
8999 }
90100 // 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() {
98108 // We want to know if a specific mutation of the path exists.
99109 if ( cleanUrlRules ) {
100110 let appendedPath = pathname ;
111+ let appendedOriginalPath = originalPathname ;
101112 if ( hasTrailingSlash ) {
102113 if ( trailingSlashBehavior !== undefined ) {
103114 // We want to remove the trailing slash and see if a file exists with an .html attached.
104115 appendedPath =
105- pathutils . removeTrailingString ( pathname , "/" ) + ".html" ;
116+ pathutils . removeTrailingString ( appendedPath , "/" ) + ".html" ;
117+ appendedOriginalPath =
118+ pathutils . removeTrailingString ( appendedOriginalPath , "/" ) +
119+ ".html" ;
106120 }
107121 } else {
108122 // Let's see if our path is a simple clean URL missing a .HTML5
109123 appendedPath += ".html" ;
124+ appendedOriginalPath += ".html" ;
110125 }
111126
112127 return res . superstatic
@@ -119,7 +134,8 @@ module.exports = function() {
119134 // (This works because we are in the cleanURL block.)
120135 return res . superstatic . handle ( {
121136 redirect : normalizeRedirectPath (
122- pathutils . removeTrailingSlash ( pathname ) + search
137+ pathutils . removeTrailingSlash ( originalPathname ) +
138+ search
123139 )
124140 } ) ;
125141 }
@@ -133,17 +149,26 @@ module.exports = function() {
133149 appendedPath ,
134150 "/index"
135151 ) ;
152+ appendedOriginalPath = pathutils . removeTrailingString (
153+ appendedOriginalPath ,
154+ ".html"
155+ ) ;
156+ appendedOriginalPath = pathutils . removeTrailingString (
157+ appendedOriginalPath ,
158+ "/index"
159+ ) ;
136160 return res . superstatic . handle ( {
137161 redirect :
138- pathutils . addTrailingSlash ( appendedPath ) + search
162+ pathutils . addTrailingSlash ( appendedOriginalPath ) +
163+ search
139164 } ) ;
140165 }
141166 // If we've gotten this far and still have `/index.html` on the end, we want to remove it from the URL.
142- if ( _ . endsWith ( appendedPath , "/index.html" ) ) {
167+ if ( _ . endsWith ( appendedOriginalPath , "/index.html" ) ) {
143168 return res . superstatic . handle ( {
144169 redirect : normalizeRedirectPath (
145170 pathutils . removeTrailingString (
146- appendedPath ,
171+ appendedOriginalPath ,
147172 "/index.html"
148173 ) + search
149174 )
0 commit comments