Powered By Blogger

sábado, 1 de diciembre de 2012

Chrome Dev Tools: Networking and the Console

In Part 1 – Chrome Dev Tools: Markup and Style, we reviewed two panels: Elements and Resources. Moving on, now, we’ll take a look at the next two panels: Network and Console.


Network Panel

The Network panel gives us a view into the resources that are requested and downloaded over the network in real-time. Viewing network traffic isn’t the most glamorous activity – especially if you’re new to web development. However, performance becomes an important issue, as your site’s traffic increases. Identifying and fixing requests that take a long time to complete is an important step in optimizing a site.

An HTTP proxy, such as Charles Proxy, can give you a decent amount of information regarding your website’s network. That being said, the Network panel offers a surprising amount of detailed information; considering how it’s only a few clicks away, it’s certainly an excellent candidate for debugging network issues. The following is a screenshot of the Network panel when loading the Linkedin mobile site on a fresh page load:

The network panel

$0 returns the currently selected element in the Elements panel.

  1. This button opens the Network panel. Be sure to open the tab before loading a page so that it captures the network requests (e.g. open a new browser tab, open up developer tools, open up the network tab and then finally load the URL to monitor)
  2. This row contains one resource with interesting information that I cover in the following points. The first column is the name and path of the resource. The resource name will show on the first line while the path to the resource shows on the second.
  3. The HTTP method used for the request.
  4. The HTTP status code returned by the server. 200 is a common response for a successful response; although anything within the range of 200-299 is considered OK. Notice how one of the above requests is in red. This is related to the HTTP response code, a 401 Unauthorized response, because I’m not logged in to the LinkedIn mobile site.
  5. The content-type of a request. For example, in an HTTP POST (using the metrics resource as an example), the content-type in the request and response headers is: application/json; whereas, the main HTML page’s response headers contain a content-type of text/html. A small note on the application cache: when serving a manifest file, be sure to serve it with a content type of text/cache-manifest. When debugging, the “Type” column in the Network panel is the place to ensure it’s being served correctly!
  6. The initiator is an interesting column; it allows us to see why a resource is downloaded. For example, you may see the __utm.gif file often in your debugging sessions; it is used by Google Analytics. The initiator shows as ga.js:52, a value in file_name:line_number format. With this column, you can discover why, when, and how resources are downloaded. If Parser is shown for the initiator, chances are good that the browser initiated the request upon seeing something like a <link /> or <script /> element in the document.
  7. Timing contains information on how long a request actually took to download.

  8. This displays the size of the resource, an invaluable piece of information. When building a site, be sure to regularly “sort by size” in the Network panel and ensure that your resource sizes are correct. It is sometimes considered a good practice to base 64 encode images and embed them in CSS files. This can reduce the number of HTTP requests, but it also increases your CSS file’s size. Large resources should be delayed as much as possible to provide a snappy experience for the user.
  9. This displays the amount of time it took to retrieve the resource. Resources served from a different domain may take longer to download than other resources served from another origin.
  10. DOMContentLoaded (an event we can listen for using JavaScript) fires once the DOM is ready.
  11. The Load event which fires when the DOM is ready and resources have finished loading.
  12. Timeline view of network requests. You can hover over them for more information.
  13. Total size transferred, number of requests, and time taken.
  14. Icons to interact with the Network panel, such as clearing the panel of all requests and changing icon size.
  15. Useful network requests filters, e.g. you can control + click XHR and Images to only show network requests for an image resource or a request made with an XHR object.
  16. Headings for the network requests; you can click a heading to sort the requests by the data in the column.

Selected Resource Pane

Clicking on a request displays more information in a new panel, splitting the Network panel into two vertical panes.

Errors stand out from other console entries thanks to their red color and icon.

The Selected Resource pane has tabs within itself, such as Headers, Preview, Response, Cookies & timing.

  • Headers contains the request and response headers.
  • Preview contains a preview of the resource, for example a rendered image or some source code.
  • Response is similar to Preview and shows what was received from the server.
  • Cookies (if any) shows cookies sent with the request.
  • Timing contains information on how long a request actually took to download.

You can close this pane by clicking on the small X in the top left corner of the this pane.

Headers

The Headers tab provides you with some interesting information, which can prove useful when debugging. It displays both the request and response headers.

Network panel, source vs. parsed

Preview / Response

The Preview and Response tabs display a small preview of the selected resource. The developer tools is smart enough to correctly display the response’s content. Images display as rendered images, XML as text, etc. You’ll also find extra information such as image sizes, content-type, etc.

The response tab

Timing

The Timing tab includes time for DNS lookups, SSL negotiation, time to send and receive bytes, and the time that was spent waiting for a response.

Network panel, resource timing information

Basically, everything regarding time can be found within this tab.

Types of resources

The Chrome Developer Tools handles many common resources, and you’ll get syntax highlighting in the preview for resources such as JavaScript, HTML, etc. Images will also display in their rendered form (very similar to the resources pane).

WebSockets

The Network panel has the ability to display the contents of a request made using the WebSocket protocol.

Viewing websockets in the network panel

Clicking on such a request displays a new tab named “Frames”, listing the frames sent and received.

Resource Context Menu

Network panel context menu

Right-clicking a particular resource within the Network panel brings up the resource context menu. There are a few options including:

  • Clearing the browser cache and cookies to force the browser to download resources from the server.
  • Copying the full URL of a resource to your clipboard.
  • Copying the request and response headers of a resource is useful for debugging. The request headers often include things like the user-agent, the accepted languages the client supports, and possibly cookies. Response headers can include HTTP status codes, content lengths and server information.
  • Viewing the associated HAR data, which I cover below.

HAR

Performance becomes an important issue, as your site’s traffic increases.

HAR stands for HTTP Archive, a JSON-based format that contains a list of resources loaded in the page. The format contains nodes such as:

  • creator – information about the client that generated the HAR. For example, Chrome’s dev tools generates a HAR with creator.name set to “WebInspector”.
  • pages – contains a list of pages associated with the capture. Along with the page title, you’ll also get a pageTimings object which contains the page load time.
  • entries – contains the bulk of the data, and is likely to be a large array of resources. Each entries element can contain request (cookies, headersSize, HTTPVersion, method, url, queryString and more), response (bodySize, status, headersSize, cookies and more), timings (blocked, connect, dns, receive, send, ssl & wait) and more.

Having all this data is great and opens all sorts of possibilities for automation, in regard to page performance. Once you possess HAR data, you can use an easy-to-use viewer called HAR Viewer.

viewing HAR data

You can also view header information with each resource:

viewing HAR data headers

Console Panel

The Console Panel allows you to execute JavaScript code within the context of the currently loaded page. The console isn’t ideal for writing large amounts of code, but it is excellent for small snippets. The following screenshot shows the Console Panel:

console panel diagram

The console is excellent for small snippets.

  1. console.log writes the provided values to the console. There’s no need to concatenate values together; console.log supports multiple arguments of different types and writes each of them to the console. When you execute a command, you see the output in the immediately following line.
  2. You may occasionally see “[native code]” when attempting to view a native function’s definition.
  3. You can write functions and execute them. Be aware that pressing the enter key executes the code you typed into the console instead of inserting a new line. Hold shift+enter to insert a new line.
  4. An example of an array. Notice the colors are different for different types of values.
  5. An example of a large array with 303 elements. Notice how the elements are grouped together; this prevents the console from being flooded with large amounts of data.
  6. $0 is a shortcut to return the currently selected element in the Elements panel. Right-clicking on an element in the console gives you an option to view that element in the Elements panel.
  7. Errors stand out from other console entries thanks to their red color and icon.
  8. We can access anything in the page’s scope, including the window object. In the screenshot, I created a new WebKitCSSMatrix object.
  9. You can also style console entries using CSS.
  10. The console provides code completion. Try typing in “window.” and see the vast amount of properties you can choose from!

UI

On the Network panel, we can filter the resources shown in the console by clicking on the filters at the bottom; this is similar to the filtering on the Console Panel. You can control + click the filters to show more than one type.

being selective in the console

Logged messages can also be styled like so:

  console.log('%cHello', 'color: green; font-weight: bold;');  

Grouping

You can also group console entries by using console.group. The following screenshot shows an example:

grouping in the console

Use console.group() to start a group and console.groupEnd() to end it. Anything written to the console between those delimiters will appear inside the group. Try it for yourself:

  console.group("%cMy Custom Group", "color: green");  console.log("A value part of a group");  console.group("%cA sub-group", "color: blue");  console.log("Sub-group value");  console.groupEnd();  console.log("We're out of the sub-group now");  console.groupEnd();  console.log("%cA message with no grouping", "text-decoration:underline");  

Inspecting

Viewing the contents of an object is simple. For small objects, the console displays an object’s contents in one line. Larger objects, however, appear as a collapsed group, and you can inspect them by clicking on the drop down arrow immediately to the left of the object.

renaming an element

Double clicking an identifier inside an object automatically highlights, making it easy to copy, paste, and edit.

Miscellaneous

Learning keyboard shortcuts and other commands helps you work more efficiently. Here are some examples:

  • console.clear() clears the console; Control + L achieves the same affect.
  • console.assert() throws an error if the passed argument doesn’t resolve to true.
  • console.error() throws a custom error in the console.
  • $0 returns the currently selected element in the Elements panel.
  • Esc toggles the console on any panel other than the Console panel.
shortcuts in the console

Try the following:

  console.clear();  console.assert(0 > 1);  console.error("Error message");  $0 //get the currently selected element  

Until Next Time!

I hope you’ve learned a few new tricks. Please keep in mind that Chrome’s developer tools are always in a state of flux. For example, a ruler tool was implemented shortly after we published the previous tutorial in this mini-series. Be sure to enable them and try it out!

rulers and guides

In the following tutorial, we’ll continue to look at JavaScript and discuss techniques to edit resources after a page has loaded. Thanks for reading!



11 comentarios:

Anónimo dijo...

miur [url=http://cheapbeatsbydrdrestores.com]cheap beats by dre[/url] iymsgx http://cheapbeatsbydrdrestores.com kiev [url=http://cheapbeatsbydredreshops.com]dr dre beats[/url] ajofaf http://cheapbeatsbydredreshops.com zrtzc [url=http://beatsbydrdreaustraliasale.com]beats by dre[/url] gdjwrr http://beatsbydrdreaustraliasale.com wzqep [url=http://beatsbydrdresaleaustralia.com]beats by dre sale[/url] wwopq http://beatsbydrdresaleaustralia.com mby [url=http://cheapbeatsheadphonesaustralia.com]beats by dre[/url] fufrr http://cheapbeatsheadphonesaustralia.com sfv

Anónimo dijo...

ttvcw [url=http://drdrebeatsuksales.co.uk]dr dre beats[/url] zdwzd http://drdrebeatsuksales.co.uk fzagt [url=http://beatsbydreukforsales.co.uk]beats by dre[/url] hruvw http://beatsbydreukforsales.co.uk kgoiv [url=http://cheapdrebeatsuksale.co.uk]cheap beats by dre[/url] jqbub http://cheapdrebeatsuksale.co.uk llycw [url=http://cheapestbeatsheadphones.co.uk]beats outlet[/url] bbzpf http://cheapestbeatsheadphones.co.uk qwzgg [url=http://cheapestdrebeats.co.uk]beats by dre[/url] vlhyj http://cheapestdrebeats.co.uk kfrqb [url=http://cheapdrebeatssale.co.uk]beats by dre uk[/url] pampb http://cheapdrebeatssale.co.uk hmqz

Anónimo dijo...

pdrio [url=http://beatsbydreheadsetdiscount.co.uk]beats by dre[/url] pkhrq http://beatsbydreheadsetdiscount.co.uk bofuh [url=http://beatsdrdreheadphonesforsale.co.uk]beats headphones[/url] vooai http://beatsdrdreheadphonesforsale.co.uk wwlbn [url=http://discountbeatsheadphone.co.uk]dr dre beats[/url] gcqbl http://discountbeatsheadphone.co.uk pbaka [url=http://drdrebeatsukstores.co.uk]beats by dre[/url] jemom http://drdrebeatsukstores.co.uk ddivg [url=http://newbeatsbydreheadphonesuk.co.uk]beats by dre[/url] nnifo http://newbeatsbydreheadphonesuk.co.uk obnlz [url=http://newdrdreheadphonesslae.co.uk]beats by dre uk[/url] hqueq http://newdrdreheadphonesslae.co.uk qygd

Anónimo dijo...

dwcdu [url=http://retrojordansshoesonline.com]jordan shoes[/url] mhpqq http://retrojordansshoesonline.com nbnhm [url=http://discountjordanretro11.com]cheap jordan shoes[/url] yjqye http://discountjordanretro11.com ehdzq [url=http://jordan11concordonlinesale.com]jordan 11[/url] btueq http://jordan11concordonlinesale.com gwwtg [url=http://newairjordan11shoes.com]air jordan[/url] iwjuh http://newairjordan11shoes.com sjqzw [url=http://airjordan11concordshoes.com]jordan shoes[/url] fjfqy http://airjordan11concordshoes.com qldgk [url=http://jordanscheapformen.com]cheap jordans[/url] gqxza http://jordanscheapformen.com oxvx

Anónimo dijo...

gkbqi [url=http://newestredsoleshoes.com]red bottom shoes[/url] tvkztr http://newestredsoleshoes.com xrcph [url=http://redbottomsoutletsale.com]red bottoms[/url] cnprvl http://redbottomsoutletsale.com oiabk [url=http://cheaplouboutinoutletforwomen.com]christian louboutin[/url] pjrha http://cheaplouboutinoutletforwomen.com rndtm [url=http://newchristianlouboutinssale.com]christian louboutin outlet[/url] pltzo http://newchristianlouboutinssale.com bkfwr vrpyx [url=http://bestchristianlouboutinstore.com]christian louboutin sale[/url] cdeww http://bestchristianlouboutinstore.com oolvy [url=http://louboutinonlinestores.com]christian louboutin outlet[/url] qfyki http://louboutinonlinestores.com grpbh

Anónimo dijo...

bjqzs [url=http://redbottomshoesshops.com]Red Bottoms[/url] zgmenm http://redbottomshoesshops.com htmnp [url=http://redbottomheelsstores.com]Red Bottom Shoes[/url] qojhpd http://redbottomheelsstores.com vkpfo [url=http://christianlouboutinheelsshops.com]christian louboutin shoes[/url] woxco http://christianlouboutinheelsshops.com ieqcn [url=http://christianlouboutinonlinesale.com]christian louboutin sale[/url] psuub http://christianlouboutinonlinesale.com izolq [url=http://bestchristianlouboutinstore.com]cheap christian louboutin[/url] tvblm http://bestchristianlouboutinstore.com khfkr [url=http://louboutinonlinestores.com]christian louboutin sale[/url] cjkzo http://louboutinonlinestores.com bhhki

Anónimo dijo...

pcfkz [url=http://christianlouboutinboutique.fr]christian louboutin[/url] ureoa http://christianlouboutinboutique.fr lahcz rdmzk [url=http://soldeslouboutinpascher.fr]chaussures pas cher[/url] zwbsf http://soldeslouboutinpascher.fr icpsi pisaw [url=http://louboutinpaschersolde.fr]chaussure louboutin[/url] mztkz http://louboutinpaschersolde.fr icupa mnnqn [url=http://christianlouboutinfrancesolde.fr]chaussures louboutin[/url] ymgcz http://christianlouboutinfrancesolde.fr sltfa

Anónimo dijo...

zgq [url=http://www.canadagooseparka-shop.com]Canada Goose[/url] frc http://www.canadagooseparka-shop.com knz [url=http://www.canadagooseparkaok.com]Canada Goose[/url] qms http://www.canadagooseparkaok.com gzb [url=http://www.gocanadagoosesoutlet-ca.com]Canada Goose Parka[/url] nyt http://www.gocanadagoosesoutlet-ca.com qkx [url=http://www.okcanadagoosesfactory.com]Canada Goose Sale[/url] qng http://www.okcanadagoosesfactory.com fuw [url=http://www.sale-canadagooseoutlet.ca]Canada Goose Outlet[/url] shm http://www.sale-canadagooseoutlet.ca eum http://www.lillilassmann.at/node/69#comment-197793
http://bbs.hyx99.com/uhome/space.php?uid=19227&do=blog&id=40409
http://jancada.com/content/Income-Make-Money-Payano-Tiptopwebsitecom#comment-52283
http://www.teamgod.net/members/johnallim1
http://rappan.sakura.ne.jp/blog/2012/07/20-203152.php#comments

Anónimo dijo...

bdu [url=http://www.hollisteroutletnamee.com]Hollister Clothing[/url] eua http://www.hollisteroutletnamee.com apm [url=http://www.hollisterjeansname.com]Hollister Jeans[/url] avk http://www.hollisterjeansname.com pcg [url=http://www.hollisteroutletname.com]Hollister Clothing[/url] wlb http://www.hollisteroutletname.com tdi [url=http://www.hollisterjeanswe.com]Hollister Outlet[/url] pbl http://www.hollisterjeanswe.com qhl [url=http://www.cheapbeatsbydrename.com]Hollister Outlet[/url] qgo http://www.cheapbeatsbydrename.com ewi [url=http://www.celineluggagetotediscount.com]Celine Bag[/url] ukr http://www.celineluggagetotediscount.com udt [url=http://www.celinehandbagsdiscount.com]Celine Bags[/url] bke http://www.celinehandbagsdiscount.com ijh

Anónimo dijo...

qbt [url=http://www.celineluggagetotediscount.com]Celine Handbags[/url] yay http://www.celineluggagetotediscount.com jmh [url=http://www.celinehandbagsdiscount.com]Celine Handbags[/url] ymy http://www.celinehandbagsdiscount.com mqf [url=http://www.celinebagxiao.com]Celine Handbags[/url] dtr http://www.celinebagxiao.com gzt [url=http://www.celinehandbagswe.com]Celine Bag[/url] uzx http://www.celinehandbagswe.com rpf [url=http://www.drdrebeatsname.com]Cheap Beats By Dre[/url] aet http://www.drdrebeatsname.com wfu [url=http://www.hollisteroutletnamee.com]Hollister Outlet[/url] hrl http://www.hollisteroutletnamee.com hqy [url=http://www.hollisterjeansname.com]Hollister Jeans[/url] nvn http://www.hollisterjeansname.com dia

Anónimo dijo...

rru [url=http://www.celineluggagetotediscount.com]Celine Handbags[/url] nej http://www.celineluggagetotediscount.com bjv [url=http://www.celinehandbagsdiscount.com]Celine Bag[/url] qpj http://www.celinehandbagsdiscount.com joc [url=http://www.celinebagxiao.com]Celine Bag[/url] emo http://www.celinebagxiao.com lzb [url=http://louisvuittonsaleky.com]Louis Vuitton Sale[/url] yfg http://louisvuittonsaleky.com ppp [url=http://louisvuittonoutletonlineky.com]Louis Vuitton Handbags[/url] cnl http://louisvuittonoutletonlineky.com loy [url=http://louisvuittonhandbagsoutletky.com]Louis Vuitton Outle[/url] yzb http://louisvuittonhandbagsoutletky.com oyl [url=http://cheapbeatsbydreky.com]Beats By Dre[/url] cdl http://cheapbeatsbydreky.com rxn