-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathplugin.js
147 lines (137 loc) · 5.26 KB
/
plugin.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { tide, Mapping, TideSearchApi } from '@dpc-sdp/ripple-nuxt-tide/lib/core'
import { serverSetProperties } from '@dpc-sdp/ripple-nuxt-tide/modules/authenticated-content/lib/authenticate'
export default ({ app, req, store , route }, inject) => {
// We need to serialize functions, so use `serialize` instead of `JSON.stringify`.
// https://github.com/nuxt-community/modules/issues/170
// https://www.npmjs.com/package/serialize-javascript
const options = <%= serialize(options) %>
const config = options
// Inject `tide` key
// -> app.$tide
// -> this.$tide in vue components
// -> this.$tide in store actions/mutations
const tideService = tide(app.$axios, options.site, config)
inject('tide', tideService)
const getBaseUrl = () => {
const apiRoot = options.searchApi ? `/${options.searchApi.apiBase}/${options.searchApi.apiVersion}` : '/search-api/v2/'
if (process.env.SEARCH_API_BASE_URL) {
return process.env.SEARCH_API_BASE_URL + apiRoot
}
if (req) {
return req.protocol + '://' + req.headers.host + apiRoot
} else if (typeof window !== 'undefined') {
return window.location.origin + apiRoot
}
}
if (
options.modules?.landingPage?.contentCollection !== false &&
options.modules?.landingPage?.dataList !== false
) {
inject('tideSearchApi', new TideSearchApi({
client: app.$axios,
baseUrl: getBaseUrl()
}))
}
inject('tideMapping', new Mapping(config, tideService))
// Register Tide Vuex module
if (store) {
const storeModule = {
namespaced: true,
state: () => ({
host: null,
protocol: null,
currentUrl: null,
currentDomain: null,
siteData: null,
pageData: null,
pageHead: null
}),
mutations: {
setHost (state, host) {
state.host = host
},
setProtocol (state, protocol) {
state.protocol = protocol
},
setCurrentUrl (state, currentUrl) {
state.currentUrl = currentUrl
},
setCurrentDomain (state, currentDomain) {
state.currentDomain = currentDomain
},
setSiteData (state, siteData) {
state.siteData = siteData
},
setPageData (state, pageData) {
state.pageData = pageData
},
setPageHead (state, pageHead) {
state.pageHead = pageHead
}
},
actions: {
async init ({ commit, dispatch }, { requestId = null } = {}) {
if (process.server) {
await dispatch('setSiteData', { requestId })
commit('setHost', req.headers.host)
// Set protocol
const protocol = process.server ? (req.socket.encrypted ? 'https:' : 'http:') : window.location.protocol
commit('setProtocol', protocol)
// Load site module store.
if (config.modules.site === 1) {
await store.dispatch('tideSite/init', { requestId })
}
// Load authenticated content store.
if (config.modules.authenticatedContent === 1) {
serverSetProperties(req.headers.cookie, route.path, store)
}
}
},
async setSiteData ({ commit }, { requestId = null } = {}) {
const headersConfig = { requestId }
let siteData = await app.$tide.getSiteData(headersConfig)
if (siteData instanceof Error) {
// If got a Nuxt auth session expire error, try again.
// As Nuxt auth should reset auth on error, retry should pass.
if (siteData.name === 'ExpiredAuthSessionError') {
siteData = await app.$tide.getSiteData(headersConfig)
if (siteData instanceof Error) {
throw siteData
}
} else {
throw siteData
}
}
siteData.lastFetched = Date.now()
commit('setSiteData', siteData)
if (siteData.field_site_domains) {
// Current site domain should be the first one in site domains
const siteDomain = siteData.field_site_domains.valueOf().split('\r\n', 1)
commit('setCurrentDomain', siteDomain[0])
}
if (config.modules.alert === 1) {
if (siteData.site_alerts && siteData.site_alerts.length > 0) {
await store.dispatch('tideAlerts/setAlerts', { alerts: siteData.site_alerts, siteSection: siteData.drupal_internal__tid })
}
}
},
setCurrentUrl ({ commit }, fullPath) {
const url = store.state.tide.protocol + '//' + store.state.tide.host + fullPath
commit('setCurrentUrl', url)
},
setPageData ({ commit }, pageData) {
commit('setPageData', pageData)
},
setPageHead ({ commit }, head) {
commit('setPageHead', head)
}
}
}
// In SSR, Vuex register store module on both server side and client side.
// That causes the module store states will be reset in client side.
// Use `{ preserveState: true }` to skip reset states when register module.
// https://github.com/vuejs/vuex/issues/1130
// https://vuex.vuejs.org/guide/modules.html#dynamic-module-registration
store.registerModule('tide', storeModule, { preserveState: !!store.state.tide })
}
}