import React from 'react'; import { renderToString } from 'react-dom/server'; import { RoutingContext, match } from 'react-router'; import createLocation from 'history/lib/createLocation'; export function matchRoute(routes, url) { const location = createLocation(url); return new Promise((resolve, reject) => { match({ routes, location }, (error, redirectLocation, renderProps) => { resolve({error, redirectLocation, renderProps}); }); }); } export function* renderPage(routes, url, state) { const { error, redirectLocation, renderProps } = yield matchRoute(routes, url); if (error) { throw new Error(error.message); } else if (redirectLocation) { return redirectLocation.pathname + redirectLocation.search; } else if (renderProps === null) { return false; } const content = renderToString(); return '\n' + content.replace('', ` `); }