@@ -23,39 +23,26 @@ module.exports = (server, options, allDone) => {
23
23
return reply . redirect ( redirectTo ) . code ( options . statusCode ) ;
24
24
} ;
25
25
26
- if ( options . method === 'append' ) {
27
- server . ext ( 'onPreResponse' , ( request , reply ) => {
28
- const statusCode = request . response . output ? request . response . output . statusCode : request . response . statusCode ;
29
- // if the route was already found by hapi then just ignore it:
30
- if ( statusCode !== 404 ) {
31
- return reply . continue ( ) ;
32
- }
33
- const method = request . method . toLowerCase ( ) ;
34
- // before failing, first check if there's a slashed route we can redirect to:
35
- if ( [ 'get' , 'head' ] . indexOf ( method ) !== - 1 && request . path [ request . path . length - 1 ] !== '/' ) {
36
- const slashedPath = `${ request . path } /` ;
37
- return doRedirect ( slashedPath , request , reply ) ;
38
- }
39
- // otherwise it really is a 404:
26
+ // if (options.method === 'append') {
27
+ server . ext ( 'onPreResponse' , ( request , reply ) => {
28
+ const statusCode = request . response . output ? request . response . output . statusCode : request . response . statusCode ;
29
+ // if the route was already found by hapi then just ignore it:
30
+ if ( statusCode !== 404 ) {
40
31
return reply . continue ( ) ;
41
- } ) ;
42
- } else if ( options . method === 'remove' ) {
43
- server . ext ( 'onPreResponse' , ( request , reply ) => {
44
- const statusCode = request . response . output ? request . response . output . statusCode : request . response . statusCode ;
45
- // if the route was already found by hapi then just ignore it:
46
- if ( statusCode !== 404 ) {
47
- return reply . continue ( ) ;
48
- }
49
- // before failing, check if there's an unslashed route we can redirect to:
50
- const method = request . method . toLowerCase ( ) ;
51
- if ( [ 'get' , 'head' ] . indexOf ( method ) !== - 1 && request . path !== '/' && request . path [ request . path . length - 1 ] === '/' ) {
52
- const slashlessPath = request . path . replace ( / \/ $ / , '' ) ;
53
- return doRedirect ( slashlessPath , request , reply ) ;
54
- }
55
- // otherwise it really is a 404:
56
- return reply . continue ( ) ;
57
- } ) ;
58
- }
32
+ }
33
+ const method = request . method . toLowerCase ( ) ;
34
+ // pick a condition checker based on either 'append' or 'remove' mode:
35
+ const condition = options . method === 'append' ? ( ) => request . path [ request . path . length - 1 ] !== '/' :
36
+ ( ) => request . path !== '/' && request . path [ request . path . length - 1 ] === '/' ;
37
+ // see if we need to do a redirect for either slashed/slashless path:
38
+ if ( [ 'get' , 'head' ] . indexOf ( method ) !== - 1 && condition ( ) ) {
39
+ // pick a redirection based on either 'append' or 'remove' mode:
40
+ const redirectPath = options . method === 'append' ? `${ request . path } /` : request . path . replace ( / \/ $ / , '' ) ;
41
+ return doRedirect ( redirectPath , request , reply ) ;
42
+ }
43
+ // otherwise it really is a 404:
44
+ return reply . continue ( ) ;
45
+ } ) ;
59
46
allDone ( ) ;
60
47
} ;
61
48
0 commit comments