Page Speed: Specify a character set early More Recommendations

Rule Summary

Content
High
Easy
96%

Overview

Specifying a character set early for your HTML documents allows the browser to begin executing scripts immediately.

How does your site score on this rule?

Details from Google

HTML documents are sent over the Internet as a sequence of bytes accompanied by character encoding information. Character encoding information is specified in the HTTP response headers sent with the document, or in the HTML markup of the document itself. The browser uses the character encoding information to convert the stream of bytes into characters that it renders on-screen. Because a browser cannot correctly render a page without knowing how to construct the page's characters, most browsers buffer a certain number of bytes before executing any JavaScript or drawing the page, while they search for character set information in the input. (A notable exception is Internet Explorer versions 6, 7, and 8.)

Browsers differ with the respect to the number of bytes buffered and the default encoding assumed if no character set is found. However, once they have buffered the requisite number of bytes and begun to render the page, if they encounter a character set specification that doesn't match their default, they need to reparse the input and redraw the page. Sometimes, they may even have to rerequest resources, if the mismatch affects the URLs of external resources.

To avoid these delays, you should always specify the character encoding as early as possible, for any HTML document larger than 1 KB (1024 bytes, to be precise, which is the maximum buffer limit used by any of the browsers we have tested).

recommends:

Prefer HTTP over meta tag parameters

There are several ways to specify the character set for an HTML document:

  • Server-side: You configure your web server to specify the charset parameter, with the correct character encoding, in the Content-Type header for all documents that are of type text/html; e.g. Content-Type: text/html; charset=UTF-8
  • Client-side: You include the http-equiv="content" attribute in the meta tag in the HTML code, and specify the charset parameter; e.g. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

If possible, configure your web server to specify the character set in the HTTP headers. Some browsers (e.g. Firefox) use a shorter buffer delay before executing JavaScript if the charset is present in the header; that is, they can skip the additional buffering delay required to check the HTML markup.

Specify the meta tag at the top of the head section

If you don't control your web server configuration, and need to set the charset in the meta tag, be sure to specify the markup at the very top of the <head> section of the document. Browsers look for the charset parameter within the first 1024 bytes of the document, so to avoid performance penalties, it's crucial that the parameter appears as early as possible in the document head.

Always specify a content type

Before browsers can begin to check for a character set, they must first the determine the content type of the document being processed. If this is not specified in the HTTP header or the HTML meta tag, they will attempt to "sniff" the type, using various algorithms. This process can cause additional delays, besides representing a security vulnerability. For both performance and security reasons, you should always specify a content type for all resources (not only text/html).

Be sure to specify the correct character encoding

It's important that the character set you specify in an HTTP header or HTML meta tag match the character encoding actually used to author your HTML documents. If you specify a charset parameter in both the HTTP header and HTML meta tag, make sure they match each other. If the browser detects an incorrect or mismatched encoding, it will render the page incorrectly and/or incur additional delays while it redraws the page. For more information on valid character sets, see Section 5.2, Character Encodings in the HTML 4.01 Specification.

Read More

Contribute to this recommendation

Have an opinion, link, or other contribution to share regarding this recommendation? Share it with us and help make the web a faster place!