-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathmiddleware.js
195 lines (177 loc) · 5.5 KB
/
middleware.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
const { gzipSync } = require('zlib')
const generateETag = require('etag')
const fresh = require('fresh')
const { createSitemap, createSitemapIndex } = require('./builder')
const { createRoutesCache } = require('./cache')
const logger = require('./logger')
const { setDefaultSitemapOptions, setDefaultSitemapIndexOptions } = require('./options')
const { excludeRoutes } = require('./routes')
/**
* Register a middleware for each sitemap or sitemapindex
*
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
/* istanbul ignore if */
if (depth > 1) {
// see https://webmasters.stackexchange.com/questions/18243/can-a-sitemap-index-contain-other-sitemap-indexes
logger.warn("A sitemap index file can't list other sitemap index files, but only sitemap files")
}
const isSitemapIndex = options && options.sitemaps && Array.isArray(options.sitemaps) && options.sitemaps.length > 0
if (isSitemapIndex) {
registerSitemapIndex(options, globalCache, nuxtInstance, depth)
} else {
registerSitemap(options, globalCache, nuxtInstance, depth)
}
}
/**
* Register a middleware to serve a sitemap
*
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
function registerSitemap(options, globalCache, nuxtInstance, depth = 0) {
const base = nuxtInstance.options.router.base
// Init options
options = setDefaultSitemapOptions(options, nuxtInstance, depth > 0)
// Init cache
const cache = {}
cache.staticRoutes = () => excludeRoutes(options.exclude, globalCache.staticRoutes)
cache.routes = createRoutesCache(cache, options)
// On run cmd "start" or "generate [--no-build]"
if (globalCache.staticRoutes) {
// On server ready
nuxtInstance.nuxt.hook('listen', () => {
// Hydrate cache
cache.routes.get('routes')
})
}
if (options.gzip) {
// Add server middleware for sitemap.xml.gz
nuxtInstance.addServerMiddleware({
path: options.pathGzip,
async handler(req, res, next) {
try {
// Init sitemap
const routes = await cache.routes.get('routes')
const gzip = await createSitemap(options, routes, base, req).toGzip()
// Check cache headers
if (validHttpCache(gzip, options.etag, req, res)) {
return
}
// Send http response
res.setHeader('Content-Type', 'application/gzip')
res.end(gzip)
} catch (err) {
/* istanbul ignore next */
next(err)
}
},
})
}
// Add server middleware for sitemap.xml
nuxtInstance.addServerMiddleware({
path: options.path,
/**
* @param {Request} req
* @param {Response} res
* @param {*} next
*/
async handler(req, res, next) {
try {
// Init sitemap
const routes = await cache.routes.get('routes')
const xml = await createSitemap(options, routes, base, req).toXML()
// Check cache headers
if (validHttpCache(xml, options.etag, req, res)) {
return
}
// Send http response
res.setHeader('Content-Type', 'application/xml')
res.end(xml)
} catch (err) {
/* istanbul ignore next */
next(err)
}
},
})
}
/**
* Register a middleware to serve a sitemapindex
*
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
function registerSitemapIndex(options, globalCache, nuxtInstance, depth = 0) {
const base = nuxtInstance.options.router.base
// Init options
options = setDefaultSitemapIndexOptions(options, nuxtInstance)
if (options.gzip) {
// Add server middleware for sitemapindex.xml.gz
nuxtInstance.addServerMiddleware({
path: options.pathGzip,
handler(req, res, next) {
// Init sitemap index
const sitemapIndex = createSitemapIndex(options, base, req)
const gzip = gzipSync(sitemapIndex)
// Check cache headers
if (validHttpCache(gzip, options.etag, req, res)) {
return
}
// Send http response
res.setHeader('Content-Type', 'application/gzip')
res.end(gzip)
},
})
}
// Add server middleware for sitemapindex.xml
nuxtInstance.addServerMiddleware({
path: options.path,
handler(req, res, next) {
// Init sitemap index
const xml = createSitemapIndex(options, base, req)
// Check cache headers
if (validHttpCache(xml, options.etag, req, res)) {
return
}
// Send http response
res.setHeader('Content-Type', 'application/xml')
res.end(xml)
},
})
// Register linked sitemaps
options.sitemaps.forEach((sitemapOptions) => registerSitemaps(sitemapOptions, globalCache, nuxtInstance, depth + 1))
}
/**
* Validate the freshness of HTTP cache using headers
*
* @param {Object} entity
* @param {Object} options
* @param {Request} req
* @param {Response} res
* @returns {boolean}
*/
function validHttpCache(entity, options, req, res) {
if (!options) {
return false
}
const { hash } = options
const etag = hash ? hash(entity, options) : generateETag(entity, options)
if (fresh(req.headers, { etag })) {
// Resource not modified
res.statusCode = 304
res.end()
return true
}
// Add ETag header
res.setHeader('ETag', etag)
return false
}
module.exports = { registerSitemaps, registerSitemap, registerSitemapIndex }