@@ -42,6 +42,11 @@ module.exports = function () {
42
42
const pathname = pathutils . normalizeMultiSlashes ( parsedUrl . pathname ) ;
43
43
const search = parsedUrl . search || "" ;
44
44
45
+ const parsedOriginalUrl = url . parse ( req . originalUrl ) ;
46
+ const originalPathname = pathutils . normalizeMultiSlashes (
47
+ parsedOriginalUrl . pathname
48
+ ) ;
49
+
45
50
const cleanUrlRules = ! ! _ . get ( req , "superstatic.cleanUrls" ) ;
46
51
47
52
// Exact file always wins.
@@ -50,8 +55,11 @@ module.exports = function () {
50
55
if ( result ) {
51
56
// If we are using cleanURLs, we'll trim off any `.html` (or `/index.html`), if it exists.
52
57
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
+ ) ;
55
63
if ( _ . endsWith ( redirPath , "/index" ) ) {
56
64
redirPath = pathutils . removeTrailingString ( redirPath , "/index" ) ;
57
65
}
@@ -67,7 +75,7 @@ module.exports = function () {
67
75
}
68
76
69
77
// Now, let's consider the trailing slash.
70
- const hasTrailingSlash = pathutils . hasTrailingSlash ( pathname ) ;
78
+ const hasTrailingSlash = pathutils . hasTrailingSlash ( originalPathname ) ;
71
79
72
80
// We want to check for some other files, namely an `index.html` if this were a directory.
73
81
const pathAsDirectoryWithIndex = pathutils . asDirectoryIndex (
@@ -83,7 +91,8 @@ module.exports = function () {
83
91
! cleanUrlRules
84
92
) {
85
93
return res . superstatic . handle ( {
86
- redirect : pathutils . addTrailingSlash ( pathname ) + search ,
94
+ redirect :
95
+ pathutils . addTrailingSlash ( originalPathname ) + search ,
87
96
} ) ;
88
97
}
89
98
if (
@@ -94,13 +103,14 @@ module.exports = function () {
94
103
// No infinite redirects
95
104
return res . superstatic . handle ( {
96
105
redirect : normalizeRedirectPath (
97
- pathutils . removeTrailingSlash ( pathname ) + search
106
+ pathutils . removeTrailingSlash ( originalPathname ) + search
98
107
) ,
99
108
} ) ;
100
109
}
101
110
if ( trailingSlashBehavior === true && ! hasTrailingSlash ) {
102
111
return res . superstatic . handle ( {
103
- redirect : pathutils . addTrailingSlash ( pathname ) + search ,
112
+ redirect :
113
+ pathutils . addTrailingSlash ( originalPathname ) + search ,
104
114
} ) ;
105
115
}
106
116
// 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 () {
114
124
// We want to know if a specific mutation of the path exists.
115
125
if ( cleanUrlRules ) {
116
126
let appendedPath = pathname ;
127
+ let appendedOriginalPath = originalPathname ;
117
128
if ( hasTrailingSlash ) {
118
129
if ( trailingSlashBehavior !== undefined ) {
119
130
// We want to remove the trailing slash and see if a file exists with an .html attached.
120
131
appendedPath =
121
- pathutils . removeTrailingString ( pathname , "/" ) + ".html" ;
132
+ pathutils . removeTrailingString ( appendedPath , "/" ) + ".html" ;
133
+ appendedOriginalPath =
134
+ pathutils . removeTrailingString ( appendedOriginalPath , "/" ) +
135
+ ".html" ;
122
136
}
123
137
} else {
124
138
// Let's see if our path is a simple clean URL missing a .HTML5
125
139
appendedPath += ".html" ;
140
+ appendedOriginalPath += ".html" ;
126
141
}
127
142
128
143
return providerResult ( req , res , appendedPath ) . then (
@@ -134,7 +149,8 @@ module.exports = function () {
134
149
// (This works because we are in the cleanURL block.)
135
150
return res . superstatic . handle ( {
136
151
redirect : normalizeRedirectPath (
137
- pathutils . removeTrailingSlash ( pathname ) + search
152
+ pathutils . removeTrailingSlash ( originalPathname ) +
153
+ search
138
154
) ,
139
155
} ) ;
140
156
}
@@ -148,17 +164,26 @@ module.exports = function () {
148
164
appendedPath ,
149
165
"/index"
150
166
) ;
167
+ appendedOriginalPath = pathutils . removeTrailingString (
168
+ appendedOriginalPath ,
169
+ ".html"
170
+ ) ;
171
+ appendedOriginalPath = pathutils . removeTrailingString (
172
+ appendedOriginalPath ,
173
+ "/index"
174
+ ) ;
151
175
return res . superstatic . handle ( {
152
176
redirect :
153
- pathutils . addTrailingSlash ( appendedPath ) + search ,
177
+ pathutils . addTrailingSlash ( appendedOriginalPath ) +
178
+ search ,
154
179
} ) ;
155
180
}
156
181
// 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" ) ) {
158
183
return res . superstatic . handle ( {
159
184
redirect : normalizeRedirectPath (
160
185
pathutils . removeTrailingString (
161
- appendedPath ,
186
+ appendedOriginalPath ,
162
187
"/index.html"
163
188
) + search
164
189
) ,
0 commit comments