Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include “critical styles” in SSR output #392

Open
BigAB opened this issue Oct 20, 2017 · 5 comments
Open

Include “critical styles” in SSR output #392

BigAB opened this issue Oct 20, 2017 · 5 comments

Comments

@BigAB
Copy link
Contributor

BigAB commented Oct 20, 2017

This was discussed on a recent live stream (24:36).

I see a significant delay from when the HTML is sent to the client, and when the CSS arrives (when artificially throttling the network with Chrome tools at least).

The render is blocked until the CSS arrives, which undoes a big portion of the super performance gains of SSR.

Is there a way, to determine the CSS needed to render, and push it in with the HTML in a style tag?

It might look something like:

<html>
<head>
  <title>My App</title>
  <style>body { ... }, nav.header { ... }</style>
</head>
<body>
  <link rel="stylesheet" href="/dist/bundles/app/app.styles.css">
</body>
</html>
  1. The critical styles here are inlined.
  2. The rest of the main bundles styles are put into a <link> tag and added to the browser. Some browsers won't block rendering for link tags inside of the body.

Possible tools to look at for implementation ideas:

https://github.com/addyosmani/critical-path-css-tools

@justinbmeyer
Copy link
Contributor

justinbmeyer commented Oct 20, 2017 via email

@BigAB
Copy link
Contributor Author

BigAB commented Oct 20, 2017

My example was not using HTTP/2.

Yeah, maybe it's a non-issue if using PUSH and HTTP/2. I guess we can close this.

@matthewp
Copy link
Contributor

Need to configure steal-bundle-manifest with your app to use push.

@matthewp
Copy link
Contributor

This is a valid optional enhancement though. Not sure how to really do it, you wouldn't want to inline the entire main css bundle most likely.

@chasenlehara chasenlehara changed the title zones/steal should include "Critical styles" in the HTML Include “critical styles” in SSR output Nov 16, 2018
@leoj3n
Copy link
Contributor

leoj3n commented Nov 18, 2018

In the following video @matthewp mentions that style <link> tags in the <body> won't block the rest of the page rendering:

https://youtu.be/kx-JxYSWlEc?t=1510

However, I recently read this article that explains the blocking is when any async JavaScript comes after a style <link>:

https://csswizardry.com/2018/11/css-and-network-performance/

So that might be what you're actually seeing. Just thought I'd add this here.

EDIT:

The same article goes into more depth than I realized and talks about new browsers optimizing for <link> in the body like @matthewp mentioned, while older browsers fall back to the behavior above:

However, due to a recent change in Chrome (version 69, I believe), and behaviour already present in Firefox and IE/Edge, <link rel="stylesheet" />s will only block the rendering of subsequent content, rather than the whole page. This means that we’re now able to construct our pages like:

<html>
<head>

  <link rel="stylesheet" href="core.css" />

</head>
<body>

  <link rel="stylesheet" href="site-header.css" />
  <header class="site-header">

    <link rel="stylesheet" href="site-nav.css" />
    <nav class="site-nav">...</nav>

  </header>

  <link rel="stylesheet" href="content.css" />
  <main class="content">

    <link rel="stylesheet" href="content-primary.css" />
    <section class="content-primary">

      <h1>...</h1>

      <link rel="stylesheet" href="date-picker.css" />
      <div class="date-picker">...</div>

    </section>

    <link rel="stylesheet" href="content-secondary.css" />
    <aside class="content-secondary">

      <link rel="stylesheet" href="ads.css" />
      <div class="ads">...</div>

    </aside>

  </main>

  <link rel="stylesheet" href="site-footer.css" />
  <footer class="site-footer">
  </footer>

</body>

And links to this other article about it: https://jakearchibald.com/2016/link-in-body/

BTW, here's Google's recommendations on implementing critical path:
https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants