/
server-entry.js
203 lines (169 loc) · 6.28 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* THIS FILE IS GENERATED AUTOMATICALLY.
* DO NOT EDIT.
*
* You are probably looking on adding startup/initialization code.
* Use "quasar new boot <name>" and add it there.
* One boot file per concern. Then reference the file(s) in quasar.config.js > boot:
* boot: ['file', ...] // do not add ".js" extension to it.
*
* Boot files are your "main.js"
**/
import { createApp<%= store && ssr.manualStoreSsrContextInjection !== true ? ', unref' : '' %> } from 'vue'
<% extras.length > 0 && extras.filter(asset => asset).forEach(asset => { %>
import '@quasar/extras/<%= asset %>/<%= asset %>.css'
<% }) %>
<% animations.length > 0 && animations.filter(asset => asset).forEach(asset => { %>
import '@quasar/extras/animate/<%= asset %>.css'
<% }) %>
// We load Quasar stylesheet file
import 'quasar/dist/quasar.<%= __css.quasarSrcExt %>'
<% if (framework.cssAddon) { %>
// We add Quasar addons, if they were requested
import 'quasar/src/css/flex-addon.sass'
<% } %>
<% css.length > 0 && css.filter(asset => asset.server !== false).forEach(asset => { %>
import '<%= asset.path %>'
<% }) %>
import createQuasarApp from './app.js'
import quasarUserOptions from './quasar-user-options.js'
<% if (preFetch) { %>
import App from 'app/<%= sourceFiles.rootComponent %>'
const appPrefetch = typeof App.preFetch === 'function'
? App.preFetch
: (
// Class components return the component options (and the preFetch hook) inside __c property
App.__c !== void 0 && typeof App.__c.preFetch === 'function'
? App.__c.preFetch
: false
)
<% } %>
<%
const bootNames = []
if (boot.length > 0) {
function hash (str) {
const name = str.replace(/\W+/g, '')
return name.charAt(0).toUpperCase() + name.slice(1)
}
boot.filter(asset => asset.server !== false).forEach(asset => {
let importName = 'qboot_' + hash(asset.path)
bootNames.push(importName)
%>
import <%= importName %> from '<%= asset.path %>'
<% }) } %>
const publicPath = `<%= build.publicPath %>`
<% if (build.publicPath !== '/') { %>
const doubleSlashRE = /\/\//
const addPublicPath = url => (publicPath + url).replace(doubleSlashRE, '/')
<% } %>
const bootFiles = [<%= bootNames.join(',') %>].filter(boot => typeof boot === 'function')
const httpRE = /^https?:\/\//
function getRedirectUrl (url, router) {
if (typeof url === 'string' && httpRE.test(url) === true) {
return url
}
try { return <%= build.publicPath === '/' ? 'router.resolve(url).href' : 'addPublicPath(router.resolve(url).href)' %> }
catch (err) {}
return url
}
const { components, directives, ...qUserOptions } = quasarUserOptions
// 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 ssrContext => {
return new Promise(async (resolve, reject) => {
const {
app, router<%= store ? ', store' + (__storePackage === 'vuex' ? ', storeKey' : '') : '' %>
} = await createQuasarApp(createApp, qUserOptions, ssrContext)
<% if (bootNames.length > 0) { %>
let hasRedirected = false
const redirect = (url, httpStatusCode) => {
hasRedirected = true
reject({ url: getRedirectUrl(url, router), code: httpStatusCode })
}
for (let i = 0; hasRedirected === false && i < bootFiles.length; i++) {
try {
await bootFiles[i]({
app,
router,
<%= store ? 'store,' : '' %>
ssrContext,
redirect,
urlPath: ssrContext.req.url,
publicPath
})
}
catch (err) {
reject(err)
return
}
}
if (hasRedirected === true) {
return
}
<% } %>
app.use(router)
<% if (store && __storePackage === 'vuex') { %>app.use(store, storeKey)<% } %>
const url = ssrContext.req.url<% if (build.publicPath !== '/') { %>.replace(publicPath, '/')<% } %>
const { fullPath } = router.resolve(url)
if (fullPath !== url) {
return reject({ url: <%= build.publicPath === '/' ? 'fullPath' : 'addPublicPath(fullPath)' %> })
}
// set router's location
router.push(url).catch(() => {})
// wait until router has resolved possible async hooks
router.isReady().then(() => {
let matchedComponents = router.currentRoute.value.matched
.filter(record => record.components !== void 0)
.flatMap(record => Object.values(record.components))
// no matched routes
if (matchedComponents.length === 0) {
return reject({ code: 404 })
}
<% if (preFetch) { %>
let hasRedirected = false
const redirect = (url, httpStatusCode) => {
hasRedirected = true
reject({ url: getRedirectUrl(url, router), code: httpStatusCode })
}
// filter and convert all components to their preFetch methods
matchedComponents = matchedComponents
.filter(m => (
typeof m.preFetch === 'function'
// Class components return the component options (and the preFetch hook) inside __c property
|| (m.__c !== void 0 && typeof m.__c.preFetch === 'function')
))
.map(m => m.__c !== void 0 ? m.__c.preFetch : m.preFetch)
if (appPrefetch !== false) {
matchedComponents.unshift(appPrefetch)
}
// 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.
matchedComponents
.reduce(
(promise, preFetchFn) => promise.then(() => hasRedirected === false && preFetchFn({
<% if (store) { %>store,<% } %>
ssrContext,
currentRoute: router.currentRoute.value,
redirect,
urlPath: ssrContext.req.url,
publicPath
})),
Promise.resolve()
)
.then(() => {
if (hasRedirected === true) { return }
<% if (store && ssr.manualStoreSsrContextInjection !== true) { %>ssrContext.state = unref(store.state)<% } %>
resolve(app)
})
.catch(reject)
<% } else { %>
<% if (store && ssr.manualStoreSsrContextInjection !== true) { %>ssrContext.state = unref(store.state)<% } %>
resolve(app)
<% } %>
}).catch(reject)
})
}