kadirahq / meteor-dochead

Isomorphic way to manipulate document.head for Meteor apps
MIT License
131 stars 17 forks source link

pass SSR context #14

Open fabien-h opened 8 years ago

fabien-h commented 8 years ago

This PR concerns this issue on FlowRouter : https://github.com/kadirahq/flow-router/issues/205

When ReactLayout.render is called asynchronousely, the ssr context is not accessible anymore. This is a double PR (I'ma making the same for DocHead). It just add the context as an optionnal parametter of the render and _addToHead functions.

So in the action hook of the route constructor in flow router, you can now do this :

action( params, queryParams ) {

    let context;
    if ( Meteor.isServer )
        context = FlowRouter.ssrContext.get();

    Meteor.call( 'getAsyncData', ( err, data ) => {

        DocHead.setTitle( data.title, context );

        DocHead.addMeta( {
            name: 'description',
            content: data.description
        }, context );

        ReactLayout.render( ReactLayouts.MainLayout, {
            content: <ReactView.MyView data={ data } />,
        }, context );

    } );

}

There's probably a smarter way, but this one works.