mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 22:25:12 +00:00
HTTP.2 Push Headers
This commit is contained in:
parent
7146b0f2a8
commit
38e7dd509a
@ -54,13 +54,14 @@ export async function render (req, res) {
|
||||
res.statusCode = 404
|
||||
return res.end()
|
||||
}
|
||||
const { html, error, redirected } = await this.renderRoute(req.url, context)
|
||||
const { html, error, redirected, resourceHints } = await this.renderRoute(req.url, context)
|
||||
if (redirected) {
|
||||
return html
|
||||
}
|
||||
if (error) {
|
||||
res.statusCode = context.nuxt.error.statusCode || 500
|
||||
}
|
||||
// ETag header
|
||||
if (!error && this.options.render.etag) {
|
||||
const etag = generateETag(html, this.options.render.etag)
|
||||
if (fresh(req.headers, { etag })) {
|
||||
@ -72,6 +73,18 @@ export async function render (req, res) {
|
||||
}
|
||||
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
|
||||
if (rel === 'preload') {
|
||||
pushAssets.push(`<${href}>; rel=${rel}; as=${as}`)
|
||||
}
|
||||
}
|
||||
res.setHeader('Link', pushAssets)
|
||||
res.end(html, 'utf8')
|
||||
return html
|
||||
} catch (err) {
|
||||
@ -106,7 +119,8 @@ export async function renderRoute (url, context = {}) {
|
||||
if (this._routerBaseSpecified) {
|
||||
HEAD += `<base href="${this.options.router.base}">`
|
||||
}
|
||||
HEAD += context.renderResourceHints() + context.renderStyles()
|
||||
const resourceHints = context.renderResourceHints()
|
||||
HEAD += resourceHints + context.renderStyles()
|
||||
APP += `<script type="text/javascript">window.__NUXT__=${serialize(context.nuxt, { isJSON: true })}</script>`
|
||||
APP += context.renderScripts()
|
||||
const html = this.appTemplate({
|
||||
@ -117,6 +131,7 @@ export async function renderRoute (url, context = {}) {
|
||||
})
|
||||
return {
|
||||
html,
|
||||
resourceHints,
|
||||
error: context.nuxt.error,
|
||||
redirected: context.redirected
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user