Created
January 8, 2018 18:02
-
-
Save odinuge/3c9778e991621eb579d0d8ea5676365c to your computer and use it in GitHub Desktop.
Simple proof of concept how to do SSR with raven-for-redux in SSR
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
// configureStore.js | |
export default function configureStore(initialState: State, Raven: any): Store { | |
const middlewares = [ | |
// Other middlewares | |
createRavenMiddleware(Raven, ravenMiddlewareOptions) | |
]; | |
// configure store | |
return createStore(/**/); | |
} | |
// universalRaven.js | |
export interface UniversalRaven { | |
captureException(ex: Error, options?: RavenOptions): *; | |
setDataCallback(data: any, orig?: any): *; | |
captureException(ex: Error, options?: RavenOptions): *; | |
} | |
// Mimic raven-js API | |
export class UniversalRavenNode implements UniversalRaven { | |
raven: any; | |
dataCallback: any => any = state => state; | |
constructor(raven: any) { | |
this.raven = raven; | |
} | |
// This is neccecary to get proper timing | |
captureBreadcrumb = (data: any) => this.raven.captureBreadcrumb(data); | |
setDataCallback = (callback: any) => (this.dataCallback = callback); | |
captureException = (error: Error, extraData: any = {}) => { | |
const data = this.dataCallback({ | |
extra: {}, | |
...extraData | |
}); | |
return this.raven.captureException(error, data); | |
}; | |
} | |
// server/render.js | |
function render(req: $Request, res: $Response, next: Middleware) { | |
match({ routes, location: req.url }, (err, redirect, renderProps) => { | |
// This will set the user context for the given request | |
Raven.context(function() { | |
// init react & redux stuff | |
const raven = new UniversalRavenNode(Raven); | |
const store = configureStore({}, raven); | |
const respond = (error: Error) => { | |
if (error) { | |
raven.captureException(error); | |
} | |
// Render and return the | |
}; | |
// Run all SSR requests | |
prepareWithTimeout(app).then(respond, respond); | |
}); | |
}); | |
} | |
// client/index.js | |
import Raven from 'raven-js'; | |
const store = configureStore(preloadedState, Raven); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment