Skip to content

Commit aa80e2f

Browse files
committed
Fixed issue with trailing slashes
1 parent 488b027 commit aa80e2f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

modules/components/Routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function findMatches(path, routes, defaultRoute, notFoundRoute) {
3232

3333
if (matches != null) {
3434
var rootParams = getRootMatch(matches).params;
35-
35+
3636
params = route.props.paramNames.reduce(function (params, paramName) {
3737
params[paramName] = rootParams[paramName];
3838
return params;

modules/utils/Path.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function encodeURLPath(path) {
1616

1717
var paramCompileMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$]*)|[*.()\[\]\\+|{}^$]/g;
1818
var paramInjectMatcher = /:([a-zA-Z_$?][a-zA-Z0-9_$?]*)|[*]/g;
19+
var paramInjectTrailingSlashMatcher = /\/\/\?|\/\?/g;
1920
var queryMatcher = /\?(.+)/;
2021

2122
var _compiledPatterns = {};
@@ -83,11 +84,10 @@ var Path = {
8384

8485
var splatIndex = 0;
8586

86-
return pattern.replace(paramInjectMatcher, function (match, paramName) {
87-
console.log(pattern, match, paramName);
87+
var url = pattern.replace(paramInjectMatcher, function (match, paramName) {
8888
paramName = paramName || 'splat';
8989

90-
// If param is optional dont check for existance
90+
// If param is optional don't check for existence
9191
if (paramName.slice(-1) !== '?') {
9292
invariant(
9393
params[paramName] != null,
@@ -114,6 +114,11 @@ var Path = {
114114

115115
return encodeURLPath(segment);
116116
});
117+
url = url.replace(paramInjectTrailingSlashMatcher, '/');
118+
119+
console.log(url);
120+
121+
return url;
117122
},
118123

119124
/**

modules/utils/__tests__/Path-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ describe('Path.extractParams', function () {
6161
});
6262
});
6363
});
64+
describe('and the pattern and forward slash are optional', function () {
65+
var pattern = 'comments/:id?/edit'
66+
67+
describe('and the path matches with supplied param', function () {
68+
it('returns an object with the params', function () {
69+
expect(Path.extractParams(pattern, 'comments/123/edit')).toEqual({ id: '123' });
70+
});
71+
});
72+
describe('and the path matches without supplied param', function () {
73+
it('returns an object with param set to null', function () {
74+
expect(Path.extractParams(pattern, 'comments/edit')).toEqual({id: null});
75+
});
76+
});
77+
});
6478
describe('and the path does not match', function () {
6579
it('returns null', function () {
6680
expect(Path.extractParams(pattern, 'users/123')).toBe(null);
@@ -192,6 +206,18 @@ describe('Path.injectParams', function () {
192206
});
193207
});
194208

209+
describe('and a param and forward slash are optional', function () {
210+
var pattern = 'comments/:id?/?edit';
211+
212+
it('returns the correct path when param is supplied', function () {
213+
expect(Path.injectParams(pattern, {id:'123'})).toEqual('comments/123/edit');
214+
});
215+
216+
it('returns the correct path when param is not supplied', function () {
217+
expect(Path.injectParams(pattern, {})).toEqual('comments/edit');
218+
});
219+
});
220+
195221
describe('and all params are present', function () {
196222
it('returns the correct path', function () {
197223
expect(Path.injectParams(pattern, { id: 'abc' })).toEqual('comments/abc/edit');

0 commit comments

Comments
 (0)