-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathindex.js
170 lines (137 loc) · 4.75 KB
/
index.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
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
import Vue from 'vue'
import { createRenderer } from 'vue-server-renderer'
import { WebApp } from 'meteor/webapp'
import cookieParser from 'cookie-parser'
import { onPageLoad } from 'meteor/server-render'
import { FastRender } from 'meteor/staringatlights:fast-render'
import SsrContext from './context'
import patchSubscribeData from './data'
function IsAppUrl (req) {
var url = req.url
if (url === '/favicon.ico' || url === '/robots.txt') {
return false
}
if (url === '/app.manifest') {
return false
}
// Avoid serving app HTML for declared routes such as /sockjs/.
if (RoutePolicy.classify(url)) {
return false
}
return true
}
VueSSR = {}
VueSSR.outlet = process.env.VUE_OUTLET || 'app'
VueSSR.defaultAppTemplate = `
<div id="not-the-app" style="font-family: sans-serif;">
<h1>This is not what you expected</h1>
<p>
You need to tell <code>vue-ssr</code> how to create your app by setting the <code>VueSSR.createApp</code> function. It should return a new Vue instance.
</p>
<p>
Here is an example of server-side code:
</p>
<pre style="background: #ddd; padding: 12px; border-radius: 3px; font-family: monospace;">import Vue from 'vue'
import { VueSSR } from 'meteor/akryum:vue-ssr'
function createApp () {
return new Vue({
render: h => h('div', 'Hello world'),
})
}
VueSSR.createApp = createApp</pre>
</div>
`
VueSSR.createApp = function () {
return new Vue({
template: VueSSR.defaultAppTemplate,
})
}
VueSSR.ssrContext = new Meteor.EnvironmentVariable()
VueSSR.inSubscription = new Meteor.EnvironmentVariable() // <-- needed in data.js
patchSubscribeData(VueSSR)
const renderer = createRenderer()
function writeServerError (sink, { code = 500, message = 'Server Error' } = {}) {
sink.setStatusCode(code)
sink.appendToBody(message)
}
WebApp.rawConnectHandlers.use(cookieParser())
onPageLoad(sink => new Promise((resolve, reject) => {
const req = sink.request
// Fast render
const loginToken = req.cookies['meteor_login_token']
const headers = req.headers
const frLoginContext = new FastRender._Context(loginToken, { headers })
FastRender.frContext.withValue(frLoginContext, function () {
// we're stealing all the code from FlowRouter SSR
// https://github.com/kadirahq/flow-router/blob/ssr/server/route.js#L61
const ssrContext = new SsrContext()
VueSSR.ssrContext.withValue(ssrContext, () => {
try {
// const frData = InjectData.getData(res, 'fast-render-data')
// if (frData) {
// ssrContext.addData(frData.collectionData)
// }
// Vue
const context = { url: req.url }
let asyncResult
const result = VueSSR.createApp(context)
if (result && typeof result.then === 'function') {
asyncResult = result
} else {
asyncResult = Promise.resolve(result)
}
asyncResult.then(app => {
renderer.renderToString(
app,
context,
(error, html) => {
if (error) {
console.error(error)
writeServerError(sink)
return
}
// const frContext = FastRender.frContext.get()
// const data = frContext.getData()
// // InjectData.pushData(res, 'fast-render-data', data)
// const injectData = EJSON.stringify({
// 'fast-render-data': data,
// })
// // sink.appendToHead(`<script type="text/inject-data">${encodeURIComponent(injectData)}</script>`)
let appendHtml
if (typeof context.appendHtml === 'function') appendHtml = context.appendHtml()
const head = ((appendHtml && appendHtml.head) || context.head) || ''
const body = ((appendHtml && appendHtml.body) || context.body) || ''
const js = ((appendHtml && appendHtml.js) || context.js) || ''
const statusCode = ((appendHtml && appendHtml.statusCode) || context.statusCode) || 200
const script = js && `<script type="text/javascript">${js}</script>`
sink.setStatusCode(statusCode)
sink.renderIntoElementById(VueSSR.outlet, html)
sink.appendToHead(head)
sink.appendToBody([body, script])
resolve()
},
)
}).catch(e => {
console.error(e)
writeServerError(sink, e)
resolve()
})
} catch (error) {
console.error(error)
writeServerError(sink)
resolve()
}
})
})
}))
return
/* eslint-disable */
Meteor.bindEnvironment(function () {
WebApp.rawConnectHandlers.use(cookieParser())
WebApp.connectHandlers.use((req, res, next) => {
if (!IsAppUrl(req)) {
next()
return
}
})
})()