@@ -7,17 +7,17 @@ module.exports = opts => {
7
7
maxRedirects : 5 ,
8
8
...opts
9
9
} ;
10
- const { defaultPath, maxRedirects } = opts ;
11
10
return ( req , res , next ) => {
12
11
if ( ! req . session )
13
12
return next ( new Error ( 'Sessions required for `express-redirect-loop`' ) ) ;
14
13
15
14
const { redirect, end } = res ;
16
15
17
16
res . end = function ( chunk , encoding ) {
18
- if ( ! req . xhr ) {
17
+ // instead of `!req.xhr` we need to use !accepts HTML
18
+ // because Fetch does not provide XMLHttpRequest
19
+ if ( req . accepts ( 'html' ) ) {
19
20
req . session . prevPrevPath = req . session . prevPath ;
20
- req . session . prevPrevMethod = req . session . prevMethod ;
21
21
req . session . prevPath = req . originalUrl ;
22
22
req . session . prevMethod = req . method ;
23
23
// if it was a redirect then store how many times
@@ -53,30 +53,27 @@ module.exports = opts => {
53
53
54
54
address = this . location ( address ) . get ( 'Location' ) ;
55
55
56
- req . prevPrevPath = req . session . prevPrevPath || defaultPath ;
57
- req . prevPrevMethod = req . session . prevPrevMethod || 'GET' ;
58
- req . prevPath = req . session . prevPath || defaultPath ;
59
- req . prevMethod = req . session . prevMethod || req . method ;
60
- req . maxRedirects = req . session . maxRedirects || 1 ;
56
+ const prevPrevPath = req . session . prevPrevPath || opts . defaultPath ;
57
+ const prevPath = req . session . prevPath || opts . defaultPath ;
58
+ const prevMethod = req . session . prevMethod || req . method ;
59
+ const maxRedirects = req . session . maxRedirects || 1 ;
61
60
62
- if (
63
- req . prevPath &&
64
- address === req . prevPath &&
65
- req . method === req . prevMethod
66
- ) {
61
+ if ( prevPath && address === prevPath && req . method === prevMethod ) {
67
62
if (
68
- req . prevPrevPath &&
69
- address !== req . prevPrevPath &&
70
- req . maxRedirects <= maxRedirects
63
+ prevPrevPath &&
64
+ address !== prevPrevPath &&
65
+ maxRedirects <= opts . maxRedirects
71
66
) {
72
- address = req . prevPrevPath ;
67
+ address = prevPrevPath ;
73
68
} else {
74
69
// if the prevPrevPath w/o querystring is !== prevPrevPath
75
70
// then redirect then to prevPrevPath w/o querystring
76
- const { pathname } = new Url ( req . prevPrevPath , { } ) ;
77
- if ( pathname === req . prevPrevPath ) address = '/' ;
71
+ const { pathname } = new Url ( prevPrevPath , { } ) ;
72
+ if ( pathname === prevPrevPath ) address = '/' ;
78
73
else address = pathname ;
79
74
}
75
+ } else if ( maxRedirects > opts . maxRedirects ) {
76
+ address = opts . defaultPath ;
80
77
}
81
78
82
79
redirect . call ( res , status , address ) ;
0 commit comments