WIP: Use react-ssr-prepass to fetchData

TODO: State does not update correctly. Need to figure out why.
This commit is contained in:
Jerko Steiner 2019-05-19 20:14:32 +12:00
parent bc960f533e
commit e63b2b1c03
4 changed files with 48 additions and 4 deletions

15
package-lock.json generated
View File

@ -10719,6 +10719,12 @@
} }
} }
}, },
"object-is": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/object-is/-/object-is-1.0.1.tgz",
"integrity": "sha1-CqYOyZiaCz7Xlc9NBvYs8a1lObY=",
"dev": true
},
"object-keys": { "object-keys": {
"version": "1.0.12", "version": "1.0.12",
"resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz",
@ -11873,6 +11879,15 @@
"tiny-warning": "^1.0.0" "tiny-warning": "^1.0.0"
} }
}, },
"react-ssr-prepass": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/react-ssr-prepass/-/react-ssr-prepass-1.0.5.tgz",
"integrity": "sha512-A8QXXIHBpzr5/bnG3rwFgJWU6gLo7/dcW1bBIluoBI2hMyC+uuMRO+rXEtUpX6AVDhWfdFVi2I7kvWUDIRgDtA==",
"dev": true,
"requires": {
"object-is": "^1.0.1"
}
},
"read": { "read": {
"version": "1.0.7", "version": "1.0.7",
"resolved": "https://registry.npmjs.org/read/-/read-1.0.7.tgz", "resolved": "https://registry.npmjs.org/read/-/read-1.0.7.tgz",

View File

@ -62,6 +62,7 @@
"react-redux": "^6.0.0", "react-redux": "^6.0.0",
"react-router-config": "^5.0.0", "react-router-config": "^5.0.0",
"react-router-dom": "^4.3.1", "react-router-dom": "^4.3.1",
"react-ssr-prepass": "^1.0.5",
"redux": "^4.0.1", "redux": "^4.0.1",
"sanitize-html": "^1.20.0", "sanitize-html": "^1.20.0",
"sourceify": "git+https://github.com/jeremija/sourceify.git#sources-content", "sourceify": "git+https://github.com/jeremija/sourceify.git#sources-content",

View File

@ -1,4 +1,5 @@
import React from 'react' import React from 'react'
import ssrPrepass from 'react-ssr-prepass'
import {Action} from 'redux' import {Action} from 'redux'
import {IAPIDef} from '@rondo/common' import {IAPIDef} from '@rondo/common'
import {IClientConfig} from './IClientConfig' import {IClientConfig} from './IClientConfig'
@ -18,17 +19,23 @@ export class ServerRenderer<A extends Action, D extends IAPIDef>
http: IHTTPClient<D> http: IHTTPClient<D>
}>, }>,
) {} ) {}
render<State>( async render<State>(
url: string, url: string,
store: Store<State>, store: Store<State>,
config: IClientConfig, config: IClientConfig,
host: string = '', host: string = '',
headers: Record<string, string> = {},
) { ) {
const {RootComponent} = this const {RootComponent} = this
const http = new HTTPClient<D>(host + config.baseUrl + '/api') // TODO set cookie in headers here...
const http = new HTTPClient<D>(
'http://' + host + config.baseUrl + '/api',
headers,
)
const context: StaticRouterContext = {} const context: StaticRouterContext = {}
const stream = renderToNodeStream(
const element = (
<Provider store={store}> <Provider store={store}>
<StaticRouter <StaticRouter
basename={config.baseUrl} basename={config.baseUrl}
@ -37,8 +44,17 @@ export class ServerRenderer<A extends Action, D extends IAPIDef>
> >
<RootComponent config={config} http={http} /> <RootComponent config={config} http={http} />
</StaticRouter> </StaticRouter>
</Provider>, </Provider>
) )
console.log('prepass')
await ssrPrepass(element, async (el, component) => {
if (component && 'fetchData' in component) {
await (component as any).fetchData()
}
})
console.log('prepass done')
const stream = renderToNodeStream(element)
return stream return stream
} }
} }

View File

@ -0,0 +1,12 @@
declare module 'react-ssr-prepass' {
import {Component, ReactElement} from 'react'
export type Visitor = (
element: ReactElement,
instance?: Component,
) => void | Promise<any>
function ssrPrepass(node: ReactElement, visitor?: Visitor): Promise<void>
export = ssrPrepass
}