Created
January 28, 2018 19:50
-
-
Save odinuge/c38d3656ed52aef2cf3f7a049ad27dab to your computer and use it in GitHub Desktop.
server-side-rendering-express.jsx
This file contains hidden or 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
export default function configureStore( | |
initialState: State, | |
Raven: UniversalRaven | |
): Store { | |
const messageMiddleware = createMessageMiddleware( | |
message => addToast({ message }), | |
Raven | |
); | |
const middlewares = [ | |
[...], | |
createRavenMiddleware(Raven, ravenMiddlewareOptions), | |
]; | |
return createStore(....); | |
} |
This file contains hidden or 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
//@flow | |
import express from 'express'; | |
import Raven from 'raven'; | |
import { bindToCurrentContext } from './universalRaven'; | |
const app = express(); | |
app.use(Raven.requestHandler()); // This will add a custom context to each request: https://github.com/getsentry/raven-node/blob/master/lib/client.js#L533 | |
// Other middleware here | |
app.use(function (req: $Request, res: $Response, next: Middleware) { | |
const raven = bindToCurrentContext(Raven); | |
// Do all your server-side rendering here, with the 'raven' object as your raven instance | |
// Everything done with this object is connected to the the current request | |
// This object should also be sent to the `createRavenMiddleawre` | |
// Example: | |
// raven.captureException(err); | |
// raven.captureBreadcrumb(.....); | |
const store = configureStore({}, raven); | |
const app = ( | |
<Provider store={store}> | |
<RouterContext {...renderProps} createElement={createElement} /> | |
</Provider> | |
); | |
res.send(.....); | |
} |
This file contains hidden or 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
//@flow | |
import type { raven } from 'raven'; | |
export interface UniversalRaven { | |
captureException(ex: Error, options?: RavenOptions): *; | |
setDataCallback(data: any, orig?: any): *; | |
captureException(ex: Error, options?: RavenOptions): *; | |
} | |
/* | |
* This function returns a UniversalRaven instance that mimics a raven | |
* api. It binds to the active context using the implicit binding in | |
* arrow functions. | |
*/ | |
export const bindToCurrentContext = (Raven: raven) => | |
new UniversalRavenNode(Raven); | |
// Mimic raven-js API using raven node | |
export class UniversalRavenNode implements UniversalRaven { | |
Raven: raven; | |
dataCallback: any => any = state => state; | |
constructor(Raven: any) { | |
this.Raven = Raven; | |
} | |
captureBreadcrumb = (data: any) => this.Raven.captureBreadcrumb(data); | |
// Maybe add support for nested callbacks? | |
setDataCallback = (callback: any) => (this.dataCallback = callback); | |
captureException = (error: Error, extraData: any = {}) => { | |
const data = this.dataCallback({ | |
extra: {}, | |
...extraData | |
}); | |
return this.Raven.captureException(error, data); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment