Skip to content

Commit

Permalink
Fix context.next() for multiple nested routes (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy committed Apr 20, 2017
1 parent a1c9c81 commit b23bce1
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased][unreleased]

- Fix `context.next()` for multiple nested routes
([#91](https://github.com/kriasoft/universal-router/pull/91))
- Add `pretty` option for `generateUrls(router, options)` to prettier encoding of URI path segments
([#88](https://github.com/kriasoft/universal-router/pull/88))
- Add source maps for minified builds ([#87](https://github.com/kriasoft/universal-router/pull/87))
Expand Down
42 changes: 27 additions & 15 deletions dist/universal-router.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/universal-router.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/universal-router.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/universal-router.min.js.map

Large diffs are not rendered by default.

36 changes: 25 additions & 11 deletions src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import matchPath from './matchPath';
import matchRoute from './matchRoute';
import resolveRoute from './resolveRoute';

function isChildRoute(parentRoute, childRoute) {
let route = childRoute;
while (route) {
route = route.parent;
if (route === parentRoute) {
return true;
}
}
return false;
}

class Router {
constructor(routes, options = {}) {
if (Object(routes) !== routes) {
Expand All @@ -30,12 +41,19 @@ class Router {
typeof pathOrContext === 'string' ? { path: pathOrContext } : pathOrContext);
const match = matchRoute(this.root, this.baseUrl, context.path.substr(this.baseUrl.length));
const resolve = this.resolveRoute;
let matches;
let parent;
let matches = null;
let nextMatches = null;

function next(resume) {
parent = matches ? matches.value.route.parent : null;
matches = match.next();
function next(resume, parent = matches.value.route) {
matches = nextMatches || match.next();
nextMatches = null;

if (!resume) {
if (matches.done || !isChildRoute(parent, matches.value.route)) {
nextMatches = matches;
return Promise.resolve(null);
}
}

if (matches.done) {
return Promise.reject(Object.assign(
Expand All @@ -52,18 +70,14 @@ class Router {
return result;
}

if (resume || parent === matches.value.route.parent) {
return next(resume);
}

return result;
return next(resume, parent);
});
}

context.url = context.path;
context.next = next;

return next(true);
return next(true, this.root);
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/matchPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import pathToRegexp from 'path-to-regexp';
const cache = new Map();

function decodeParam(val) {
if (!val) {
return val;
}

try {
return decodeURIComponent(val);
} catch (err) {
Expand Down Expand Up @@ -46,7 +42,7 @@ function matchPath(routePath, urlPath, end, parentParams) {
}

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

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

0 comments on commit b23bce1

Please sign in to comment.