2018-01-04 17:36:47 +00:00
|
|
|
const generateETag = require('etag')
|
|
|
|
const fresh = require('fresh')
|
|
|
|
|
2018-01-04 17:39:12 +00:00
|
|
|
const { getContext } = require('../../common/utils')
|
2018-01-04 17:36:47 +00:00
|
|
|
|
|
|
|
module.exports = async function nuxtMiddleware(req, res, next) {
|
|
|
|
// Get context
|
|
|
|
const context = getContext(req, res)
|
|
|
|
|
|
|
|
res.statusCode = 200
|
|
|
|
try {
|
|
|
|
const result = await this.renderRoute(req.url, context)
|
|
|
|
await this.nuxt.callHook('render:route', req.url, result)
|
2018-01-13 05:22:11 +00:00
|
|
|
const {
|
|
|
|
html,
|
|
|
|
cspScriptSrcHashes,
|
|
|
|
error,
|
|
|
|
redirected,
|
|
|
|
getPreloadFiles
|
|
|
|
} = result
|
2018-01-04 17:36:47 +00:00
|
|
|
|
|
|
|
if (redirected) {
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
res.statusCode = context.nuxt.error.statusCode || 500
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ETag header
|
|
|
|
if (!error && this.options.render.etag) {
|
|
|
|
const etag = generateETag(html, this.options.render.etag)
|
|
|
|
if (fresh(req.headers, { etag })) {
|
|
|
|
res.statusCode = 304
|
|
|
|
res.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.setHeader('ETag', etag)
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:10:52 +00:00
|
|
|
// HTTP2 push headers for preload assets
|
2018-01-04 17:36:47 +00:00
|
|
|
if (!error && this.options.render.http2.push) {
|
|
|
|
// Parse resourceHints to extract HTTP.2 prefetch/push headers
|
|
|
|
// https://w3c.github.io/preload/#server-push-http-2
|
|
|
|
const pushAssets = []
|
2018-01-04 22:10:52 +00:00
|
|
|
const preloadFiles = getPreloadFiles()
|
|
|
|
const { shouldPush } = this.options.render.http2
|
|
|
|
const { publicPath } = this.resources.clientManifest
|
|
|
|
|
|
|
|
preloadFiles.forEach(({ file, asType, fileWithoutQuery, extension }) => {
|
|
|
|
// By default, we only preload scripts or css
|
2018-01-08 15:02:18 +00:00
|
|
|
/* istanbul ignore if */
|
2018-01-04 22:10:52 +00:00
|
|
|
if (!shouldPush && asType !== 'script' && asType !== 'style') {
|
|
|
|
return
|
2018-01-04 17:36:47 +00:00
|
|
|
}
|
2018-01-04 22:10:52 +00:00
|
|
|
|
|
|
|
// User wants to explicitly control what to preload
|
|
|
|
if (shouldPush && !shouldPush(fileWithoutQuery, asType)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pushAssets.push(`<${publicPath}${file}>; rel=preload; as=${asType}`)
|
|
|
|
})
|
|
|
|
|
2018-01-04 17:36:47 +00:00
|
|
|
// Pass with single Link header
|
2018-01-04 22:10:52 +00:00
|
|
|
// https://preloadFilesblog.cloudflare.com/http-2-server-push-with-multiple-assets-per-link-header
|
|
|
|
// https://www.w3.org/Protocols/9707-link-header.html
|
2018-01-04 17:36:47 +00:00
|
|
|
res.setHeader('Link', pushAssets.join(','))
|
|
|
|
}
|
|
|
|
|
2018-01-16 15:11:17 +00:00
|
|
|
if (this.options.render.csp && this.options.render.csp.enabled) {
|
|
|
|
const allowedSources = cspScriptSrcHashes.concat(this.options.render.csp.allowedSources)
|
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
res.setHeader(
|
|
|
|
'Content-Security-Policy',
|
2018-01-16 15:11:17 +00:00
|
|
|
`script-src 'self' ${(allowedSources).join(' ')}`
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2018-01-10 06:36:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 17:36:47 +00:00
|
|
|
// Send response
|
|
|
|
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(html))
|
|
|
|
res.end(html, 'utf8')
|
|
|
|
return html
|
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (context && context.redirected) {
|
|
|
|
console.error(err) // eslint-disable-line no-console
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
next(err)
|
|
|
|
}
|
|
|
|
}
|