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

Commit 7dd339a

Browse files
authored
Merge pull request #5586 from magento/kh_restore-5392
Restore PR-5392 to develop
2 parents 904253a + bc80f4e commit 7dd339a

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

_data/toc/frontend-developer-guide.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ pages:
124124
- label: Using custom fonts
125125
url: /frontend-dev-guide/css-topics/using-fonts.html
126126

127+
- label: CSS critical path
128+
url: /frontend-dev-guide/css-topics/css-critical-path.html
129+
127130
- label: Using custom CSS preprocessor
128131
children:
129132

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. This means that a 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-info }
15+
CSS critical path configuration is located on the **Stores** > Settings > **Configuration** > **ADVANCED** > **Developer** tab. However, the **Developer** tab is hidden in [production mode]({{page.baseurl}}/config-guide/bootstrap/magento-modes.html). Once in production mode, CSS critical path can only be enabled using the CLI.
16+
17+
To enable the 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+
The default Luma theme critical CSS file is located in `app/design/frontend/Magento/luma/web/css/critical.css`
29+
If there is not a `critical.css` file for the custom theme, but there is one for the 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+
If '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+
- Eliminated render-blocking CSS resources. As a result, the 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+
- The first meaningful paint time improved from 3.5 seconds to 2.3 seconds.
52+
53+
![First meaningful paint time improved]({{ site.baseurl }}/common/images/critical_css_improvements2.png)
54+
55+
- The speed index increased by 0.8 seconds.
56+
57+
![Speed index increased]({{ site.baseurl }}/common/images/critical_css_improvements3.png)
58+
59+
As a result, the Google PageSpeed Insights score improved by **5** points.
60+
61+
{: .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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ 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-
To enable or disable these settings, go to _Admin_ panel, click **Stores** > Settings > **Configuration** > **Advanced** > **Developer** > **CSS Settings**.
35+
* Use CSS critical path to eliminate render-blocking CSS resources.
36+
37+
To enable / disable these settings, go into Admin > **Stores** > Setting > **Configuration** > **Advanced** > **Developer** > **CSS Settings**.
3638

3739
## Change styles: walkthrough {#css_walk}
3840

@@ -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/theme-best-practice.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ We recommend using the following best practices when developing themes:
2323
1. 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.
2424
1. Use `<theme_dir>/etc/view.xml` to change image types or sizes, or add your own types. See [Configure images properties]({{ page.baseurl }}/frontend-dev-guide/themes/theme-images.html) for details. Use this file to also [customize the product gallery widget]({{ page.baseurl }}/javascript-dev-guide/widgets/widget_gallery.html).
2525
1. If you need to change the wording in the user interface, [add custom CSV dictionary files]({{ page.baseurl }}/frontend-dev-guide/translations/theme_dictionary.html) instead of overriding `.phtml` templates.
26+
1. Use [the CSS critical path]({{ page.baseurl }}/frontend-dev-guide/css-topics/css-critical-path.html) to render the page much faster.
2627

2728
After updating or upgrading Magento instances, check for changes in any files that are overridden by your theme. If there were changes to default templates, layouts, or styles, copy those 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

0 commit comments

Comments
 (0)