score:0

Here's how I get the request state (typescript). However, I'm not sure how to then get the req.cookies out of the store from my actions, which are needed for the store to be fully populated. Perhaps I'm doing something wrong here.

app.get('*', (req, res) => {
    const location = req.url;
    const memoryHistory = createMemoryHistory(req.originalUrl);
    const store = configureStore(memoryHistory);
    const history = syncHistoryWithStore(memoryHistory, store);

    match({history, routes, location},
        (error, redirectLocation, renderProps) => {
            if (error) {
                res.status(500).send(error.message);
            } else if (redirectLocation) {
                res.redirect(302, redirectLocation.pathname + redirectLocation.search);
            } else if (renderProps) {
                const asyncRenderData = {...renderProps, store, cookies: req.cookies};
                loadOnServer(asyncRenderData).then(() => {
                    const css = [];
                    const markup = ReactDOMServer.renderToString(
                        <WithStylesContext onInsertCss={(styles) => css.push(styles._getCss())}>
                            <Provider store={store} key="provider">
                                <ReduxAsyncConnect {...renderProps} />
                            </Provider>
                        </WithStylesContext>,
                    );
                    try {
                        res.status(200).send(renderHTML(markup, store, css));
                    } catch (err) {
                        res.status(500).send('<pre>' + err + '</pre>');
                    }
                }).catch((err) => {
                    console.log('sending 404', err);
                    res.status(404).send(x404(JSON.stringify(err || {})));
                });
            } else {
                // https://stackoverflow.com/questions/4483849/default-redirect-for-error-404
                res.status(404).send(x404(null));
            }
        });
});

Related Query

More Query from same tag