Skip to content

Commit f7a18b0

Browse files
committed
fix: build path correctly when 'trailingSlash' is set and with query params
1 parent 2a49f9f commit f7a18b0

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

modules/RouteNode.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff 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

test/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)