import { matchPath , RouteProps } from 'react-router-dom' ; import { AsyncRouteProps , InitialProps , CtxBase } from './types' ; import { isAsyncComponent } from './utils' ; export async function loadInitialProps ( pathname , routes , ctx ) { const promises = [ ] ; const matchedComponent = routes . find ( ( route ) => { const match = matchPath ( pathname , { ... route , path : route . path || '*' } ) ; if ( match && route . component && isAsyncComponent ( route . component ) ) { const component = route . component ; promises . push ( component . load ? component . load ( ) . then ( ( ) => component . getInitialProps ( { match , ... ctx } ) ) : component . getInitialProps ( { match , ... ctx } ) ) ; } return ! ! match ; } ) ; return { match : matchedComponent , data : ( await Promise . all ( promises ) ) [ 0 ] } ; }