ASP.Net HTTP2 PushPromise is slow

女生的网名这么多〃 提交于 2019-12-07 13:45:02

问题


I'm trying to implement the Http2 push server functionality using "PushPromise" .NET 4.6.1, for this I have a "html extension" with Razor (we did not implement MVC only used the Razor engine to build the pages).

    public static IHtmlString PushPromiseStylesheet(this HtmlHelper htmlHelper, string src, bool addDomElement = true)
    {
        var context = HttpContext.Current;
        var path = System.Web.Optimization.Styles.Url(src).ToString();

        var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } };
        context.Response.PushPromise(path, "GET", headers);

        var styleElement = $"<link rel=\"preload\" href=\"{path}\" as=\"style\">";

        return new HtmlString(addDomElement ? styleElement : String.Empty);
    }

    public static IHtmlString PushPromiseJavascript(this HtmlHelper htmlHelper, string src)
    {
        var context = HttpContext.Current;
        var path = System.Web.Optimization.Scripts.Url(src).ToString();

        var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } };
        context.Response.PushPromise(path,"GET",headers);

        var javascriptElement = $"<link rel=\"preload\" href=\"{path}\" as=\"script\">";

        return new HtmlString(javascriptElement);
    }
    public static IHtmlString PushPromiseImage(this HtmlHelper htmlHelper, string src, bool addDomElement = false)
    {
        var context = HttpContext.Current;
        var path = System.Web.Optimization.Scripts.Url(src).ToString();

        var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } };
        context.Response.PushPromise(path,"GET",headers);

        var imgElement = $"<link rel=\"preload\" href=\"{path}\">";

        return new HtmlString(addDomElement ? imgElement : String.Empty);
    }

    public static IHtmlString PushPromiseWebFont(this HtmlHelper htmlHelper, string src, string type = null)
    {
        var context = HttpContext.Current;
        var path = System.Web.Optimization.Scripts.Url(src).ToString();
        type = string.IsNullOrWhiteSpace(type) ? "font/woff2" : type;

        var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } };
        context.Response.PushPromise(path, "GET", headers);

        var fontElement = $"<link rel=\"preload\" href=\"{path}\" as=\"font\" type=\"{type}\"> ";

        return new HtmlString(fontElement);
    }

And in the < head > of the page:

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<title>@Page.Title</title>
@Html.PushPromiseStylesheet("~/css/app/fonts.css")
@Html.PushPromiseStylesheet("~/css/landing.base.css")
@Html.PushPromiseStylesheet("~/css/landing.style.css")
@Html.PushPromiseImage("/assets/img/bg-ofertas.jpg")
@Html.PushPromiseImage("/assets/img/landings/v2/iphone.png")
@Html.PushPromiseImage("/assets/img/landings/v2/ipad-new.png")
@Html.PushPromiseImage("/assets/img/landings/v2/macbook-new.png")

@Html.PushPromiseWebFont("/assets/fonts/CredimejoraIcons.woff2?miydpz")
@Html.PushPromiseWebFont("/assets/fonts/centrale_sans_regular_italic-webfont.woff2")
@Html.PushPromiseWebFont("/assets/fonts/centrale_sans_bold_italic-webfont.woff2")
@Html.PushPromiseWebFont("/assets/fonts/centrale_sans_regular-webfont.woff2")
@Html.PushPromiseWebFont("/assets/fonts/centrale_sans_bold-webfont.woff2")

@Html.PushPromiseJavascript("/assets/js/public/libs/jquery.inputmask.bundle-3.3.6.2.js")

I have hosted the same project "without using PushPromise" (credimejorademo), and in another domain I have implemented PushPromise (aquituinmueble):

https://www.webpagetest.org/video/compare.php?tests=170609_PD_e32f303e034aef3fef7140fef181a738,170609_FK_7d538ecdacf071cce320ad14bf97414c


回答1:


Just a couple of things I'd mention here. Firstly, although this may seem counter-intuitive, you're probably best off not pushing -everything-. The limited amount of testing I've seen (eg: https://www.smashingmagazine.com/2017/04/guide-http2-server-push/) suggests that pushing css only is probably the best way to improve performance, although it's worth playing around with on an individual site basis.

The other thing is that the way you are implementing PushPromise means that IIS is only getting that request once it's already started processing the page. ie, you're requesting the push at more or less the same time the page would have requested it via a standard .

Hope this helps.



来源:https://stackoverflow.com/questions/44464429/asp-net-http2-pushpromise-is-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!