File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -324,7 +324,15 @@ export default class RouteNode {
324324 return segment . absolute ? segmentPath : path + segmentPath ;
325325 } , '' ) ;
326326
327- return path + ( searchPart ? '?' + searchPart : '' ) ;
327+ let finalPath = path ;
328+
329+ if ( options . trailingSlash === true ) {
330+ finalPath = / \/ $ / . test ( path ) ? path : `${ path } /` ;
331+ } else if ( options . trailingSlash === false ) {
332+ finalPath = / \/ $ / . test ( path ) ? path . slice ( 0 , - 1 ) : path ;
333+ }
334+
335+ return finalPath + ( searchPart ? '?' + searchPart : '' ) ;
328336 }
329337
330338 getMetaFromSegments ( segments ) {
@@ -354,12 +362,6 @@ export default class RouteNode {
354362 const options = { ...defaultOptions , ...opts } ;
355363 const path = this . buildPathFromSegments ( this . getSegmentsByName ( routeName ) , params , options ) ;
356364
357- if ( options . trailingSlash === true ) {
358- return / \/ $ / . test ( path ) ? path : `${ path } /` ;
359- } else if ( options . trailingSlash === false ) {
360- return / \/ $ / . test ( path ) ? path . slice ( 0 , - 1 ) : path ;
361- }
362-
363365 return path ;
364366 }
365367
Original file line number Diff line number Diff line change @@ -513,6 +513,17 @@ describe('RouteNode', function () {
513513 withoutMeta ( match2 ) . should . eql ( { name : 'home' , params : { s : '3' } } ) ;
514514 } ) ;
515515 } ) ;
516+
517+ it ( 'should trim trailing slashes when building paths' , ( ) => {
518+ const node = new RouteNode ( '' , '' , [
519+ new RouteNode ( 'a' , '/a' , [
520+ new RouteNode ( 'b' , '/?c' )
521+ ] )
522+ ] ) ;
523+
524+ node . buildPath ( 'a.b' , { } , { trailingSlash : false } ) . should . eql ( '/a' ) ;
525+ node . buildPath ( 'a.b' , { c : 1 } , { trailingSlash : false } ) . should . eql ( '/a?c=1' ) ;
526+ } ) ;
516527} ) ;
517528
518529
You can’t perform that action at this time.
0 commit comments