forked from styled-components/styled-components-website
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
143 lines (113 loc) · 3.5 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
const dev = process.env.NODE_ENV !== 'production'
const moduleAlias = require('module-alias')
const path = require('path')
if (!dev) {
moduleAlias.addAlias('react', 'preact-compat')
moduleAlias.addAlias('react-dom', 'preact-compat')
}
const { parse } = require('url')
const express = require('express')
const LRUCache = require('lru-cache')
const next = require('next')
const axios = require('axios')
const app = next({ dir: '.', dev })
const handle = app.getRequestHandler()
const ssrCache = new LRUCache({
max: 100,
maxAge: 1000 * 60 * 60 * 24 // 24h
})
const cachedRender = (req, res, pagePath, queryParams) => {
const key = `${req.url}`
if (!dev && ssrCache.has(key)) {
res.append('X-Cache', 'HIT')
res.send(ssrCache.get(key))
return
}
app.renderToHTML(req, res, pagePath, queryParams)
.then(html => {
ssrCache.set(key, html)
res.append('X-Cache', 'MISS')
res.send(html)
})
.catch((err) => {
app.renderError(err, req, res, pagePath, queryParams)
})
}
const cachedProxyServer = (req, res, imgUrl, remoteUrl) => {
const key = `/proxy/${imgUrl}`
if (!dev && ssrCache.has(key)) {
const cached = ssrCache.get(key)
res.append('X-Cache', 'HIT')
res.type(cached.contentType)
res.end(cached.data)
return
}
axios.get(remoteUrl, {
responseType: 'arraybuffer'
}).then(({ data, headers }) => {
const contentType = headers['content-type']
// Save to cache for future
ssrCache.set(key, { data, contentType })
res.append('X-Cache', 'MISS')
res.type(contentType)
res.end(data, 'binary')
}).catch(() => {
// Failed to download image
res.status(500).send('Error')
})
}
const PORT = process.env.PORT || 3000
app.prepare()
.then(() => {
const server = express()
server.disable('x-powered-by')
server.get('/docs', (req, res) => {
cachedRender(req, res, '/docs')
})
server.get('/docs/basics', (req, res) => {
cachedRender(req, res, '/docs/basics')
})
server.get('/docs/advanced', (req, res) => {
cachedRender(req, res, '/docs/advanced')
})
server.get('/docs/api', (req, res) => {
cachedRender(req, res, '/docs/api')
})
// Proxy imageshield.io images
const proxyMap = {
'npm-v.svg': 'https://img.shields.io/npm/v/styled-components.svg',
'size.svg': 'https://img.shields.io/badge/gzip%20size-14.6%20kB-brightgreen.svg',
'downloads.svg': 'https://img.shields.io/npm/dm/styled-components.svg?maxAge=3600',
'stars.svg': 'https://img.shields.io/github/stars/styled-components/styled-components.svg?style=social&label=Star&maxAge=3600',
}
// Define proxied routes
server.get('/proxy/:imgUrl', async (req, res, next) => {
const { imgUrl } = req.params
const remoteUrl = proxyMap[imgUrl]
// Check if we want to proxy this
if (typeof remoteUrl === 'undefined') {
// Let NextJS handle it (edither a route or 404 error)
next()
return
}
cachedProxyServer(req, res, imgUrl, remoteUrl)
})
server.get('/sw.js', (req, res) => {
res.sendFile(path.resolve('./.next/sw.js'))
})
server.use('/static', express.static('./static', {
maxage: '48h',
index: false,
redirect: false
}))
server.get('*', (req, res) => {
const parsedUrl = parse(req.url, true)
handle(req, res, parsedUrl)
})
server.listen(PORT, err => {
if (err) {
throw err
}
console.log(`> Ready on http://localhost:${PORT}`)
})
})