@@ -33,7 +33,7 @@ declare global {
33
33
}
34
34
35
35
export const createClientConfiguration = ( req : express . Request ) : ClientConfiguration => {
36
- const base = relativeRoot ( req )
36
+ const base = relativeRoot ( req . originalUrl )
37
37
38
38
return {
39
39
base,
@@ -108,15 +108,28 @@ export const authenticated = async (req: express.Request): Promise<boolean> => {
108
108
109
109
/**
110
110
* Get the relative path that will get us to the root of the page. For each
111
- * slash we need to go up a directory. For example:
111
+ * slash we need to go up a directory. Will not have a trailing slash.
112
+ *
113
+ * For example:
114
+ *
112
115
* / => .
113
116
* /foo => .
114
117
* /foo/ => ./..
115
118
* /foo/bar => ./..
116
119
* /foo/bar/ => ./../..
120
+ *
121
+ * All paths must be relative in order to work behind a reverse proxy since we
122
+ * we do not know the base path. Anything that needs to be absolute (for
123
+ * example cookies) must get the base path from the frontend.
124
+ *
125
+ * All relative paths must be prefixed with the relative root to ensure they
126
+ * work no matter the depth at which they happen to appear.
127
+ *
128
+ * For Express `req.originalUrl` should be used as they remove the base from the
129
+ * standard `url` property making it impossible to get the true depth.
117
130
*/
118
- export const relativeRoot = ( req : express . Request ) : string => {
119
- const depth = ( req . originalUrl . split ( "?" , 1 ) [ 0 ] . match ( / \/ / g) || [ ] ) . length
131
+ export const relativeRoot = ( originalUrl : string ) : string => {
132
+ const depth = ( originalUrl . split ( "?" , 1 ) [ 0 ] . match ( / \/ / g) || [ ] ) . length
120
133
return normalize ( "./" + ( depth > 1 ? "../" . repeat ( depth - 1 ) : "" ) )
121
134
}
122
135
@@ -138,7 +151,7 @@ export const redirect = (
138
151
}
139
152
} )
140
153
141
- const relativePath = normalize ( `${ relativeRoot ( req ) } /${ to } ` , true )
154
+ const relativePath = normalize ( `${ relativeRoot ( req . originalUrl ) } /${ to } ` , true )
142
155
const queryString = qs . stringify ( query )
143
156
const redirectPath = `${ relativePath } ${ queryString ? `?${ queryString } ` : "" } `
144
157
logger . debug ( `redirecting from ${ req . originalUrl } to ${ redirectPath } ` )
0 commit comments