diff --git a/lib/nuxt.js b/lib/nuxt.js index ee28be97cf..a7373a8cc6 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -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 diff --git a/lib/render.js b/lib/render.js index 0cc56b79df..aa3fa67689 100644 --- a/lib/render.js +++ b/lib/render.js @@ -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) {