Skip to content

Commit 078e632

Browse files
author
Guillaume Chau
committedMay 10, 2019
feat(ssr): support Vue 2.6 new SSR system
1 parent 4135c25 commit 078e632

File tree

3 files changed

+45
-47
lines changed

3 files changed

+45
-47
lines changed
 

‎packages/vue-ssr/README.md

+31-27
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ VueSSR.createApp = function (context) {
5959
const { app, router } = CreateApp()
6060
// Set the url in the router
6161
router.push(context.url)
62-
return {
63-
app,
62+
63+
// Called when Vue app has finished rendering
64+
context.rendered = () => {
6465
// Inject some arbitrary JS
65-
js: `console.log('hello')`,
66+
context.js = `console.log('hello')`
6667
}
68+
69+
return app
6770
}
6871
```
6972

@@ -85,12 +88,14 @@ VueSSR.createApp = function (context) {
8588
}
8689

8790
// Can use components prefetch here...
88-
89-
resolve({
90-
app,
91+
92+
// Called when Vue app has finished rendering
93+
context.rendered = () => {
9194
// Inject some arbitrary JS
92-
js: `console.log('hello')`,
93-
})
95+
context.js = `console.log('hello')`
96+
}
97+
98+
resolve(app)
9499
})
95100
})
96101
}
@@ -124,26 +129,25 @@ VueSSR.createApp = function (context) {
124129

125130
// ...
126131

127-
resolve({
128-
app,
129-
appendHtml() {
130-
const {
131-
title, link, style, script, noscript, meta
132-
} = context.meta.inject()
133-
134-
return {
135-
head: `
136-
${meta.text()}
137-
${title.text()}
138-
${link.text()}
139-
${style.text()}
140-
${script.text()}
141-
${noscript.text()}
142-
`,
143-
body: script.text({ body: true })
144-
}
132+
context.appendHtml = () => {
133+
const {
134+
title, link, style, script, noscript, meta
135+
} = context.meta.inject()
136+
137+
return {
138+
head: `
139+
${meta.text()}
140+
${title.text()}
141+
${link.text()}
142+
${style.text()}
143+
${script.text()}
144+
${noscript.text()}
145+
`,
146+
body: script.text({ body: true })
145147
}
146-
})
148+
}
149+
150+
resolve(app)
147151
})
148152
}
149153
```

‎packages/vue-ssr/package.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: 'akryum:vue-ssr',
3-
version: '0.3.3',
3+
version: '0.4.0',
44
summary: 'Render Vue server-side',
55
git: 'https://github.com/Akryum/meteor-vue-component',
66
documentation: 'README.md',
@@ -19,7 +19,7 @@ Package.onUse(function (api) {
1919
'routepolicy',
2020
'url',
2121
'akryum:npm-check@0.1.1',
22-
'staringatlights:fast-render@3.0.3',
22+
'staringatlights:fast-render@3.2.0',
2323
'ejson',
2424
'server-render',
2525
])
@@ -28,7 +28,6 @@ Package.onUse(function (api) {
2828
})
2929

3030
Npm.depends({
31-
'vue-server-renderer': '2.5.18',
32-
'vue-ssr-html-stream': '2.2.0',
33-
'cookie-parser': '1.4.3',
31+
'vue-server-renderer': '2.6.10',
32+
'cookie-parser': '1.4.4',
3433
})

‎packages/vue-ssr/server/index.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue'
2-
import VueServerRenderer from 'vue-server-renderer/build.js'
2+
import { createRenderer } from 'vue-server-renderer'
33
import { WebApp } from 'meteor/webapp'
44
import cookieParser from 'cookie-parser'
55
import { onPageLoad } from 'meteor/server-render'
@@ -62,7 +62,7 @@ VueSSR.inSubscription = new Meteor.EnvironmentVariable() // <-- needed in data.j
6262

6363
patchSubscribeData(VueSSR)
6464

65-
const renderer = VueServerRenderer.createRenderer()
65+
const renderer = createRenderer()
6666

6767
function writeServerError (sink) {
6868
sink.appendToBody('Server Error')
@@ -91,24 +91,19 @@ onPageLoad(sink => new Promise((resolve, reject) => {
9191
// }
9292

9393
// Vue
94+
const context = { url: req.url }
9495
let asyncResult
95-
const result = VueSSR.createApp({ url: req.url })
96+
const result = VueSSR.createApp(context)
9697
if (result && typeof result.then === 'function') {
9798
asyncResult = result
9899
} else {
99100
asyncResult = Promise.resolve(result)
100101
}
101102

102-
asyncResult.then(result => {
103-
let app
104-
if (result.app) {
105-
app = result.app
106-
} else {
107-
app = result
108-
}
109-
103+
asyncResult.then(app => {
110104
renderer.renderToString(
111105
app,
106+
context,
112107
(error, html) => {
113108
if (error) {
114109
console.error(error)
@@ -125,11 +120,11 @@ onPageLoad(sink => new Promise((resolve, reject) => {
125120
// // sink.appendToHead(`<script type="text/inject-data">${encodeURIComponent(injectData)}</script>`)
126121

127122
let appendHtml
128-
if (typeof result.appendHtml === "function") appendHtml = result.appendHtml()
123+
if (typeof context.appendHtml === "function") appendHtml = context.appendHtml()
129124

130-
const head = ((appendHtml && appendHtml.head) || result.head) || ''
131-
const body = ((appendHtml && appendHtml.body) || result.body) || ''
132-
const js = ((appendHtml && appendHtml.js) || result.js) || ''
125+
const head = ((appendHtml && appendHtml.head) || context.head) || ''
126+
const body = ((appendHtml && appendHtml.body) || context.body) || ''
127+
const js = ((appendHtml && appendHtml.js) || context.js) || ''
133128

134129
const script = js && `<script type="text/javascript">${js}</script>`
135130

0 commit comments

Comments
 (0)
Please sign in to comment.