GSA / digitalgov.gov

Digital.gov: Better websites. Better government.
https://digital.gov
Other
227 stars 302 forks source link

Draft: Principle 6, The Smaller, the Better! #879

Closed ToniBonittoGSA closed 5 years ago

ToniBonittoGSA commented 5 years ago

Draft

The Smaller the Better, When It Comes to Page Resources!

This week we will talk about making page resources smaller to improve the rendering of the page.

The incorrect sizing and placement of objects on a site may lead to longer page load times and mobile unfriendliness. Mobile devices are getting faster with every new release, however they still rely on the mobile network to get the information to the device and we have all been in locations where we have little to no bars of connectivity. If we can reduce the size of what is being sent over the network, the page will load faster. The following are 5 solutions to make your page content lighter:

  1. Enable Compression
  2. Minify Resources
  3. Optimize Images
  4. Optimize CSS Delivery
  5. Prioritize Visible Content

Enable Compression

Issue: Large page files, which result in poor rendering performance

Enabling file compression typically saves around 50 to 70 percent of the file size, and may reduce the size of page resources by up to 90 percent. This will significantly reduce the time it takes to download the resource, and improve the time to first render the page. A good standard practice is to use gzip since all modern browsers support gzip compression for HTTP requests.

Solution: Use gzip on compressible resources!

Your web server has gzip compression enabled (but may be configured differently depending on the specific server). The following four examples are ways to enable gzip on some of the most common servers.

  1. General .htaccess — Enable the zip parameters in the .htaccess file. Below is an example:
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
< /ifModule>

(https://varvy.com/pagespeed/enable-compression.html)

  1. Apache Server — Enable the zip parameters in the .htaccess file. Below is an example:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

(https://varvy.com/pagespeed/enable-compression.html)

  1. NGINX Server — Enable compression by adding the following code to your config file:
gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

# Disable for IE < 6 because there are some known problems
gzip_disable "MSIE [1-6].(?!.*SV1)";

# Add a vary header for downstream proxies to avoid sending cached gzipped files to IE6
gzip_vary on;

(https://varvy.com/pagespeed/enable-compression.html)

  1. Litespeed Server — Under the server's configuration section, go to “Tuning” and make sure that “enable compression” is on.

You can quickly a test with a gzip compression tool, such as https://varvy.com/tools/gzip/.

One final word of caution: proxy servers and anti-virus software may disable compression as files are downloaded to a client machine.

References

Minify Resources

Issue: Large CSS, JavaScript, and HTML Files Increase Download Speeds

To improve performance CSS, JavaScript, and HTML files should be as lean as possible. Minification is the process of removing unnecessary or redundant data, unnecessary spaces, formatting and comments from the files, without affecting how the resource is processed by the browser - e.g., code comments and formatting, removing unused code, and using shorter variable and function names. This in turn reduces the file size and potentially increases the download speed of the file to the browser.

The following is an example of how minification reduces a CSS file.

The following is the CSS file text that has not been minimized:

/* Slider Comment 1 */

#slider ul {
  position: relative;
  margin: 0;
  padding: 0;
  height: 200px;
  list-style: none;
}

/* Slider Comment 2 */

#slider ul li {
  position: relative;
  display: block;
  float: left;
  margin: 0;
  padding: 0;
  width: 500px;
  height: 300px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}

The following is the CSS file text minimized, notice the difference, no white space and everything on one line:

#slider ul {position:relative; margin:0; padding:0; height:200px; list-style:none;} #slider ul li {position:relative; display:block; float:left; margin:0; padding:0; width:500px; height:300px; background:#ccc; text-align:center; line-height:300px;} 

Solution: Use tools to minify CSS, JavaScript, and HTML files!

Each file type requires a different process to minimize the file, and there are many tools available to minify each file type. The following are some examples.

References

Optimize Images

Issue: Image files are too large and impact performance

To improve performance, images should be as lean as possible. We often create or save beautiful colorful images to our site not understanding that images often account for most of the downloaded bytes on a page. Images hold data other than simply the pixels we see on the screen; this data increases the size of the image file, which in turn leads to longer load times as the image downloads.

Additionally, there are two forms of compression: Lossy and Lossless. The Lossless format retains all the information needed to produce the original image. So Lossless images carry a lot more data and in return are a much larger file size, while the Lossy formatted images will look slightly different than the original image when uncompressed. Keep in mind that this is noticeable. Lossy compression is good for web because images use a smaller amount of memory, but can look like the original image.

We choose to optimize images because 90 percent of most websites are graphics-dependent and therefore have a lot of image files. Leaving these images uncompressed and in the wrong format can drastically slow down your page load times.

Solution: Use tools to compress image files!

While image optimization is still more of an “art” than a science, the following are three key considerations when optimizing an image:

References

Optimize CSS Delivery

Issue: Render-blocking style sheets results in slow page load times

For the browser to render a page it must first process all the style and layout information. However, the browser will block rendering until all external stylesheets are downloaded and processed, which may require multiple round trips and delay the time to first render.

In a perfect and uncomplicated world, CSS setup might look something like this:

However, in practice, things are often more complicated, and each complication delays page rendering.

Solution: Streamline your CSS files to make them more efficient!

The following are some recommendations on how to streamline your CSS files to promote the fastest loading and rendering for the client:

Once you have your site and pages ready, run it against these two sites, which will help you assess the performance of your CSS sheets:

References

Prioritize Visible Content

Issue: Users don’t need the entire page to load to start looking at the above the fold content.

Visible content—also referred to as, _above the fold_ (a newspaper term)—is the part of the screen that the user sees first, before they do any scrolling. Often this is the title and/or navigation menu. Users want to see a page load quickly. However, many web pages are fairly large and no user wants to wait for an entire page to load before they see any visible content. The bigger the page, the more network round-trips are required to fetch and to render the above-the-fold content of the page. Loading the visible content before the other elements of a page creates a much better user experience.

Solution: Prioritize and limit the size of the data needed to render the visible content or above the fold content of your page

In addition to the other performance recommendations above (such as enabling compression, minifying resources, optimizing images, and optimizing CSS delivery), below are some additional recommendations you can use to prioritize and limit the size of your page to improve the visual content experience:

References


(All references to specific brands, products, and/or companies are used only for illustrative purposes and do not imply endorsement by the U.S. federal government or any federal government agency.)

ToniBonittoGSA commented 5 years ago

published