未加星标

Optimising the front end for thebrowser

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Optimisation is all about speed and satisfaction.

For the User Experience (UX) we want our front end to deliver a fast loading and performant web page. And for the Developer Experience (DX) we want the front end to be fast, easy and exemplary.

This not only gives us happy users and happy developers, but also significantly higher SEO rankings thanks to Google’s bias towards optimisation.

If you’ve been spending a lot of time improving your site’s Google Pagespeed Insights score then this will hopefully shed light on what it all actually means and the plethora of strategies we have to optimise our front end.

Background

Recently my whole team got a chance to spend some time spiking out our proposed upgrade to our codebase, potentially using React. This really got me thinking about how we should build our front end. Pretty quickly I realised that the browser would be a large factor in our approach and equally large bottleneck in our knowledge.

Approach Firstly

We can’t control the browser or do much to change the way that it behaves, but we can understand how it works so that we can optimise the payload we deliver.

Luckily, the fundamentals of browser behaviour are pretty stable, well documented and unlikely to change significantly for a long time.

So this at least gives us a goal to aim for.

Second

Code, stack, architecture and patterns on the other hand are something we can control. They’re more flexible, change at a more rapid pace and provide us with more options at our end.

Therefore

I decided to work from the outside in, figuring out what the end result of our code should be, and then form an opinion on writing that code. In this first blog we’re going to focus on all we need to know about the browser.

What the browserdoes

Let’s build up some knowledge. Here’s some trivial HTML we’ll expect our browser to run.

<!DOCTYPE html> <html> <head> <title>The "Click the button" page</title> <meta charset="UTF-8"> <link rel="stylesheet" href="styles.css" /> </head> <body> <h1> Click the button. </h1> <button type="button">Click me</button> <script> var button = document.querySelector("button"); button.style.fontWeight = "bold"; button.addEventListener("click", function () { alert("Well done."); }); </script> </body> </html> How the browser renders thepage

When the browser receives our HTML it parses it, breaking it down in to a vocabulary it understands, which is kept consistent in all browsers thanks to the html5 DOM Specification . It then runs through a series of steps to construct and render the page. Here’s the very high level overview.

1 - Use the HTML to create the Document Object Model ( DOM ).

2 - Use the CSS to create the CSS Object Model ( CSSOM ).

3 - Execute the Scripts on the DOM and CSSOM.

4 - Combine the DOM and CSSOM to form the Render Tree .

5 - Use the Render Tree to Layout the size and position of all elements.

6 - Paint in all the pixels.


Optimising the front end for thebrowser
Step one ― HTML

The browser starts reading the markup from top to bottom and uses it to create the DOM by breaking it down in to Nodes.


Optimising the front end for thebrowser
HTML delivery optimisation strategies Styles at the top and scripts at the bottom

While there are exceptions and nuances to this rule, the general idea is to load styles as early as possible and scripts as late as possible. The reason for this is that scripts require the HTML and CSS to have finished parsing before they execute, therefore we put styles up high so they have ample time to compute before we compile and execute our scripts at the bottom.

Further on we investigate how to tweak this while optimising.

Minification and compression

This applies to all content we’re delivering, including HTML, CSS, javascript, images and other assets.

Minification removes any redundant characters, including whitespace, comments, extra semicolons, etc.

Compression, such as GZip, replaces data in code or assets that is repeated by instead assigning it a pointer back to the original instance. Massively compressing the size of downloads and instead relying on the client to unpack the files.

By doing both you could potentially thin your payload down by 80 or 90%. E.g. Save 87% on bootstrap alone .

Accessibility

While this won’t make your page download any faster it will drastically increase the satisfaction of impaired users. Make sure to provide for everyone! Use aria labels on elements, provide alt text on images and all that other nice stuff .

Use tools like WAVE to identify where you could improve with accessibility.

Step two ― CSS

When it finds any style related Nodes, i.e. external, internal or inline styles, it stops rendering the DOM and uses these Nodes to create the CSSOM. That’s why they call CSS “Render Blocking” . Here are some pros and cons of the different types of styles.

// External stylesheet <link rel="stylesheet" href="styles.css"> // Internal styles <style> h1 { font-size: 18px; } </style> // Inline styles <button style="background-color: blue;">Click me</button>

The CSSOM nodes get created just like the DOM nodes, later they will be combined but here’s what they look like for now.


Optimising the front end for thebrowser

Construction of the CSSOM blocks the rendering of the page so we want to load styles as early as possible in the tree, make them as lightweight as possible, and defer loading of them where efficient.

CSS delivery optimisation strategies Use media attributes .

Media attributes specify a condition that has to be met for the styles to load, e.g. Is there a max or min resolution? Is it for a screen reader?

Desktops are very powerful but mobile devices aren’t, so we want to give them the lightest payload possible. We could hypothetically deliver only the mobile styles first, then put a media conditional on desktop styling, while this won’t stop it downloading it will stop it blocking loading of our page and using up valuable resources.

// This css will block rendering of the page until it is downloaded and processed. <link rel="stylesheet" href="mobile-styles.css"> // This css will download in the backgro

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: CSSHTMLReactJavaScriptHTML5SEOJavaUT
分页:12
转载请注明
本文标题:Optimising the front end for thebrowser
本站链接:http://www.codesec.net/view/532562.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(56)