Skip to content

Commit 25dee39

Browse files
committed
Store compiled patterns in a cache object, not on the route element's props.
Fixes compat with React 0.14 and #148.
1 parent 5250359 commit 25dee39

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

lib/matchRoutes.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var React = require('react');
66
var cloneWithProps = require('react/lib/cloneWithProps');
77
var assign = Object.assign || require('object.assign');
88

9+
var patternCache = {};
10+
911
/**
1012
* Match routes against a path
1113
*
@@ -33,9 +35,12 @@ function matchRoutes(routes, path) {
3335
}
3436

3537
if (current.props.path) {
36-
current.props.pattern = current.props.pattern || new URLPattern(current.props.path, createCompiler(current.props));
38+
// Technically, this cache will incorrectly be used if a user defines two routes
39+
// with identical paths but different compilers. FIXME?
40+
var pattern = patternCache[current.props.path] ||
41+
new URLPattern(current.props.path, createCompiler(current.props));
3742
if (!page) {
38-
match = current.props.pattern.match(path);
43+
match = pattern.match(path);
3944
if (match) {
4045
page = current;
4146
}

tests/matchRoutes.js

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ describe('matchRoutes', function() {
2020
];
2121

2222
afterEach(function() {
23-
// Remove any compiled regex patterns from all routes
24-
routes.forEach(function(r) {
25-
r.props.pattern = null;
26-
});
2723
// In case we overrode this, reset it.
2824
Router.createURLPatternCompiler = function () { return new URLPattern.Compiler(); };
2925
});

0 commit comments

Comments
 (0)