File size: 2,068 Bytes
5893f8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React from 'react'
import Relay from 'react-relay'
import Helmet from 'react-helmet'
import {match} from 'react-router'
import ReactDOMServer from 'react-dom/server'
import IsomorphicRouter from 'isomorphic-relay-router'


export default (options) => {
    
    // Validate options
    if (!options.routes) throw new Error('Missing options.routes in renderServer')
    else if (!options.render) throw new Error('Missing render function in renderServer')
    else if (!options.networkLayer && !options.graphqlUrl) throw new Error('You need to pass a GraphQL URL or a NetworkLayer, check docs')
    else if (options.networkLayer && options.graphqlUrl) throw new Error('You cannot set both GraphQL URL and a NetworkLayer, please set just one of them')

    // Setup NetworkLayer
    let networkLayer = ''
    if (options.graphqlUrl){
        networkLayer = new Relay.DefaultNetworkLayer(options.graphqlUrl)
    }else{
        networkLayer = options.networkLayer
    }

    // Return middleware
    return async (ctx, next) => {
        const {redirectLocation, renderProps} = await new Promise((resolve, reject) => {
            match({
                routes: options.routes,
                location: ctx.req.url
            }, (err, redirectLocation, renderProps) => {
                if (err) {
                    return reject(err)
                }
                return resolve({redirectLocation, renderProps})
            })
        })

        if (redirectLocation) {
            ctx.redirect(redirectLocation.pathname + redirectLocation.search)
        } else if (renderProps) {
            const {data, props} = await IsomorphicRouter.prepareData(renderProps, networkLayer)
            const reactOutput = ReactDOMServer.renderToString(IsomorphicRouter.render(props))
            const preloadedData = JSON.stringify(data)
            const helmet = Helmet.rewind()

            ctx.status = 200
            ctx.body = await options.render(reactOutput, preloadedData, helmet)
        } else {
            ctx.throw(404, 'Not found')
        }
    }
}