Skip to content

Commit 76e41f4

Browse files
committed
SSR white paper
1 parent 8736aa8 commit 76e41f4

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

ssr.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// app.js
2+
3+
import VueApollo from 'vue-apollo'
4+
5+
Vue.use(VueApollo)
6+
7+
export default function createApp ({ createApolloClients }) {
8+
const router = new VueRouter({})
9+
const store = new Vuex.Store({})
10+
const { apolloClientA, apolloClientB } = createApolloClients()
11+
const apollo = new VueApollo({
12+
clients: {
13+
a: apolloClientA,
14+
b: apolloClientB,
15+
},
16+
defaultClient: apolloClientA,
17+
})
18+
19+
const ensureReady = apollo.collect()
20+
21+
const app = new Vue({
22+
el: '#app',
23+
router,
24+
store,
25+
apollo,
26+
...App,
27+
})
28+
29+
return { app, router, store, apollo, ensureReady }
30+
}
31+
32+
// server.js
33+
34+
import { ApolloClient, createNetworkInterface } from 'apollo-client'
35+
import { createLocalInterface } from 'apollo-local-query'
36+
import graphql from 'graphql'
37+
import { schema } from './graphql/schema'
38+
39+
function createApolloClients() {
40+
const apolloClientA = new ApolloClient({
41+
ssrMode: true,
42+
networkInterface: createLocalInterface(graphql, schema)
43+
})
44+
45+
const apolloClientB = new ApolloClient({
46+
ssrMode: true,
47+
networkInterface: createNetworkInterface({
48+
uri: 'http://my-awesome-api/graphql',
49+
transportBatching: true,
50+
}),
51+
})
52+
53+
return {
54+
apolloClientA,
55+
apolloClientB,
56+
}
57+
}
58+
59+
export default function render (context) {
60+
const { app, router, store, apollo, ensureReady } = createApp({ createApolloClients })
61+
router.push(context.url)
62+
router.onReady(async () => {
63+
// Wait for apollo queries to be loaded
64+
await ensureReady()
65+
66+
// Inject initial apollo states
67+
let js = `window.__APOLLO_STATE__ = {`
68+
for (const key in apollo.clients) {
69+
const client = apollo.clients[key]
70+
const state = {[client.reduxRootKey]: client.getInitialState() }
71+
js += `['${key}']:${JSON.stringify(state)},`
72+
}
73+
js += `};`
74+
75+
// TODO Render here
76+
77+
// TODO Add the js to the response
78+
})
79+
}
80+
81+
// client.js
82+
83+
import { ApolloClient, createNetworkInterface } from 'apollo-client'
84+
85+
function createApolloClients() {
86+
const state = window.__APOLLO_STATE__
87+
88+
const apolloClientA = new ApolloClient({
89+
networkInterface: createNetworkInterface({
90+
uri: '/graphql',
91+
transportBatching: true,
92+
}),
93+
initialState: state.a,
94+
ssrForceFetchDelay: 100,
95+
})
96+
97+
const apolloClientB = new ApolloClient({
98+
networkInterface: createNetworkInterface({
99+
uri: 'http://my-awesome-api/graphql',
100+
transportBatching: true,
101+
}),
102+
initialState: state.b,
103+
ssrForceFetchDelay: 100,
104+
})
105+
106+
export {
107+
apolloClientA,
108+
apolloClientB,
109+
}
110+
}
111+
112+
createApp({ createApolloClients })

0 commit comments

Comments
 (0)