Created
August 10, 2017 08:26
-
-
Save faceyspacey/deb77e8086a8d811e6c539bcf144eac5 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
import createHistory from 'history/createMemoryHistory' | |
import { NOT_FOUND } from 'redux-first-router' | |
import configureStore from '../src/configureStore' | |
export default async (req, res) => { | |
const history = createHistory({ initialEntries: [req.path] }) | |
const { store, thunk } = configureStore(history) | |
let location = store.getState().location | |
if (doesRedirect(location, res)) return false | |
await thunk(store) // THE PAYOFF! | |
location = store.getState().location | |
if (doesRedirect(location, res)) return false | |
const status = location.type === NOT_FOUND ? 404 : 200 | |
res.status(status) | |
return store | |
} | |
const doesRedirect = ({ kind, pathname }, res) => { | |
if (kind === 'redirect') { | |
res.redirect(302, pathname) | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment