forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderonserver.ts
More file actions
218 lines (196 loc) · 5.1 KB
/
renderonserver.ts
File metadata and controls
218 lines (196 loc) · 5.1 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const javascript = {
react: [
{
fileName: 'server/routePosts.js',
code: `
import { renderToString } from 'react-dom/server'
import { createOvermindSSR } from 'overmind'
import { Provider } from 'overmind-react'
import { config } from '../client/overmind'
import App from '../client/components/App'
import db from './db'
module.exports = async (req, res) => {
const overmind = createOvermindSSR(config)
overmind.state.currentPage = 'posts'
overmind.state.posts = await db.getPosts()
const html = renderToString(
<Provider value={overmind}>
<App />
</Provider>
)
res.send(\`
<html>
<body>
<div id="app">\${html}</div>
<script>
window.__OVERMIND_MUTATIONS = \${JSON.stringify(overmind.hydrate())}
</script>
<script src="/scripts/app.js"></script>
</body>
</html>
\`)
}
`,
},
],
vue: [
{
fileName: 'server/routePosts.js',
code: `
/*
VUE will not be able to support server side rendering until version 3.0
"
Top level APIs will likely receive an overhaul to avoid globally mutating the
Vue runtime when installing plugins. Instead, plugins will be applied and scoped
to a component tree. This will make it easier to test components that rely on
specific plugins, and also make it possible to mount multiple Vue applications
on the same page with different plugins, but using the same Vue runtime
"
*/
`,
},
],
}
const typescript = {
react: [
{
fileName: 'server/routePosts.ts',
code: `
import { renderToString } from 'react-dom/server'
import { createOvermindSSR } from 'overmind'
import { Provider } from 'overmind-react'
import { config } from '../client/overmind'
import App from '../client/components/App'
import db from './db'
export default async (req, res) => {
const overmind = createOvermindSSR(config)
overmind.state.currentPage = 'posts'
overmind.state.posts = await db.getPosts()
const html = renderToString(
<Provider value={overmind}>
<App />
</Provider>
)
res.send(\`
<html>
<body>
<div id="app">\${html}</div>
<script>
window.__OVERMIND_MUTATIONS = \${JSON.stringify(overmind.hydrate())}
</script>
<script src="/scripts/app.js"></script>
</body>
</html>
\`)
}
`,
},
],
vue: javascript.vue,
angular: [
{
fileName: 'overmind/index.ts',
code: `
import { IConfig } from 'overmind'
import { OvermindService } from 'overmind-angular'
import { Injectable } from '@angular/core'
import { state } from './state'
export const config = {
state
}
declare module 'overmind' {
interface Config extends IConfig<typeof config> {}
}
@Injectable()
export class Store extends OvermindService<typeof config> {}
`,
},
{
fileName: 'common.module.ts',
code: `
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { OvermindService, OvermindModule } from 'overmind-angular'
import { AppComponent } from './components/app.component.ts'
import { Store } from './overmind'
// We create a shared module between the client and the server
@NgModule({
bootstrap: [AppComponent],
imports: [
BrowserModule.withServerTransition({appId: 'my-app'}),
OvermindModule
],
providers: [{ provide: Store, useExisting: OvermindService }]
})
export class CommonModule {}
`,
},
{
fileName: 'app.module.ts',
code: `
import { createOvermind } from 'overmind'
import { OVERMIND_INSTANCE } from 'overmind-angular'
import { CommonModule } from './common.module.ts'
import { config } from './overmind'
// The client specific module provides the Overmind instance
@NgModule({
imports: [CommonModule],
providers: [{
provide: OVERMIND_INSTANCE,
useValue: createOvermind(config)
}]
})
export class AppModule {}
`,
},
{
fileName: 'app.server.module.ts',
code: `
import { ServerModule } from '@angular/platform-server';
import { CommonModule } from './common.module.ts'
import { config } from './overmind'
// The server module does not provide an instance, it is
// provider per request to the server
@NgModule({
imports: [CommonModule, ServerModule],
})
export class AppServerModule {}
`,
},
{
fileName: 'server/routePosts.js',
code: `
import { renderModuleFactory } from '@angular/platform-server'
import { createOvermindSSR } from 'overmind'
import { config } from '../overmind'
import db from './db'
const { AppServerModuleNgFactory } = require('./server/main')
export default async (req, res) => {
const overmind = createOvermindSSR(config)
overmind.state.currentPage = 'posts'
overmind.state.posts = await db.getPosts()
const html = await renderModuleFactory(AppServerModuleNgFactory, {
extraProviders: [{
provide: OVERMIND_INSTANCE,
useValue: overmind
}]
})
res.send(\`
<!DOCTYPE html>
<html>
<head></head>
<body>
\${html}
<script>
window.__OVERMIND_MUTATIONS = \${JSON.stringify(overmind.hydrate())}
</script>
<script src="/scripts/app.js"></script>
</body>
</html>
\`)
});
`,
},
],
}
export default (ts, view) => (ts ? typescript[view] : javascript[view])