-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntime.js
56 lines (45 loc) · 1.15 KB
/
runtime.js
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
55
56
const React = require('react')
const ReactDOM = require('react-dom')
const isClient = typeof document !== 'undefined'
class ClientOnly extends React.Component {
constructor () {
super()
this.state = {
isClient: false
}
}
componentDidMount () {
this.setState({
// Need to wait until component is mounted, otherwise React will complain
// about output not being equal to server side output
isClient: isClient
})
}
render () {
const { isClient } = this.state
const { children } = this.props
if (isClient) {
return children
} else {
return null
}
}
}
const connect = (routes) => {
if (isClient) {
// We're on the client
let route = document.location.pathname.match(/^\/(.*?)(?:\.html)?$/)[1]
if (route === 'index' || route === '') {
route = 'default'
}
const Page = routes[route]
const Layout = Page.layoutProps.layout
let page = React.createElement(Page)
if (Layout) {
page = React.createElement(Layout, Page.layoutProps, page)
}
ReactDOM.render(page, document)
}
}
connect.ClientOnly = ClientOnly
module.exports = connect