-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserver-entry.js
51 lines (43 loc) · 1.66 KB
/
server-entry.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
import { app, router, store } from './app'
const isDev = process.env.NODE_ENV !== 'production'
// This exported function will be called by `bundleRenderer`.
// This is where we perform data-prefetching to determine the
// state of our application before actually rendering it.
// Since data fetching is async, this function is expected to
// return a Promise that resolves to the app instance.
export default context => {
// set router's location
router.push(context.url)
const matchedComponents = router.getMatchedComponents()
// no matched routes
if (!matchedComponents.length) {
return Promise.reject({ code: '404' })
}
// Call preFetch hooks on components matched by the route.
// A preFetch hook dispatches a store action and returns a Promise,
// which is resolved when the action is complete and store state has been
// updated.
return Promise.all(matchedComponents.map(component => {
if (component.preFetch) {
return component.preFetch(store)
}
})).then(res => {
// After all preFetch hooks are resolved, our store is now
// filled with the state needed to render the app.
// Expose the state on the render context, and let the request handler
// inline the state in the HTML response. This allows the client-side
// store to pick-up the server-side state without having to duplicate
// the initial data fetching on the client.
context.initialState = store.state
const page = res.shift()
if (page && page.h1) {
app.title = page.h1
}
if (page) {
context.title = page.title
context.description = page.description
context.keywords = page.keywords
}
return app
})
}