-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
175 lines (148 loc) · 4.88 KB
/
server.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
// @ts-check
const fs = require('fs')
const path = require('path')
const express = require('express')
const axios = require('axios')
const { SitemapStream, streamToPromise } = require('sitemap')
const { createGzip } = require('zlib')
// const { Readable } = require('stream')
const dotenv = require('dotenv')
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const IS_PROD = process.env.NODE_ENV === 'production'
if (IS_PROD) {
dotenv.config({ path: '.env.production' })
} else {
dotenv.config({ path: '.env' })
}
const API_BASE_URL = process.env.VITE_API_BASE_URL || 'https://api.improvecn.com/api'
async function createServer (
root = process.cwd(),
isProd = IS_PROD,
) {
const resolve = (p) => path.resolve(__dirname, p)
const indexProd = isProd
? fs.readFileSync(resolve('dist/client/index.html'), 'utf-8')
: ''
// @ts-ignore
const manifest = isProd ? require('./dist/client/ssr-manifest.json') : {}
// const manifest = require('./dist/client/ssr-manifest.json')
const app = express()
/**
* @type {import('vite').ViteDevServer}
*/
let vite
if (!isProd) {
vite = await require('vite').createServer({
root,
logLevel: isTest ? 'error' : 'info',
server: {
middlewareMode: 'ssr',
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100,
},
},
})
// use vite's connect instance as middleware
app.use(vite.middlewares)
} else {
app.use(require('compression')())
app.use(
require('serve-static')(resolve('dist/client'), {
index: false,
}),
)
}
// sitemap
let sitemap
app.get('/sitemap.xml', async function (req, res) {
res.header('Content-Type', 'application/xml')
res.header('Content-Encoding', 'gzip')
// if we have a cached entry send it
// TODO: 缓存?
// if (sitemap) {
// res.send(sitemap)
// return
// }
try {
const smStream = new SitemapStream({ hostname: 'https://improvecn.com/' })
const pipeline = smStream.pipe(createGzip())
// pipe your entries or directly write them.
smStream.write({ url: '/', changefreq: 'daily', priority: 1.0 })
smStream.write({ url: '/about-us/', changefreq: 'daily', priority: 1.0 })
smStream.write({ url: '/products/', changefreq: 'daily', priority: 1.0 })
smStream.write({ url: '/articles/', changefreq: 'daily', priority: 1.0 })
smStream.write({ url: '/contact/', changefreq: 'daily', priority: 1.0 })
// todo: dynamic routes
// const { data: routes } = await axios.get(`${API_BASE_URL}/dynamic-routes`)
// routes.forEach((route) => {
// smStream.write({ url: route, changefreq: 'daily', priority: 1.0 })
// })
/* or use
Readable.from([{url: '/page-1'}...]).pipe(smStream)
if you are looking to avoid writing your own loop.
*/
// cache the response
streamToPromise(pipeline).then(sm => {
sitemap = sm
})
// make sure to attach a write stream such as streamToPromise before ending
smStream.end()
// stream write the response
pipeline.pipe(res).on('error', (e) => { throw e })
} catch (e) {
console.error(e)
res.status(500).end()
}
})
app.use('*', async (req, res) => {
try {
const url = req.originalUrl
let template, render
if (!isProd) {
// always read fresh template in dev
template = fs.readFileSync(resolve('index.html'), 'utf-8')
template = await vite.transformIndexHtml(url, template)
render = (await vite.ssrLoadModule('/src/entry-server.js')).render
} else {
template = indexProd
render = require('./dist/server/entry-server.js').render
}
const [
appHtml,
preloadLinks,
headTags,
htmlAttrs,
bodyAttrs,
] = await render(url, manifest)
// TODO: initialState
let initialState = {}
initialState = '<script>window.__INITIAL_STATE__=' + JSON.stringify(initialState) + '</script>'
const html = template
.replace('{{ HTML_ATTRS }}', htmlAttrs)
.replace('{{ HEAD }}', headTags)
.replace('{{ BODY_ATTRS }}', bodyAttrs)
.replace('{{ INITIAL_STATE }}', initialState)
.replace('<!--preload-links-->', preloadLinks)
.replace('<!--app-html-->', appHtml)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
vite && vite.ssrFixStacktrace(e)
console.log(e.stack)
res.status(500).end(e.stack)
}
})
return { app, vite }
}
if (!isTest) {
const port = 3000
createServer().then(({ app }) =>
app.listen(port, () => {
console.log(`http://localhost:${port}`)
}),
)
}
// for test use
exports.createServer = createServer