Trimming the fat: reducing http requests to keep us on the mobile first diet

In an attempt to follow the ‘mobile first’ ethos, reducing the amount of loaded assets on smaller devices is high on the priority list.

Using media queries we can effectively control the loading of things like background images, but what about the CSS itself? After all, it seems wasteful to load CSS specifically for desktops when viewing a site on a mobile device.

A common misconception of media queries is that a device only downloads the CSS of a matched query (when using the query in the media attribute of a link element).

This is not the case.

All CSS files are downloaded regardless, though the CSS is not applied until you match the requirements of said query.

I feel like I’m being slightly cheated. In an ideal world I’d like the CSS to only be downloaded when they are needed, based upon matching the requirement of the media query. So how can I watch my http requests and keep my slimming mobile figure?

JavaScript to the rescue, or JavaScript polyfill time.

We could easily use a bit of JavaScript to achieve this, the fine fellows over at the BBC use the following on their blog:

<script type="text/javascript">/*<![CDATA[*/
 (function(){
  var importStatement = "";
  switch (true){
   case ($dc.screenWidth > 639):
    importStatement = '@import url("http://some.tablet.css") screen and (min-width: 640px);' + importStatement;
   default:
   importStatement = '@import url("http://some.compact.css") screen and (max-width: 639px);' + importStatement;
   break;
 }
 document.write('<style type="text/css">'+importStatement+'</style>');
 })();
/*]]>*/</script>

If you are willing to travel the JavaScript route then something like this works fine. However, when you add a content management system on top of this it gets more complicated.

Dealing with Drupal

Our previous example was very basic, aren’t all examples?! When we consider taking this approach in Drupal we come across a few hurdles.

Drupal’s CSS optimisation aggregates and compress theme, core and contrib CSS together into a greatly reduced number of files. This is great, not least because there seems to be a huge amount of core and contrib CSS in Drupal.

But as soon as we do this we loose the ability to control our CSS for different devices with a little more finesse.

A module in the making?

Ideally there would be a module which would do the following:

  • Show a list of all CSS which is put out by the system, theme, core and contrib, preferably with a description as to its purpose.

Do you know where/what all of the CSS Drupal spits out is? Probably not. I’ve never sat down and gone through all of these files and figured out exactly what they do, and indeed if I really want them. On more than one occasion I’ve found myself writing CSS to override stuff from them. This shouldn’t be the case.

  • Allow selection/deselection of said CSS, removing what you don’t want.

An ability I’ve always longed for, yet it’s never been that important to warrant the time to actually figure out how to do properly (it’s generally been quicker to just override it in the theme). But, this is the ultimate, complete control on what styles are used.

Traditionally you’d simply override an unwanted style sheet in the theme info file by using an empty declaration with the same name. In some themes, most notably Omega, this feature is now offered as a theme setting. It allows you to switch off core and theme CSS.

  • Specify groups which are separately optimised.

Having Drupal optimise our CSS is a good thing, but as stated, it kind of walks all over our plans to deliver style sheets as needed rather than the whole lot together. Having the ability to set specific groups would be great. I could then lump all core/contrib stuff together and then have the needed separate—compressed—files for my ultimate goal!

I hereby name this module CSS Admin.

Pragmatically thinking

We’ve specified a useful CSS administration module there, and so in a modular mind set the features left to finish our solution should probably be rolled into another module which is just dependant on CSS Admin.

We need the ability to:

  • Write some JavaScript into the head of the document that tests the screen width (or other variables) and then loads an appropriate CSS file.

Perhaps we could call this CSS Admin – Media Query Loader.

That’s it. Simple!

Glory awaits

Perhaps deferring the loading of CSS files is getting too picky, but I’d don’t think so. Granted, I’m probably missing a few angles of reasoning from this, but I think the core message is sound—we should be doing everything to honour the mobile first ethos by asking these kinds of questions and seeking out their solutions.

I’ve no doubt creating those modules is possible, I’ve worked with some excellent Drupal developers and I’m sure there are plenty more out there. With any luck we might get to look at this during this project and I’ll be able to follow up with a solution post, but for now I’ll make do with seeding the thought amongst you all.

Knowing my luck, in true Drupal fashion, a module already exists for that! I hope it does, but I’ve yet to come across it.

Implementing a mobile first responsive design

About a month ago we had just created the basis for our responsive design, see Hur bygger man en sajt som fungerar både på mobil, tablet och desktop – på respektive form factors villkor?. Time to give an update on the progress.

The areas that we have been working on are markup & semantics, http requests, amount of loaded KB and screen size adjustment.

On the markup and semantics front we have made additional switches from a traditional <div> structure to one of HTML5 tags (we where happy that the <time> tag was re-introduced). Our timestamps are printed in a humanized format, see timeago – a jQuery plugin.

We also added support for schema.org and microformats.org/ on images and author information. Tests width Google Rich Snippets Testing Tool indicate that it will give added value in search results. Schema.org and RDF also has a draft pending that seems interesting.

With regards to keeping the amount of http requests down we use regular methods like sprites (depending on how big you have to make the sprite, it might actually also save a good deal on total KB size) and media querys combined with a script loader to only load relevant CSS styles and JS per device. @bengoodyear created a script for loading the share buttons only when the user indicates that he or she wants to use them. An additional nice feature is that on mobile you will be able to click the share buttons individually, thus removing the need to load them all.

Images has the biggest impact when considering amount of loaded KB. This is the reason why we load a small thumbnail (16:9) as the default image. If the screen size is big enough a bigger image will load automatically. To avoid a “jump” when the bigger image is loaded we added the size of the bigger image in the CSS, so what the end user might be able to see (depending on loading speeds) is that the image goes from being blurry to sharp. The right CSS for our set of four layout widths is applied with media query.

We built a custom solution as we have our home-brew Image Management System that we connected to Drupal. If you are using Drupals own image field, you might want to check out Responsive images module.

With regards to the amount of loaded CSS files I tried out Responder module – a good solution if you want to add a responsive design to a current theme. In our case since we are using Omega Responsive HTML5 theme and have added our own solutions it does not look like a module we will add.

The markup generated by Drupal and its modules is not the best, there is a lot that does not need to be there, thus adding both unnecessary KB weight, accessibility and styling issues. This is something we are still working on, and I assume we will have to make some kind of compromise.

To limit the amount of markup loaded we first add the navigation as a <ul> <li>- list, and then with that as a base use javascript to create a <select> pulldown-menu to be used in the mobile version. This way it is easier to select a link in the mobile version, and the screen is not filled up with a list of links.

Responsive videos is also a bit of a challenge. In our case we need to support Arenan, Youtube and Vimeo.

There is a new version of Arenan being launched next year, and we are working with the implementation of that one. A big improvement will be made on how it is embedded. It will now be added as a background in a div, that is replaced by a Flash player when the user clicks the play button. Sorry, no HTML5 video support yet due to rights management issues.

This means that we can also get a small size image for each video/audio clip, and re-size it the same way we re-size images.

Currently we are experiencing problems with the size of the video, when it actually starts playing. This is something we are working on together with the team building the new version of Arenan.

Right now we are looking at using Fitvids module on all iframes to add the scaling ability to Youtube, Vimeo and other embedded materials. Might be that iframe as a target tag is to broad ;) We will have to test and see, would have been nice if Vimeo and Youtube added a class name to their embed code, so that you would be able to target them.

With regards to the set of four widths we are using, the styling has progressed nicely. It takes a while to start thinking mobile first, but when you get a hang on it it comes naturally. The interesting questions that arise then are what kind of differences do you want on the different devices? Do you want bigger images if you are using a tablet? These are the kind of questions we are thinking about, and will test on users to see what the final version will look like.

What would be a good set of page widths with the current set of screen resolutions?

We have decided to work with responsive design and are in the process of setting the widths of the pages. I took a look at the screen resolutions our visitors are using at the moment.

The setup we are looking at right now would have four brackets:

  • Less than 740px (mobile or small tablet) – would see a fluid page
  • Less than 980px (tablets or notebooks) – would see a fixed-width page of 740px
  • Less than 1220px (smaller screens) – would see a fixed-width page of 980px
  • Wider than 1220px (big screens) – would see a fixed-width page of 1220px

When the current number of screen resolutions is applied to these brackets this is the outcome:

  • 2,5 % = Less than 740px
  • 2,2 % = Less than 980px
  • 16 % = Less than 1220px
  • 80 % = Wider than 1220px

These numbers represent 94 % of all visits to our site, the remaining 6 % are spread over 2,300 different screen resolutions.

If we would have a wider page for bigger screens, the result would be:

  • 2,5 % = Less than740px
  • 2,2 % = Less than 980px
  • 49 % = Less than 1360px – would see a fixed-width page of 980px
  • 45 % = Wider than 1360px – would see a fixed-width page of 1360px

It’s also possible to move the width bracket for smaller screen sizes, but that immediately creates a cascade effect. The tablet user is not happy when grouped with the mobile users, and someone using a screen resolution of 1192px is not happy being grouped with the users of a 768px screen.

Additionally, the numbers don’t tell the full story as they don’t take into account that a user might display the browser window in a smaller size than the screen resolution they have.

What brackets are you using, and any particular reasons why you chose them?

Welcome to SYND (#ylesynd)

”You must be raving mad to even try to do such a thing”, she said. ”It is simply impossible to change so many things simultaneously.”

This was one reaction to what we are now attempting to do now. We are trying to rethink how we do web here at the Swedish-language division of the Finnish Public Broadcasting Company (Yle), or Svenska Yle, as we are generally known. And the list of things we want to reform isn’t short: the technical platform, the concept, the look, the organization of people who contribute, and the way we do our journalism. It is also true that we have small resources and huge ambitions, but hey, would it be worth it if it were easy?

So this is just an initial note of welcome to this blog. We will post here to try to open up and document these processes publicly, mostly because we are believers in not only the principles for open source software but also those in open design. This will be a record for us, but also for others who will attempt to do similar things in the future. One clear advantage of being open is that the quality of your documentation can increase dramatically.

SYND refers to one of the strands in this big re-think. It is the one where we are building a new content management system for Svenska Yle—one that will be based on the open source system Drupal.  This blog will mostly follow the process of building that platform, although we will perhaps do some occasional detours into other larger questions relating to the reform, as well as other matters that keep us busy here at svenska.yle.fi.

The planning of this web reform started already last spring and we now have a team of six developers who have, since last Monday, officially gotten their hands dirty on the project. The first phase lasts five sprints (ten weeks) and we have scheduled our first release for March 2012.

This blog will be used to showcase the work that developers and designers are doing, as well as to discuss business-related questions that we have been working on. Personally, I can pledge to post entries that try to shed a light on our main business goals and how we are thinking about our “roof concept.” Furthermore, you should expect some posts on how we are organizing the process and thoughts about why we chose open source and Drupal from both technical and business perspectives. Expect also to see lots of posts about Drupal in general and some about meta data, linked data and such things.

It is true: this is a huge thing for us and I think I can guarantee the ride will not be smooth, at least all the time. Please do join us!

P.S.
SYND stands for “Svenska Yles nya Drupal-plattform,” which translates to the new Drupal platform of Svenska Yle. The acronym “SYND” means “sin” in English.

We will of course also be reporting individually about the project. These are the people to follow on the dev team:

Mårten Seiplax, Project Owner & AD @mseiplax
Tomi Mikola [Mearra], Project Manager @tomi_mearra
Christian Wikström, Developer @cwiks
Alexander Granholm, Developer @granholm
Ben Goodyear [Exove], Developer (Themer) @bengoodyear @typekitlove
Lauri Kolehmainen [Exove], Developer @dotsi

Some other key people related to the project:
Mikael Hindsberg, Concept Designer @mickhinds
Kristoffer Söderlund, Technical Producer @soderkr
Jonas Jungar, Svenska Yle News organisation @jonasjungar
Sami Kallinen, Head of programmes, Svenska.yle.fi @sakalli