OptimizationResponsively Retrofitting An Existing Site With RWD Retrofit

Advertisement

Since the introduction of the iPhone in 2007, touchscreen mobile devices have exploded in popularity. They have introduced new problems, new solutions, new interactions, new ways of thinking and, of course, new costs to our clients.

The most important question on everyone’s mind — clients and developers alike — is, “How can we provide a great Web experience to our users on mobile?”

It All Started With iPhone

The mobile Web was rarely considered before the iPhone (mostly because mobile browsers were so bad). But after the phone’s release, clients jumped on the bandwagon and were clamouring for “mobile-friendly” websites so that they didn’t appear to be behind the times. However, mobile-specific — or, worse, iPhone-only — websites also introduced new issues and frustrations for users, often “breaking” the Web with broken links, infinite redirects and missing content.

Fast forward three years, when we find Ethan Marcotte writing about his revolutionary principle of “responsive Web design” (RWD), which changed the Web — and the way Web developers think — forever.

We’ve told our clients about all of the benefits of RWD and about why they shouldn’t build a separate mobile website. The world should have lived happily ever after. But clients who had already spent thousands of dollars on a fixed-width desktop website were less than pleased.

create-first-iphone-app-500
Can your clients afford a mobile-first responsive build? (Image Source)

Then, around the time that Marcotte’s article was published, the iPad was released. Clients who had already gone down the path of a separate mobile website then wanted their iPad users to get their current desktop website. So, we did it — with user-agent sniffing, once again straying further from the RWD methodology, even though we knew we shouldn’t.

Our fear of not being future-friendly was further exposed when Android tablets hit the market. Because there is no single unique identifier in their user agents to identify them as tablets, all Android devices, regardless of size, are generally served mobile websites, which, from the forums I frequent, frustrates most users. There are some services for detecting Android tablets, as outlined in “Server-Side Device Detection: History, Benefits and How-To,” however, as an Android user, I regularly see more mobile than desktop websites.

In fact, the issue got so bad that in Android 4.0 (Ice Cream Sandwich), Google added a “Request desktop site” option to its browser. But depending on how a website’s detection is written, it doesn’t always work, and you still have to suffer the pain of loading the mobile website first.

So, we have a few issues:

  1. Clients with an existing “desktop” website can’t afford to rebuild from scratch using a responsive methodology.
  2. They still need a more optimized experience for mobile devices.
  3. Rightly or wrongly, they still want tablets to load their desktop website.

What’s The Solution?

RWD Retrofit is a small, vanilla JavaScript “plugin” that gives developers the option to allow an existing “desktop” website to coexist with a new responsive version, with minimal modification to the original website.

It implements dynamic viewport switching to ensure that the correct version of the website is shown according to the viewport’s width (both on loading and on resizing or orientation change), and it provides an object that contains the media queries required for responding to the breakpoints with JavaScript.

I wrote it at Izilla, which helps clients to address the issues mentioned above, and I have already used it in a number of projects.

Although I’m obviously biased, I think it’s a great technique to help you quickly and efficiently improve a client’s mobile presence in a cost-effective way.

Keep in mind that RWD Retrofit isn’t a magic bullet that will instantly transform your website into a responsive website out of the box. You still need to build the responsive version.

Is It The Only Solution?

Absolutely not. Developers have three other options available to them: a dedicated mobile website, a desktop-down responsive reskinning, and a complete rebuild with mobile-first responsiveness.

Let’s look quickly at the pros and cons of each, and of RWD Retrofit.

A Dedicated Mobile Website

Pros:

  • Because it’s completely separate from the existing website, you essentially have a blank canvas to work on — dealing with a legacy code base is not really an issue.
  • Supporting fewer devices results in a more focused, streamlined implementation, because the “minimum requirements” of the website are easier to define and develop for.
  • By analyzing your users’ habits and data, you’re better able to target the user’s intent and context.
  • The cost to develop is reasonable.

Cons:

  • A separate code base could result in more expensive maintenance and content costs.
  • Supporting a subset of devices is not future-friendly.
  • User-agent detection is fragile because it can, and will, be spoofed.
  • Be wary of assumptions of user intent based on device alone. A lot of “mobile” usage is done at home.
  • There is the possibility of broken links and missing content for existing desktop pages that don’t exist on the mobile website.

Dedicated mobile websites are suitable for when content parity is not an issue, or you’re absolutely certain of the user’s goals on mobile, or the client wants a more app-like experience. I have used them successfully for targeted micro-sites that are separate from the main website.

A Desktop-Down Responsive Reskinning

Pros:

  • Uses your existing code base, making for more efficient maintenance and content updates.
  • Supports a wide variety of devices based on screen size.

Cons:

  • It might not suit your existing code base. Success relies largely on using CSS3 to style, so adapting old websites that primarily use fixed-width CSS background images for presentation might be difficult.
  • Performance overhead and inefficiencies are incurred from existing redundant code on breakpoints that don’t match.
  • Requires extensive retesting of a previously functional desktop website.
  • Cost to develop is moderate to high, depending on the code base.

This approach is better suited to simple websites that already use modern Web standards as their underlying technology. Marcotte used this approach in his Responsive Web Design book, but that was before “mobile first” became the favored approach.

A Mobile-First, Responsive Rebuild

Pros:

  • A completely fresh, considered, optimized and progressively enhanced code base.
  • A consistent, seamless experience for all users.
  • Supports the widest variety of devices based on screen size and features, including large desktops and TVs.
  • You’re future-friendly!
  • Your work will hopefully be shared and admired in social media.

Cons:

  • Planning, prototyping, development and testing could require substantial effort.
  • The cost to develop is very high.

This will hopefully become the de facto standard for all new projects and redesigns. If approached correctly, your website should support as many different kinds of devices as possible for the foreseeable future, including Android-powered, Internet-enabled refrige-toasters.

RWD Retrofit

Pros:

  • Uses your existing code base, making for more efficient maintenance and content updates.
  • A completely separate style sheet ensures that the retrofit will not interfere with existing desktop styles.
  • Minimal overhead or changes to existing website.
  • Supports a wide variety of devices based on screen size.
  • The cost to develop is reasonable.

Cons:

  • The existing markup might not be flexible or optimized enough to be repurposed for small screens.
  • Depending on how far the retrofit is taken, the transition from the largest responsive breakpoint to the desktop breakpoint could potentially be disjointed for users, but that would only really affect users who resize their browser’s window.
  • On mobile devices, the desktop version might not initially be zoomed out fully on load or on rotation.

A retrofit is a good compromise of ideas and techniques, taking the best bits from the various other solutions to provide a cost-effective solution to your existing clients.

Which Is The Best Solution?

The “best” depends on the context and budget. Obviously, the best solution you could give to your clients would be a complete mobile-first responsive build. That would provide the optimal experience for users, using a single code base that’s cost-effective to maintain. I absolutely urge you to recommend this approach if feasible.

However, many of my clients can’t afford to do this, and because their websites predate the use of CSS3 for things such as rounded corners and gradients, the “best” solution for them is to use RWD Retrofit.

RWD Retrofit is also useful for both clients and developers who are in a transitional phase to responsiveness. If your client wants to rebuild using the mobile-first philosophy but can’t afford to just yet, push to retrofit their website. Generally speaking, having some kind of mobile optimization is better than nothing. Also, remember that practice makes perfect. Attempting a small-scale responsive website first will equip you with the skills needed for when you come to do the “real” thing.

How Does RWD Retrofit Work?

Here goes:

  1. When the page starts to load, the script checks to see whether the user is on a mobile device that supports touch events.
  2. If they are, it checks whether you’ve set to override the desktop breakpoint on mobile, and then changes the breakpoint accordingly.
  3. It then checks to see whether you’ve set the viewport width for the desktop version to be anything other than the default of 980 pixels, and sets that accordingly. The article “Viewport Meta Tag For Non-Responsive Design” explains why you may want to do this.
  4. A function is called that compares the width of the document to the desktop media query, and if it’s equal or larger, it alters the viewport to the desktop one.
  5. This same function is called on a resize or orientation change (depending on what the device supports) to see whether it needs to switch over to the mobile viewport, and vice versa.
  6. Finally, it returns the responsive and desktop media queries in a JavaScript object for you to test against in other scripts, so that:
    1. you don’t have to write your media queries in both the markup and JavaScript;
    2. if you did override the desktop breakpoint on mobile devices, you’ll always have the correct media query.

At its core, RWD Retrofit should be thought of as a technique, not a solution. If you don’t plan to change the breakpoint that the desktop website is served at on mobile, or you don’t plan to change the desktop viewport’s width when it is served, or you don’t mind duplicating media queries in your JavaScript, then the RWD Retrofit JavaScript itself isn’t required. The technique will still work, allowing you to achieve similar results.

Implementing RWD Retrofit

Below is the process I use to retrofit a website.

Initial Set-Up

  1. Open the index (i.e. main) page, which has your global <head> section.
  2. Add
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    as one of the first children of <head>.

  3. Find the link to the current desktop style sheet, and change its media attribute to media="all and (min-width: Xpx)", where X is the current width of the desktop website, which will result in something like this:
    <link href="/css/desktop.css" rel="stylesheet" media="all and (min-width: 980px)" />

    If you currently have multiple style sheets at runtime, I strongly recommend that you combine and minify them into one to improve performance.

  4. Go through your JavaScript functions and, for the time being, comment out any that won’t apply to the responsive version of the website. (Again, if you currently have multiple JavaScript files at runtime, I strongly recommend that you combine and minify them into one to improve performance — excluding any jQuery, which is best served from a CDN.)
  5. Create a new responsive style sheet, and link to it with a max-width media query that is 1 pixel less than the desktop one, like so:
    <link href="/css/rwd.css" rel="stylesheet" media="all and (max-width: 979px)" />
    

    If you now open this in a Web browser with a narrow window, you’ll have an unstyled document — the clean slate that we want.

Creating the Responsive Version

Building a responsive style sheet is a massive topic on its own, and because a lot of good resources are already online, I’ll just provide some pointers.

  1. Begin with a mobile-first philosophy. Resize your browser window to a “mobile” size. I actually prefer to go smaller than the standard 320 pixels because feature phones are often forgotten. To help with this, Firefox introduced a pretty cool Responsive Design View mode in version 15. Or you can use the Web Developer Toolbar extension, available for both Firefox and Chrome.
  2. Although undertaking a truly mobile-first responsive rebuild wouldn’t be a best practice, elements in your markup may be suitable for the original desktop website but need to be hidden on smaller screens. Use an accessible method to do this. Chris Coyier rounds up approaches to doing this.
  3. As you develop and write the CSS, increase your browser’s width to see how the new responsive layout reacts and adapts as you approach the desktop’s breakpoint. If you need to insert more media queries, do it directly in the responsive style sheet to keep them nicely contained. (To help you obtain the viewport’s current width to use as a new breakpoint, I’ve written Viewport Genie, which is available as both a bookmarklet and a tiny JavaScript include. You can also use Firefox’s Responsive Design View mode.)
  4. When you’re happy with the basic styling on desktop browsers, start testing some real devices. Things will need adjusting, and issues will arise that don’t appear on the desktop. (Both Safari on iOS and Chrome on Android allow you to use remote debugging from your browser to preview your website on the device. Alternatively, you could use Adobe’s Edge Inspect, which is available for both iOS and Android, or Viljami Salminen’s Remote Preview.)

Implementing RWD Retrofit

Now it’s time to drop in the RWD Retrofit JavaScript and its dependencies.

  1. Download the minified RWD Retrofit from GitHub, and reference it immediately below your style sheets.
  2. Optionally, download a custom build of Modernizr and include that underneath RWD Retrofit. At a minimum, I recommend that your build contain Modernizr.load and Add CSS Classes. You may also want to include other feature tests depending on how you plan to style the responsive version. If your markup doesn’t use any of the HTML5 layout elements, then you won’t need html5shiv. (I recommend combining the two JavaScripts above into one base.min.js resource.)
  3. Download the minified version of the matchMedia polyfill from my fork on GitHub, and include it before your other custom JavaScripts, which should hopefully be immediately before the closing </body> tag to prevent rendering blocking. (The matchMedia polyfill is required for older browsers that don’t support this API natively.)
  4. Download the minified version of Enquire.js, and reference it beneath the matchMedia polyfill. (Enquire.js is a lightweight pure JavaScript library for responding to CSS media queries.)
  5. Give your desktop style sheet’s <link> a class of "rwdretrofit-desktop".
  6. Give your responsive style sheet’s <link> a class of "rwdretrofit-mobile".
  7. Add an optional data-breakpoint-width="X" attribute to the desktop style sheet’s <link>, where X is the pixel width of the desktop breakpoint on mobile devices (for example, 768 for iPads and other large tablets).
  8. Add an optional data-viewport-width="X" attribute to the desktop style sheet’s <link>, where X is the pixel width that the desktop viewport will be set to on mobile devices.
  9. Add an optional data-debug="true" attribute to the desktop style sheet’s <link>, which forces desktop and non-touch-enabled browsers to use RWD Retrofit. (If you’ve specified a custom data-breakpoint-width, ensure that you remove the data-debug attribute before going live, or else your desktop breakpoint will trigger at the wrong size on desktops.)

If you’ve added the optional data- attributes, you will now be able to test these breakpoint and viewport changes on mobile devices, or by setting your browser to emulate touch events. At the time of writing, Chrome is the only browser able to do this through its Developer Tools (in Settings → Overrides).

Because I don’t serve the responsive version to users of IE 8 and below, I also reference the desktop style sheet in a conditional comment beneath the others, like so:

<!--[if lt IE 9 ]>
   <link href="/css/desktop.css" rel="stylesheet" type="text/css" media="all" />
<![endif]-->

If you wanted these users to have a responsive experience, you should be able to use Scott Jehl’s Respond.js media query polyfill instead, although I haven’t tested it.

Modifying Your JavaScript Functions

No doubt, you will have JavaScript functions that should be fired on the desktop version but not on mobile — such as a lightbox script (for example, Jack Moore’s ColorBox).

Keep in mind that there is no one process to achieve this (because everybody writes their JavaScript functions differently), however, here’s how I’d modify a lightbox function:

  1. First, let’s assume that we have one JavaScript file named main.js, in which everything is wrapped in a function that executes when the DOM is fully loaded, whereupon we call ColorBox on all links with a class of lightbox, like so:
    $(document).ready(function() {
       // Call ColorBox on appropriate links
       $('a.lightbox').colorbox();
    });
    
  2. Because the lightbox function can now be called multiple times, let’s instead create a variable that stores the lightbox links for counting and improved performance (because jQuery won’t need to query the DOM each time):
    var $lightboxLinks = $('a.lightbox');
    
  3. We now need to convert the call to ColorBox into a function, so that we can use it whenever the desktop media query is matched. I prefer to create them as methods of a literal object:
    var lightbox = {
       initialize: function() {
          // If the number of links to be ColorBox'd is greater than 0
          if ($lightboxLinks.length > 0) {
             $lightboxLinks.colorbox();
          }
       },
    };
    
  4. We also need to remove any ColorBox’d links for when our responsive retrofit media query is matched. So, we create a function to do that after our initialize method:
    destroy: function() {
          // Remove all traces of ColorBox
          $.colorbox.remove();
          // Remove the click event handlers that ColorBox added
          $lightboxLinks.off('click');
       }
    
  5. Now, we need to add in the conditional tests by using the two media-query objects that RWD Retrofit exposes (rwdRetrofit.desktop and rwdRetrofit.mobile) and Enquire.js:
    // If rwdRetrofit is loaded
    if (rwdRetrofit) {
       // Use enquire.js to test the media queries
       // Test for the desktop media query
       enquire.register(rwdRetrofit.desktop, {
          // If it matches
          match: function() {
             lightbox.initialize();
          },
          // If it doesn't
          unmatch: function() {
             lightbox.destroy();
          }
       // Immediately start testing for the media queries
       }).listen();
    }
    

    (I could have just as easily tested for rwdRetrofit.mobile and reversed the match and unmatch functions, but logically that wouldn’t make as much sense.)

And that’s as simple as it can be to modify existing JavaScripts to work “responsively.” You just need to follow a similar pattern, creating initialize() and destroy() functions for other code that should be selectively used or that should behave differently depending on the media query matched.

But let’s take it further. Because ColorBox isn’t used in our responsive version, we’re punishing our “mobile” users by making them download the script. So, we can use Modernizr’s asynchronous, conditional resource loader to load it only when the desktop breakpoint is matched.

  1. Modify the lightbox.initialize() function to conditionally load the ColorBox script:
    initialize: function() {
       // If the number of links to be ColorBox'd is greater than 0
       if ($lightboxLinks.length > 0) {
          Modernizr.load({
             // Load the minified ColorBox script
             load: '/js/jquery.colorbox-min.js',
             // When it's loaded, apply ColorBox to the links.
             // This will also be triggered if the script has previously been loaded
             complete: function() {
                $lightboxLinks.colorbox();
             }
          });
       }
    }
    
  2. Modify the lightbox.destroy() function to first check whether ColorBox has loaded:
    destroy: function() {
       if (typeof($.colorbox) != 'undefined') {
          // Remove all traces of ColorBox
          $.colorbox.remove();
          // Remove the click event handlers that ColorBox added
          $lightboxLinks.off('click');
       }
    }
    

Putting it all together will give you this:

$(document).ready(function() {
   var $lightboxLinks = $('a.lightbox');

   var lightbox = {
      initialize: function() {
         // If the number of links to be ColorBox'd is greater than 0
         if ($lightboxLinks.length > 0) {
            Modernizr.load({
               // Load the minified ColorBox script
               load: '/js/jquery.colorbox-min.js',
               // When it's loaded, apply ColorBox to the links
               // This will also be triggered if the script has previously been loaded
               complete: function() {
                  $lightboxLinks.colorbox();
               }
            });
         }
      },
      destroy: function() {
         if (typeof($.colorbox) != 'undefined') {
            // Remove all traces of ColorBox
            $.colorbox.remove();
            // Remove the click event handlers that ColorBox added
            $lightboxLinks.off('click');
         }
      }
   };
   
   // If rwdRetrofit is loaded
   if (rwdRetrofit) {
      // Use enquire.js to test the media queries
      // Test for the desktop media query
      enquire.register(rwdRetrofit.desktop, {
         // If it matches
         match: function() {
            lightbox.initialize();
         },
         // If it doesn't
         unmatch: function() {
            lightbox.destroy();
         }
      // Immediately start testing for the media queries
      }).listen();
   }
});

Because modifying the DOM when breakpoints match comes with a performance hit, if an initialize() function creates elements in the DOM that should be removed in the other “version” of the website, you could just hide it in the appropriate style sheet instead, which adds slightly extra bloat to your CSS, but would be more performant and better on the device’s battery, for example.

I’ve created a one-page demo website, and the code for it is on GitHub, too.

The demo goes further than the example above, by adding extra selective JavaScript functions, such as a toggle navigation on small screens and an accessible carousel and tabs.

In Conclusion

If you can’t convince your clients to start from scratch with a mobile-first philosophy, I hope this gives you the knowledge, impetus and tools that you need to retrofit their websites!

(al)

↑ Back to top

Matt Stow is the Senior UI Developer for XO Digital, based in Newcastle, Australia. He specialises in developing responsive websites and apps using Suzi—a mature, Sass and Grunt front-end framework. You can follow Matt on Twitter, check out his website and his GitHub repos, too.

  1. 1

    Excellent article folks! I certainly always try to opt for a mobile first approach if at all possible. As you mention in the article though, sometimes this just isn’t practical.

    2
    • 2

      Look at past traffic and see what makes sense. If your site is more likely to be visited from the desktop then go with it first.

      Blind use of “Mobile First!” as a buzz phrase recently isn’t particularly helpful. Bit like saying “Only use blue links because someone might be red colour blind!”

      0
      • 3

        I get what you’re saying but it’s not really about ‘Buzz Words’, it’s more about a future friendly approach. While a site may get traffic from desktops predominantly now, it doesn’t mean that will be the case in the future. It’s best to be as prepared as you can for all possible contexts.

        5
  2. 4

    Awesome article, Matt! Keep ‘em coming! :-)

    0
  3. 5

    Cool Stuff. Thanks Matt.

    0
  4. 6

    Why does evrybody always compare good RWD to bad mobile websites ?

    Server-side detection coupled with a mobile website, done right, is as future friendly than RWD. It may or may not outperform a RWD website. It depends a lot on your content and users.

    Great agencies trust data, not bloguers who write more than they work.

    1
  5. 7

    Great Article! Perfect timing actually. Content relates to projects I’m starting very soon.

    0
  6. 8

    Excelent Article! Downloading RWD :)

    1
  7. 9

    Thank you Mat for summarising the options in such an easy to follow Pros vs Cons way! And Retrofitting seams like an alternative for those not ready to do a full RWD remake. For those, who still want to start from scratch with a mobile-first philosophy, we are working on a tool to help with the Cons and make it easier and faster. It is a tool for responsive web prototyping called Froont. Have you checked out the demo at http://www.froont.com? Would love to hear your expert opinion on the idea of the tool and I would love to send you a special early invite code, just ask me directly!

    0
    • 10

      Thanks for the kind words Anna :)

      I have seen the Froont demo and signed up in February. I’d love an invite code, it looks really interesting!

      0
    • 11

      Hey,

      Looks really interesting! I’d love an invite please. Thanks.

      0
  8. 12

    This is fantastic! Thanks! A much more affordable approach for my clients who want to convert there sites to a responsive version :)

    -1
  9. 13

    Uff…

    There is something I dont get here:
    Why not seperate different CSS via browser-detection or media-query, each for their unique device’s formfactor?

    Secondly:
    Even you are free to do whatever you want to the CSS in order to built up a RWD site…. mostly the problem will be, that there is structured data and sturdy html-markup comming from a DB handed towards a template engine generating the frontend code.

    So if ya cant get hold on these processes, you cant go and create the structures you need for RWD with Javascript alone…

    2
    • 14

      Daniel, you’re right. My first con for a retrofit was: “The existing markup might not be flexible or optimized enough to be repurposed for small screens.”

      Luckily, with the projects I’ve applied it to so far, the markup was clean & semantic enough to do most of what I wanted. I used JavaScript here and there to make minor modifications as required.

      2
  10. 15

    All solutions such as this should be considered as temporary. Like a plaster cast, 6 weeks max whilst the broken bone heals underneath. They provide an additional layer of complexity which will not make for long term maintainable code.

    5
  11. 16

    While I don’t really understand the coding process and technicalities, I would that mobile-first responsive build, which is clearly the best one, would become more budget friendly as time goes on. It seems like people use their mobile devices more than their desktops these days and I’m sure that trend will continue. In the future, I can see people using their phone for everything and simply attaching some kind of keyboard to them for faster typing.

    0
  12. 17

    Peter McLachlan

    May 17, 2013 2:24 pm

    Hi Matt!

    Great article! I’m curious if you’ve had a look at Mobify.js (www.mobifyjs.com)? Mobify.js allows you to apply a responsive template to a website client side. Mobify.js handles all the detection magic, exposes an easy to use content-query API and templating engine to repackage content, and performs a lot of performance optimizations that keep unwanted scripts & images from loading.

    The Mobify.js platform is open source & free to use.

    If you’re supporting business customers, Mobify provides a complete PaaS offering that’s being used by top retailers and Fortune 500 companies.

    0
  13. 18

    This seems very nice, but doesn’t seem necessary to me. Why not just have the site load a different CSS file when it detects that Ida being viewed on a mobile device? Who cares about the existing markup? Even if a site is “fixed” you can just override that in the new style sheet, and have if optimized for mobile. Or am I missing something??

    0
  14. 19

    Interesting article. Thank you. I guess I’m still not sure if you have access to the markup and the backend why RWD Retrofit is necessary over just using media queries and a separate stylesheet and what advantages it provides if you aren’t dealing with massive background images, images used as shims, rounded corners, and other effluvia.

    0

Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or else it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!

↑ Back to top