-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
serverEntry.js
45 lines (38 loc) · 1.24 KB
/
serverEntry.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
import metadata from '@dynamic/metadata'
import originalServerEntry from '@vuepress/core/lib/client/serverEntry'
import { sync } from 'vuex-router-sync'
export default context => new Promise((resolve, reject) => {
originalServerEntry(context).then(app => {
const router = app.$options.router
const store = app.$options.store
sync(store, router)
router.onReady(() => {
const matchedComponents = router.getMatchedComponents()
if (!matchedComponents.length) {
return reject({ code: 404 })
}
const apiRoutePattern = /^\/api\//
const currentRoutePath = router.currentRoute.path
if (!apiRoutePattern.test(currentRoutePath)) {
return resolve(app)
}
const metadataKey = app.$page.metadataKey
if (!metadataKey) {
// Skip pages with no associated API metadata
return resolve(app)
}
const version = app.$page.version || 'next'
const versionedMetadataKey = `${version}/${metadataKey}`
store.replaceState({
metadata: {
metadata: {
[versionedMetadataKey]: metadata[version][metadataKey]
},
requests: {}
}
})
context.state = store.state
resolve(app)
})
}, reject)
})