feat(http2): add render.http2.push option

This option disables http2 push headers by default as is currently inconsistent with different browser and webservers
This commit is contained in:
Pooya Parsa 2017-06-05 13:19:27 +04:30
parent 5763d21905
commit 349f6e6219
2 changed files with 20 additions and 14 deletions

View File

@ -51,6 +51,9 @@ class Nuxt {
scrollBehavior: null
},
render: {
http2: {
push: false
},
static: {},
gzip: {
threshold: 0
@ -66,11 +69,11 @@ class Nuxt {
}
// Sanitization
if (options.loading === true) delete options.loading
if (options.router && typeof options.router.middleware === 'string') options.router.middleware = [ options.router.middleware ]
if (options.router && typeof options.router.middleware === 'string') options.router.middleware = [options.router.middleware]
if (options.router && typeof options.router.base === 'string') {
this._routerBaseSpecified = true
}
if (typeof options.transition === 'string') options.transition = { name: options.transition }
if (typeof options.transition === 'string') options.transition = {name: options.transition}
this.options = _.defaultsDeep(options, defaults)
// Ready variable
this._ready = false

View File

@ -74,20 +74,23 @@ export async function render (req, res) {
}
res.setHeader('ETag', etag)
}
// HTTP2 push headers
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 regex = /link rel="([^"]*)" href="([^"]*)" as="([^"]*)"/g
const pushAssets = []
let m
while (m = regex.exec(resourceHints)) { // eslint-disable-line no-cond-assign
const [_, rel, href, as] = m // eslint-disable-line no-unused-vars
if (rel === 'preload') {
pushAssets.push(`<${href}>; rel=${rel}; as=${as}`)
}
}
res.setHeader('Link', pushAssets)
}
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.setHeader('Content-Length', Buffer.byteLength(html))
// Parse resourceHints to extract HTTP.2 prefetch/push headers
// https://w3c.github.io/preload/#server-push-http-2
const regex = /link rel="([^"]*)" href="([^"]*)" as="([^"]*)"/g
const pushAssets = []
let m
while (m = regex.exec(resourceHints)) { // eslint-disable-line no-cond-assign
const [_, rel, href, as] = m // eslint-disable-line no-unused-vars
if (rel === 'preload') {
pushAssets.push(`<${href}>; rel=${rel}; as=${as}`)
}
}
res.setHeader('Link', pushAssets)
res.end(html, 'utf8')
return html
} catch (err) {