forked from davidsrn/Solitarde-GitPages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-static-routes.js
108 lines (87 loc) · 2.94 KB
/
react-static-routes.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import universal, { setHasBabelPlugin } from 'react-universal-component'
import { cleanPath } from 'react-static'
setHasBabelPlugin()
const universalOptions = {
loading: () => null,
error: props => {
console.error(props.error);
return <div>An error occurred loading this page's template. More information is available in the console.</div>;
},
}
const t_0 = universal(import('../src/containers/Index'), universalOptions)
const t_1 = universal(import('../src/containers/ContactUs'), universalOptions)
const t_2 = universal(import('../src/containers/Products'), universalOptions)
const t_3 = universal(import('../src/containers/Community'), universalOptions)
const t_4 = universal(import('../src/containers/AboutUs'), universalOptions)
const t_5 = universal(import('../src/containers/Events'), universalOptions)
const t_6 = universal(import('../src/containers/Event'), universalOptions)
const t_7 = universal(import('../src/containers/404'), universalOptions)
// Template Map
global.componentsByTemplateID = global.componentsByTemplateID || [
t_0,
t_1,
t_2,
t_3,
t_4,
t_5,
t_6,
t_7
]
// Template Tree
global.templateIDsByPath = global.templateIDsByPath || {
'404': 7
}
// Get template for given path
const getComponentForPath = path => {
path = cleanPath(path)
return global.componentsByTemplateID[global.templateIDsByPath[path]]
}
global.reactStaticGetComponentForPath = getComponentForPath
global.reactStaticRegisterTemplateIDForPath = (path, id) => {
global.templateIDsByPath[path] = id
}
export default class Routes extends Component {
render () {
const { component: Comp, render, children } = this.props
const getFullComponentForPath = path => {
let Comp = getComponentForPath(path)
let is404 = path === '404'
if (!Comp) {
is404 = true
Comp = getComponentForPath('404')
}
return newProps => (
Comp
? <Comp {...newProps} {...(is404 ? {is404: true} : {})} />
: null
)
}
const renderProps = {
componentsByTemplateID: global.componentsByTemplateID,
templateIDsByPath: global.templateIDsByPath,
getComponentForPath: getFullComponentForPath
}
if (Comp) {
return (
<Comp
{...renderProps}
/>
)
}
if (render || children) {
return (render || children)(renderProps)
}
// This is the default auto-routing renderer
return (
<Route path='*' render={props => {
let Comp = getFullComponentForPath(props.location.pathname)
// If Comp is used as a component here, it triggers React to re-mount the entire
// component tree underneath during reconciliation, losing all internal state.
// By unwrapping it here we keep the real, static component exposed directly to React.
return Comp && Comp({ ...props, key: props.location.pathname })
}} />
)
}
}