concept match in category isomorphic web applications

appears as: match, matches
Isomorphic Web Applications: Universal Development with React

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.

  • Handle specific routes (for example, the cart and products routes) with React Router using the match function
  • 7.2.1. Using match to handle routing

    The renderView middleware handles route matching. It uses a function called match that’s provided by React Router. After you’ve added it, you’ll be able to navigate to each route you created in sharedRoutes. Figure 7.9 shows sample output of navigating to localhost:3000/cart after you add the code from listings 7.4 and 7.5.

    Listing B.1. Fetching data on the server—middleware/renderView.jsx
    //...other code import { StaticRouter } from 'react-router'; #1 import { matchRoutes, renderRoutes } from 'react-router-config'; #2 import routes from '../shared/sharedRoutesv4'; #2 export default function renderView(req, res, next) { const matches = matchRoutes(routes, req.path); #3 const context = {}; #4 if (matches) { const store = initRedux(); let actions = []; matches.map(({match, route}) => { #5 const component = route.component; if (component) { if (component.displayName && component.displayName.toLowerCase().indexOf('connect') > -1 ) { let parentComponent = component.WrappedComponent; if (parentComponent.prefetchActions) { actions.push(parentComponent.prefetchActions()); } else if (parentComponent.wrappedComponent && parentComponent.wrappedComponent().prefetchActions) { #6 actions.push(parentComponent.wrappedComponent().prefetchActions()); } } else if (component.prefetchActions) { actions.push(component.prefetchActions()); } } }); actions = actions.reduce((flat, toFlatten) => { return flat.concat(toFlatten); }, []); const promises = actions.map((initialAction) => { return store.dispatch(initialAction()); }); Promise.all(promises).then(() => { const app = renderToString( <Provider store={store}> <StaticRouter location={req.url} context={context}> #7 { renderRoutes(routes) } #8 </StaticRouter> </Provider> ); if (!context.url) { #9 const html = renderToString(<HTML renderedToStringComponents={app} / >); res.send(`<!DOCTYPE html>${html}`); } }); } else { next(); } } #1 Imports the dependencies #2 Imports the dependencies #3 Calls matchRoutes to find out which components are being loaded #4 Creates a context object used to determine whether there’s a route redirect #5 Iterates through the matches so you can call the prefetchActions function. Most of this method is the same as in chapter 7. #6 This is the major difference in the code. Because App is wrapped in two HOCs, you have to check two levels down to access the root component. #7 Uses the StaticRouter on the server instead of the BrowserRouter, as you don’t need a history. Passes in the location and context object. #8 Calls renderRoutes just as in main.jsx #9 Checks the context to make sure there isn’t a redirect !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":15}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":76}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":8}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":8}],[{\"line\":3,\"ch\":2},{\"line\":3,\"ch\":27}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":76}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":76}],[{\"line\":8,\"ch\":2},{\"line\":8,\"ch\":76}],[{\"line\":9,\"ch\":2},{\"line\":9,\"ch\":76}],[{\"line\":11,\"ch\":2},{\"line\":11,\"ch\":16}],[{\"line\":13,\"ch\":4},{\"line\":13,\"ch\":21}],[{\"line\":14,\"ch\":4},{\"line\":14,\"ch\":76}],[{\"line\":15,\"ch\":6},{\"line\":15,\"ch\":40}],[{\"line\":16,\"ch\":6},{\"line\":16,\"ch\":22}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":36}],[{\"line\":18,\"ch\":12},{\"line\":18,\"ch\":71}],[{\"line\":7,\"ch\":49},{\"line\":7,\"ch\":52}],[{\"line\":11,\"ch\":13},{\"line\":11,\"ch\":16}],[{\"line\":16,\"ch\":19},{\"line\":16,\"ch\":22}],[{\"line\":19,\"ch\":8},{\"line\":19,\"ch\":11}],[{\"line\":21,\"ch\":45},{\"line\":21,\"ch\":48}],[{\"line\":24,\"ch\":55},{\"line\":24,\"ch\":58}],[{\"line\":27,\"ch\":44},{\"line\":27,\"ch\":47}],[{\"line\":50,\"ch\":22},{\"line\":50,\"ch\":25}],[{\"line\":20,\"ch\":10},{\"line\":20,\"ch\":59}],[{\"line\":21,\"ch\":10},{\"line\":21,\"ch\":48}],[{\"line\":22,\"ch\":12},{\"line\":22,\"ch\":60}],[{\"line\":23,\"ch\":10},{\"line\":23,\"ch\":56}],[{\"line\":24,\"ch\":5},{\"line\":24,\"ch\":76}],[{\"line\":25,\"ch\":8},{\"line\":25,\"ch\":75}],[{\"line\":1,\"ch\":22},{\"line\":1,\"ch\":23}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":9,\"ch\":19},{\"line\":9,\"ch\":20}],[{\"line\":14,\"ch\":30},{\"line\":14,\"ch\":31}],[{\"line\":23,\"ch\":10},{\"line\":23,\"ch\":11}],[{\"line\":26,\"ch\":10},{\"line\":26,\"ch\":11}],[{\"line\":27,\"ch\":8},{\"line\":27,\"ch\":9}],[{\"line\":29,\"ch\":8},{\"line\":29,\"ch\":9}],[{\"line\":30,\"ch\":6},{\"line\":30,\"ch\":7}],[{\"line\":31,\"ch\":4},{\"line\":31,\"ch\":5}],[{\"line\":34,\"ch\":4},{\"line\":34,\"ch\":5}],[{\"line\":38,\"ch\":4},{\"line\":38,\"ch\":5}],[{\"line\":41,\"ch\":30},{\"line\":41,\"ch\":31}],[{\"line\":43,\"ch\":29},{\"line\":43,\"ch\":30}],[{\"line\":44,\"ch\":28},{\"line\":44,\"ch\":29}],[{\"line\":45,\"ch\":35},{\"line\":45,\"ch\":36}],[{\"line\":51,\"ch\":73},{\"line\":51,\"ch\":74}],[{\"line\":53,\"ch\":39},{\"line\":53,\"ch\":40}],[{\"line\":54,\"ch\":6},{\"line\":54,\"ch\":7}],[{\"line\":55,\"ch\":4},{\"line\":55,\"ch\":5}],[{\"line\":56,\"ch\":2},{\"line\":56,\"ch\":3}],[{\"line\":58,\"ch\":2},{\"line\":58,\"ch\":3}],[{\"line\":59,\"ch\":0},{\"line\":59,\"ch\":1}],[{\"line\":27,\"ch\":8},{\"line\":27,\"ch\":47}],[{\"line\":28,\"ch\":10},{\"line\":28,\"ch\":52}],[{\"line\":1,\"ch\":22},{\"line\":1,\"ch\":23}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":9,\"ch\":19},{\"line\":9,\"ch\":20}],[{\"line\":14,\"ch\":30},{\"line\":14,\"ch\":31}],[{\"line\":23,\"ch\":10},{\"line\":23,\"ch\":11}],[{\"line\":26,\"ch\":10},{\"line\":26,\"ch\":11}],[{\"line\":27,\"ch\":8},{\"line\":27,\"ch\":9}],[{\"line\":29,\"ch\":8},{\"line\":29,\"ch\":9}],[{\"line\":30,\"ch\":6},{\"line\":30,\"ch\":7}],[{\"line\":31,\"ch\":4},{\"line\":31,\"ch\":5}],[{\"line\":34,\"ch\":4},{\"line\":34,\"ch\":5}],[{\"line\":38,\"ch\":4},{\"line\":38,\"ch\":5}],[{\"line\":41,\"ch\":30},{\"line\":41,\"ch\":31}],[{\"line\":43,\"ch\":29},{\"line\":43,\"ch\":30}],[{\"line\":44,\"ch\":28},{\"line\":44,\"ch\":29}],[{\"line\":45,\"ch\":35},{\"line\":45,\"ch\":36}],[{\"line\":51,\"ch\":73},{\"line\":51,\"ch\":74}],[{\"line\":53,\"ch\":39},{\"line\":53,\"ch\":40}],[{\"line\":54,\"ch\":6},{\"line\":54,\"ch\":7}],[{\"line\":55,\"ch\":4},{\"line\":55,\"ch\":5}],[{\"line\":56,\"ch\":2},{\"line\":56,\"ch\":3}],[{\"line\":58,\"ch\":2},{\"line\":58,\"ch\":3}],[{\"line\":59,\"ch\":0},{\"line\":59,\"ch\":1}],[{\"line\":1,\"ch\":22},{\"line\":1,\"ch\":23}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":9,\"ch\":19},{\"line\":9,\"ch\":20}],[{\"line\":14,\"ch\":30},{\"line\":14,\"ch\":31}],[{\"line\":23,\"ch\":10},{\"line\":23,\"ch\":11}],[{\"line\":26,\"ch\":10},{\"line\":26,\"ch\":11}],[{\"line\":27,\"ch\":8},{\"line\":27,\"ch\":9}],[{\"line\":29,\"ch\":8},{\"line\":29,\"ch\":9}],[{\"line\":30,\"ch\":6},{\"line\":30,\"ch\":7}],[{\"line\":31,\"ch\":4},{\"line\":31,\"ch\":5}],[{\"line\":34,\"ch\":4},{\"line\":34,\"ch\":5}],[{\"line\":38,\"ch\":4},{\"line\":38,\"ch\":5}],[{\"line\":41,\"ch\":30},{\"line\":41,\"ch\":31}],[{\"line\":43,\"ch\":29},{\"line\":43,\"ch\":30}],[{\"line\":44,\"ch\":28},{\"line\":44,\"ch\":29}],[{\"line\":45,\"ch\":35},{\"line\":45,\"ch\":36}],[{\"line\":51,\"ch\":73},{\"line\":51,\"ch\":74}],[{\"line\":53,\"ch\":39},{\"line\":53,\"ch\":40}],[{\"line\":54,\"ch\":6},{\"line\":54,\"ch\":7}],[{\"line\":55,\"ch\":4},{\"line\":55,\"ch\":5}],[{\"line\":56,\"ch\":2},{\"line\":56,\"ch\":3}],[{\"line\":58,\"ch\":2},{\"line\":58,\"ch\":3}],[{\"line\":59,\"ch\":0},{\"line\":59,\"ch\":1}],[{\"line\":31,\"ch\":4},{\"line\":31,\"ch\":7}],[{\"line\":38,\"ch\":4},{\"line\":38,\"ch\":7}],[{\"line\":55,\"ch\":4},{\"line\":55,\"ch\":7}],[{\"line\":42,\"ch\":10},{\"line\":42,\"ch\":23}],[{\"line\":43,\"ch\":12},{\"line\":43,\"ch\":30}],[{\"line\":44,\"ch\":12},{\"line\":44,\"ch\":76}],[{\"line\":45,\"ch\":12},{\"line\":45,\"ch\":76}],[{\"line\":46,\"ch\":10},{\"line\":46,\"ch\":25}],[{\"line\":50,\"ch\":6},{\"line\":50,\"ch\":76}]]"} !@%STYLE%@!