UNPKG

react-router

Version:

A complete routing library for React

65 lines (52 loc) 2.67 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } import invariant from 'invariant'; import createMemoryHistory from './createMemoryHistory'; import createTransitionManager from './createTransitionManager'; import { createRoutes } from './RouteUtils'; import { createRouterObject, createRoutingHistory } from './RouterUtils'; /** * A high-level API to be used for server-side rendering. * * This function matches a location to a set of routes and calls * callback(error, redirectLocation, renderProps) when finished. * * Note: You probably don't want to use this in a browser unless you're using * server-side rendering with async routes. */ function match(_ref, callback) { var history = _ref.history; var routes = _ref.routes; var location = _ref.location; var options = _objectWithoutProperties(_ref, ['history', 'routes', 'location']); !(history || location) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'match needs a history or a location') : invariant(false) : void 0; history = history ? history : createMemoryHistory(options); var transitionManager = createTransitionManager(history, createRoutes(routes)); var unlisten = void 0; if (location) { // Allow match({ location: '/the/path', ... }) location = history.createLocation(location); } else { // Pick up the location from the history via synchronous history.listen // call if needed. unlisten = history.listen(function (historyLocation) { location = historyLocation; }); } var router = createRouterObject(history, transitionManager); history = createRoutingHistory(history, transitionManager); transitionManager.match(location, function (error, redirectLocation, nextState) { callback(error, redirectLocation, nextState && _extends({}, nextState, { history: history, router: router, matchContext: { history: history, transitionManager: transitionManager, router: router } })); // Defer removing the listener to here to prevent DOM histories from having // to unwind DOM event listeners unnecessarily, in case callback renders a // <Router> and attaches another history listener. if (unlisten) { unlisten(); } }); } export default match;