Skip to content

Commit b23bce1

Browse files
committed
Fix context.next() for multiple nested routes (#91)
1 parent a1c9c81 commit b23bce1

File tree

8 files changed

+105
-52
lines changed

8 files changed

+105
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased][unreleased]
66

7+
- Fix `context.next()` for multiple nested routes
8+
([#91](https://github.com/kriasoft/universal-router/pull/91))
79
- Add `pretty` option for `generateUrls(router, options)` to prettier encoding of URI path segments
810
([#88](https://github.com/kriasoft/universal-router/pull/88))
911
- Add source maps for minified builds ([#87](https://github.com/kriasoft/universal-router/pull/87))

dist/universal-router.js

Lines changed: 27 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/universal-router.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/universal-router.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/universal-router.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Router.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import matchPath from './matchPath';
1212
import matchRoute from './matchRoute';
1313
import resolveRoute from './resolveRoute';
1414

15+
function isChildRoute(parentRoute, childRoute) {
16+
let route = childRoute;
17+
while (route) {
18+
route = route.parent;
19+
if (route === parentRoute) {
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
1526
class Router {
1627
constructor(routes, options = {}) {
1728
if (Object(routes) !== routes) {
@@ -30,12 +41,19 @@ class Router {
3041
typeof pathOrContext === 'string' ? { path: pathOrContext } : pathOrContext);
3142
const match = matchRoute(this.root, this.baseUrl, context.path.substr(this.baseUrl.length));
3243
const resolve = this.resolveRoute;
33-
let matches;
34-
let parent;
44+
let matches = null;
45+
let nextMatches = null;
3546

36-
function next(resume) {
37-
parent = matches ? matches.value.route.parent : null;
38-
matches = match.next();
47+
function next(resume, parent = matches.value.route) {
48+
matches = nextMatches || match.next();
49+
nextMatches = null;
50+
51+
if (!resume) {
52+
if (matches.done || !isChildRoute(parent, matches.value.route)) {
53+
nextMatches = matches;
54+
return Promise.resolve(null);
55+
}
56+
}
3957

4058
if (matches.done) {
4159
return Promise.reject(Object.assign(
@@ -52,18 +70,14 @@ class Router {
5270
return result;
5371
}
5472

55-
if (resume || parent === matches.value.route.parent) {
56-
return next(resume);
57-
}
58-
59-
return result;
73+
return next(resume, parent);
6074
});
6175
}
6276

6377
context.url = context.path;
6478
context.next = next;
6579

66-
return next(true);
80+
return next(true, this.root);
6781
}
6882
}
6983

src/matchPath.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import pathToRegexp from 'path-to-regexp';
1212
const cache = new Map();
1313

1414
function decodeParam(val) {
15-
if (!val) {
16-
return val;
17-
}
18-
1915
try {
2016
return decodeURIComponent(val);
2117
} catch (err) {
@@ -46,7 +42,7 @@ function matchPath(routePath, urlPath, end, parentParams) {
4642
}
4743

4844
for (let i = 1; i < m.length; i += 1) {
49-
params[regexp.keys[i - 1].name] = decodeParam(m[i]);
45+
params[regexp.keys[i - 1].name] = m[i] && decodeParam(m[i]);
5046
}
5147

5248
return { path: path === '' ? '/' : path, keys: regexp.keys.slice(), params };

0 commit comments

Comments
 (0)