Last active
February 19, 2017 00:35
-
-
Save ayroblu/53066b5f81366a09e5089b6fd77599e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function universalLoader(req, res) { | |
const filePath = path.resolve(__dirname, '..', 'build', 'index.html') | |
fs.readFile(filePath, 'utf8', (err, htmlData)=>{ | |
if (err) { | |
console.error('err', err) | |
return res.status(404).end() | |
} | |
match({ routes, location: req.url }, (err, redirect, renderProps) => { | |
if(err) { | |
console.error('huh err', err) | |
return res.status(404).end() | |
} else if(redirect) { | |
res.redirect(302, redirect.pathname + redirect.search) | |
} else if(renderProps) { | |
let store = configureStore() | |
const ReactApp = renderToString( | |
createElement(Provider, {store}, | |
createElement(RouterContext, renderProps) | |
) | |
) | |
const RenderedApp = htmlData.replace('{{SSR}}', ReactApp) | |
res.send(RenderedApp) | |
} else { | |
return res.status(404).end() | |
} | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment