Skip to content

Commit 19fc594

Browse files
author
Guillaume Chau
committed
vue-ssr: JS injection
1 parent ec00093 commit 19fc594

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

packages/vue-ssr/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
## v0.2.0 - 2017-05-01
4+
5+
- You can now inject arbitrary JS to the generated HTML by passing an object with `app` and `js` attribute to `VueSSR.createApp`.
6+

packages/vue-ssr/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ meteor add akryum:vue-ssr
1212

1313
**:warning: All your client-side code should be available on the server (which means they shouldn't be in a `client` folder), and the code and libraries should be able to run on the server.**
1414

15-
*:warning: vuex & apollo are not tested yet.*
16-
1715
Wrap your Vue root instance in a `createApp` function and export it, alongside with the router instance:
1816

1917
```javascript
@@ -61,7 +59,11 @@ VueSSR.createApp = function (context) {
6159
const { app, router } = CreateApp()
6260
// Set the url in the router
6361
router.push(context.url)
64-
return app
62+
return {
63+
app,
64+
// Inject some arbitrary JS
65+
js: `console.log('hello')`,
66+
}
6567
}
6668
```
6769

@@ -84,7 +86,11 @@ VueSSR.createApp = function (context) {
8486

8587
// Can use components prefetch here...
8688

87-
resolve(app)
89+
resolve({
90+
app,
91+
// Inject some arbitrary JS
92+
js: `console.log('hello')`,
93+
})
8894
})
8995
})
9096
}

packages/vue-ssr/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
name: 'akryum:vue-ssr',
3-
version: '0.1.0',
3+
version: '0.2.0',
44
summary: 'Render Vue server-side',
55
git: 'https://github.com/Akryum/meteor-vue-component',
66
documentation: 'README.md'

packages/vue-ssr/server/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ Meteor.bindEnvironment(function () {
108108
asyncResult = Promise.resolve(result)
109109
}
110110

111-
asyncResult.then(app => {
111+
asyncResult.then(result => {
112+
113+
let app
114+
if (result.app) {
115+
app = result.app
116+
} else {
117+
app = result
118+
}
119+
112120
renderer.renderToString(
113121
app,
114122
(error, html) => {
@@ -122,7 +130,9 @@ Meteor.bindEnvironment(function () {
122130
const data = frContext.getData()
123131
InjectData.pushData(res, 'fast-render-data', data)
124132

125-
res.write = patchResWrite(res.write, VueSSR.template.replace(VueSSR.outlet, html))
133+
const script = (result.js && `<script>${result.js}</script>`) || ''
134+
135+
res.write = patchResWrite(res.write, VueSSR.template.replace(VueSSR.outlet, html) + script)
126136

127137
next()
128138
},

0 commit comments

Comments
 (0)