Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 681851d

Browse files
authored
Merge pull request #4726 from magento-performance/css_critical_path
Document CSS critical path
2 parents 729f7d5 + 5881bb8 commit 681851d

File tree

10 files changed

+564
-64
lines changed

10 files changed

+564
-64
lines changed
51.4 KB
Loading
37.5 KB
Loading
22.6 KB
Loading
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
group: frontend-developer-guide
3+
title: CSS critical path
4+
functional_areas:
5+
- Frontend
6+
---
7+
8+
All CSS styles loaded from external files are considered as render-blocking. It means that web page will not be displayed until these files are loaded.
9+
By using 'CSS critical path', we deliver minified critical CSS inline in `<head>` and defer all non-critical styles that are loaded asynchronously.
10+
Thus we can significantly improve the time to first render of our pages.
11+
12+
## Enable CSS critical path
13+
14+
{: .bs-callout .bs-callout-info }
15+
CSS critical path configuration is located in **Stores** > Settings > **Configuration** > **ADVANCED** > **Developer** tab. But **Developer** tab is hidden in [production mode][production-mode]. Once in production mode, CSS critical path can only be enabled using the CLI.
16+
17+
Enable CSS critical path:
18+
19+
```bash
20+
bin/magento config:set dev/css/use_css_critical_path 1
21+
```
22+
23+
Make sure that there is a `critical.css` file for your theme. Other non-critical CSS files will be loaded asynchronously.
24+
25+
## Overview of Magento's critical CSS
26+
27+
The 'critical' CSS file should be located in `app/design/frontend/<your_vendor_name>/<your_theme_name>/web/css/critical.css`
28+
Default Luma theme critical CSS file is located in `app/design/frontend/Magento/luma/web/css/critical.css`
29+
If there is not `critical.css` for the custom theme but there is Luma theme, Luma's `critical.css` will be used.
30+
THe critical css file path can also be configured in `di.xml` as a constructor `filePath` argument in the `Magento\Theme\Block\Html\Header\CriticalCss` block.
31+
32+
To generate a critical CSS for your theme, critical path CSS generators like [Penthouse](https://www.npmjs.com/package/penthouse) or [Critical](https://www.npmjs.com/package/critical) can be used,or you can create it yourself. While creating critical CSS, adhere to the following principles:
33+
34+
- Minify your `critical.css` to reduce its size.
35+
- Do not duplicate styles in `critical.css` and non-critical style sheets.
36+
- Do not introduce new styles in `critical.css` that are not present in non-critical style sheets. CSS rules from non-critical style sheets should overwrite critical CSS rules. Otherwise your styles can be broken.
37+
38+
### Critical CSS loader
39+
40+
Ff 'CSS critical path' is enabled in the configuration, a preloading spinner will be used. It is added in `Magento/Theme/view/frontend/layout/default.xml`.
41+
After non-critical CSS is loaded and applied, the spinner disappears. The spinner will disappear automatically only if you have CSS styles from the 'Blank' theme CSS. If you have your own CSS rules, you should hide the preloader by using the `data-role='main-css-loader'` attribute.
42+
43+
## Critical CSS performance improvements
44+
45+
Introducing a critical path CSS to Magento leads to performance improvements:
46+
47+
1. Eliminated render-blocking CSS resources. As a result, time for loading render-blocking resources decreases substantially.
48+
49+
![CSS resources eliminated as render-blocking]({{ site.baseurl }}/common/images/critical_css_improvements1.png)
50+
51+
2. First meaningful paint time improved from 3.5 sec to 2.3 sec.
52+
53+
![First meaningful paint time improved]({{ site.baseurl }}/common/images/critical_css_improvements2.png)
54+
55+
3. Speed index increased by 0.8 sec.
56+
57+
![Speed index increased]({{ site.baseurl }}/common/images/critical_css_improvements3.png)
58+
59+
As a result Google PageSpeed Insights score improved by **5** points.
60+
61+
{: .bs-callout .bs-callout-info }
62+
These are results for mobile devices with slow connection from [Google PageSpeed Insights](https://developers.google.com/speed/pagespeed/insights/)

guides/v2.2/frontend-dev-guide/css-topics/css-overview.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ There are a couple options to help with CSS and site performance.
3232

3333
* Minification of CSS files reduces the file size being sent. It does this by stripping white space within the file.
3434

35+
* Use CSS critical path to eliminate render-blocking CSS resources.
36+
3537
To enable / disable these settings, go into Admin > **Stores** > Setting > **Configuration** > **Advanced** > **Developer** > **CSS Settings**.
3638

3739
## Change styles: walkthrough {#css_walk}
@@ -68,6 +70,7 @@ Other topics of this chapter describe the following:
6870
* [CSS Preprocessing]({{ page.baseurl }}/frontend-dev-guide/css-topics/css-preprocess.html): how stylesheets are preprocessed and compiled
6971
* [Magento UI Library]({{ page.baseurl }}/frontend-dev-guide/css-topics/theme-ui-lib.html): how to use the Magento styles [library](https://glossary.magento.com/library) in your custom themes
7072
* [Using Custom Fonts]({{ page.baseurl }}/frontend-dev-guide/css-topics/using-fonts.html): how to add custom fonts
73+
* [CSS critical path]({{ page.baseurl }}/frontend-dev-guide/css-topics/css-critical-path.html): how to use CSS critical path
7174
* [Customizing styles illustration]({{ page.baseurl }}/frontend-dev-guide/css-topics/css-practice.html): how to change a theme's color scheme using Magento UI library.
7275
7376
[The default view of a product page, with the orange Add to Cart button]: {{site.baseurl}}/common/images/css_over1.png

guides/v2.2/frontend-dev-guide/css-topics/using-fonts.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ To ensure the stability of your customizations and prevent upgrades from overwri
2525
@font-path: '@{baseDir}fonts/<path_to_font_file>',
2626
@font-weight: <font_weight>,
2727
@font-style: <font_style>
28+
@font-display: <auto|block|fallback|option|swap>
2829
);
2930
```
3031

@@ -42,8 +43,18 @@ To ensure the stability of your customizations and prevent upgrades from overwri
4243
src: url('../fonts/opensans/light/opensans-300.eot?#iefix') format('embedded-opentype'), url('../fonts/opensans/light/opensans-300.woff2') format('woff2'), url('../fonts/opensans/light/opensans-300.woff') format('woff'), url('../fonts/opensans/light/opensans-300.ttf') format('truetype'), url('../fonts/opensans/light/opensans-300.svg#Open Sans') format('svg');
4344
font-weight: 300;
4445
font-style: normal
46+
font-display: swap;
4547
}
4648
```
49+
50+
`@font-display: swap` is declared by default for Magento Blank theme in `app/design/frontend/Magento/blank/web/css/source/_typography.less`.
51+
52+
Fallback web fonts that are used by default in Magento are located in `lib/web/css/source/lib/variables/_typography.less`.
53+
54+
## `<font>` head type
55+
56+
A `<font>` node is added to HTML `<head>` type for layout in `lib/internal/Magento/Framework/View/Layout/etc/head.xsd`. All resources added with `<font>` node will be downloaded with `preload` html attribute.
57+
4758
## Overview of Magento's Icon CSS
4859

4960
In addition to including custom fonts in your Magento Blank theme, you also can include custom fonts for any icons in the Blank theme. The icon font files for the Magento Blank theme are located in the `lib/web/fonts/Blank-Theme-Icons` directory. The `lib/web/css/source/lib/variables/_typography.less` file defines the font icon path and name for the icons and the `web/css/source/_icons.less` file uses these files to define the icon font face itself, which should be used in all CSS declarations.

guides/v2.2/frontend-dev-guide/theme-best-practice.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ Some other customizations that can be performed using layout instructions are th
2727
3. Reuse the markup and design patterns from the default Magento files by referencing the existing `.phtml` templates ([templates hints can help]({{ page.baseurl }}/frontend-dev-guide/themes/debug-theme.html#debug-theme-templ)) or copy-pasting HTML markup to your custom templates.
2828
4. Use `<theme_dir>/etc/view.xml` to change image types sizes or add your own types. See [Configure images properties]({{ page.baseurl }}/frontend-dev-guide/themes/theme-images.html) for details. Use this file also to [customize product gallery widget]({{ page.baseurl }}/javascript-dev-guide/widgets/widget_gallery.html)
2929
5. If you need to change the wording in user interface, [add custom CSV dictionary files]({{ page.baseurl }}/frontend-dev-guide/translations/theme_dictionary.html) instead of overriding `.phtml` templates.
30+
6. Use [CSS critical path]({{ page.baseurl }}/frontend-dev-guide/css-topics/css-critical-path.html) to get the page to render much faster.
3031

3132
Keep in mind that, after updating or upgrading Magento instances, there may be changes to default templates, layouts, and styles. So it is recommended to check if those changes affect any files overridden in your theme and if so, copy the changes to your templates, layouts, and styles.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.2/frontend-dev-guide/css-topics/css-critical-path.md

guides/v2.3/frontend-dev-guide/css-topics/using-fonts.md

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.2/frontend-dev-guide/css-topics/using-fonts.md

0 commit comments

Comments
 (0)