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