Download - RESS – Responsive Webdesign and Server Side Components

Transcript
Page 1: RESS – Responsive Webdesign and Server Side Components

RESS RWD and Server Side Components

Sven Wolfermann | maddesigns

Page 2: RESS – Responsive Webdesign and Server Side Components

Freelancer for modern web development (HTML5, CSS3, jQuery) from Berlin

CSS3 Adventskalender 2010/2011

wrotes articles and post for T3N-, PHP- and Webstandards-Magazin (new: Screengui.de)

mobile Geek

Sven Wolfermann (36)

Twitter: @maddesigns

Web: http://maddesigns.de

·

·

·

·

·

·

/

Page 3: RESS – Responsive Webdesign and Server Side Components

www vs. m. desktop vs mobile

/

Page 4: RESS – Responsive Webdesign and Server Side Components

desktop vs. mobile

dividing pages

large site (feature rich) vs. small size (performant)

/

Page 5: RESS – Responsive Webdesign and Server Side Components

desktop vs tablet vs mobile

dividing more pages?

large site vs. tablet vs. small size

vs. car vs. fridge vs. watch vs. …

/

Page 6: RESS – Responsive Webdesign and Server Side Components

Responsive Web Design

A List Apart article by Ethan Marcotte

/

Page 7: RESS – Responsive Webdesign and Server Side Components

A List Apart is responsive too now

Page 8: RESS – Responsive Webdesign and Server Side Components

Responsive Web Design

Flexible grids, based on percentage units

Variable image and video sizes – images fit into grid

CSS3 media queries

·

·

·

image source: http://macrojuice.com/

/

Page 9: RESS – Responsive Webdesign and Server Side Components

RWD

solves many things

one URL, one code base, one deployment

all contents available (if not hidden)

Future friendly

·

·

·

/

Page 10: RESS – Responsive Webdesign and Server Side Components

moto.oakley.com

·85.4MB page weight

471 HTTP requests

2 minutes 45 seconds until loading screen replaced withcontent

4 minutes 10 seconds until onLoad event

·

·

·

·

Page 11: RESS – Responsive Webdesign and Server Side Components

85.4MB, 471 HTTP requests!

THIS IS NOT RWD!

Oakley's monster page of baubles

/

Page 12: RESS – Responsive Webdesign and Server Side Components

moto.oakley.com fail

ok, ok, Oakley does it better now: JUST 14.2MB, 291 request (more than 70MB less)

with mobile user-agent? 6.7MB, 114 requests :/

Oakley's Moto diet

/

Page 13: RESS – Responsive Webdesign and Server Side Components

RWD

has some issues

site tend to be too large for mobile

some content is hard to adapt: images, tables, ads, ...

IE8 doesn't understand RWD (basically)

·

·

·

/

Page 14: RESS – Responsive Webdesign and Server Side Components

Guy Podjarny's RWD performance tests

sites have nearly same weight on mobile as on desktopReal World RWD Performance – Take 2

/

Page 15: RESS – Responsive Webdesign and Server Side Components

RWD is more

Beyond Squishy: The Principles of Adaptive Design

Page 16: RESS – Responsive Webdesign and Server Side Components

Performance

Reduce image payload (the biggest effect)

Reduce JavaScript and CSS payload

Further optimize based on feature detection

·

·

·

Lightening Your Responsive Website Design With RESS

/

Page 17: RESS – Responsive Webdesign and Server Side Components

Browser Feature Detection

/

Page 18: RESS – Responsive Webdesign and Server Side Components

Testing browser features with Vanilla JS

Cutting the mustard

if('querySelector' in document && 'localStorage' in window && 'addEventListener' in window) { // bootstrap the javascript application }

if browser supports 'querySelector','localStorage' and 'addEventListener' do hot stuff

BBC Responsive News – Cutting the mustard

/

Page 19: RESS – Responsive Webdesign and Server Side Components

Modernizr

Client side feature detection

Modernizr is a JavaScript library that detects HTML5 & CSS3 features inthe user's browser.http://modernizr.com/

/

Page 20: RESS – Responsive Webdesign and Server Side Components

Modernizr

throw in <head>

<head> <script src="modernizr.js"></script></head>

Modernizr features test: geolocation

Modernizr.geolocation // true or false

if (Modernizr.geolocation) { navigator.geolocation.getCurrentPosition(show_map);} else { // no native support; maybe try a fallback?}

/

Page 21: RESS – Responsive Webdesign and Server Side Components

Modernizr

Modernizr can load different files based on tests

Modernizr.load({ test: Modernizr.geolocation, yep : 'geo.js', nope: 'geo-polyfill.js'});

Modernizr.load is not part of the "development" version

Modernizr adds classes to <html>

<html class="js flexbox canvas canvastext webgl no-touch geolocation postmessage hashchange history boxshadow cssanimations csscolumns cssgradients csstransforms csstransforms3d csstransitions fontface video audio localstorage svg inlinesvg">

/

Page 22: RESS – Responsive Webdesign and Server Side Components

Modernizr

Another Sample: datepicker

<script src="modernizr.js"></script>

<script>Modernizr.load({ test: Modernizr.inputtypes.date, nope: ['jquery.datepicker.js', 'jquery.datepicker.css'], complete: function () { $('input[type=date]').datepicker({ dateFormat: 'yy-mm-dd' }); }});</script>

load jQuery datepicker library for browsers that don't have nativedatepickers

/

Page 23: RESS – Responsive Webdesign and Server Side Components

Conditional loading

/

Page 24: RESS – Responsive Webdesign and Server Side Components

Conditional loading

http://bradfrostweb.com/wp-content/uploads/2013/09/Keynote-11.png

Page 25: RESS – Responsive Webdesign and Server Side Components

Conditional loading

http://bradfrostweb.com/wp-content/uploads/2013/09/Keynote3.png

Page 26: RESS – Responsive Webdesign and Server Side Components

Conditional loading – window.matchMedia

Returns a new MediaQueryList object representing the parsed resultsof the specified media query string.

if (window.matchMedia("(min-width: 40em)").matches) { /* load secondary stuff */}

matchMedia Polyfill

Modernizr.load and Picturefill uses matchMedia for example

MDN Window.matchMedia

/

Page 27: RESS – Responsive Webdesign and Server Side Components

Conditional loading – Modernizr.load

Modernizr loads scripts and CSS based on media queries

Modernizr.load([

{

test: Modernizr.mq("only screen and (min-width: 1051px)"),

yep: '/js/large.js'

},

{

test: Modernizr.mq("only screen and (min-width: 600px) and (max-width: 1050px)"),

yep: '/js/medium.js'

},

{

test: Modernizr.mq("only screen and (min-width: 320px) and (max-width: 599px)"),

yep: '/js/small.js'

}

]);

you can use EM in media queries too ;)

/

Page 28: RESS – Responsive Webdesign and Server Side Components

Conditional loading – pairing CSS & JS

holding CSS and JavaScript Breakpoints in sync

body:after { content: 'small'; display: none;}@media (min-width: 650px) { body:after { content: 'middle'; }}@media (min-width: 1200px) { body:after { content: 'large'; }}

/

Page 29: RESS – Responsive Webdesign and Server Side Components

Conditional loading – pairing CSS & JS

holding CSS and JavaScript Breakpoints in sync

var size = window.getComputedStyle(document.body,':after') .getPropertyValue('content');

if (size == 'large') { // Load some more content.}

Conditional CSS

/

Page 30: RESS – Responsive Webdesign and Server Side Components

Conditional loading – Ajax-include pattern

Replace:

<a href="..." data-replace="latest/fragment">Latest Articles</a>

Before:

<a href="..." data-before="latest/fragment">Latest Articles</a>

After:

<a href="..." data-after="latest/fragment">Latest Articles</a>

init with jQuery:

$("[data-replace],[data-before],[data-after]").ajaxInclude();

An Ajax-Include Pattern for Modular Content

/

Page 31: RESS – Responsive Webdesign and Server Side Components

Browser-Sniffing

Page 32: RESS – Responsive Webdesign and Server Side Components

Browser-Sniffing

remember the old days? Not? You lucky guy! UA-string history

Browser-Sniffing went wrong

/

Page 33: RESS – Responsive Webdesign and Server Side Components

Browser-Sniffing

is hard and unreliable

For example, Safari user agent string on an iOS7 iPhone:

Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/546.10 (KHTML, like Gecko) Version/6.0 Mobile/7E18WD Safari/8536.25

often browsers aims other browsers

detecting the "right" user agent is complicated

Parsing UA string is not a regex jobUA-Detection libraries

/

Page 34: RESS – Responsive Webdesign and Server Side Components

What?! UA-Sniffing is wild guessing

Page 35: RESS – Responsive Webdesign and Server Side Components

This is NOT a "mobile" detection!

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { if (document.cookie.indexOf("mobile_redirect=false") == -1) { window.location = "http://m.yoursite.com/"; }}

more like this

// Check if UA is mobile (from http://detectmobilebrowsers.com/)if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(agent) { isUAMobile = true;}

/

Page 36: RESS – Responsive Webdesign and Server Side Components

Device Detection

/

Page 37: RESS – Responsive Webdesign and Server Side Components

Device Detection Libraries

WURFL (Java/.NET/PHP) free *

Scientiamobile (Java/.NET/PHP) commercial *

Device Atlas (Java/.NET/PHP) commercial

Netbiscuits' Device Library commercial

51degrees (.NET, C, Java, PHP) free limited / commercial

Open DDR (Java/.net) Open Source

Mobile Detect (PHP) Open Source

·

·

·

·

·

·

·

* free with restricted license

/

Page 38: RESS – Responsive Webdesign and Server Side Components

Device Detection Libraries

Problems

updates are slow

not reliable

·

·

/

Page 39: RESS – Responsive Webdesign and Server Side Components
Page 40: RESS – Responsive Webdesign and Server Side Components

Client meets server

/

Page 41: RESS – Responsive Webdesign and Server Side Components

Client meets Server

set browser width cookie with JS

RESS = {}; RESS.writeCookie = function (name, value) { //cookie code } //Store width in a cookieRESS.storeSizes = function () { //Get screen width var width = window.innerWidth; // Set a cookie with the client side capabilities. RESS.writeCookie("RESS", "width." + width);} RESS.storeSizes();

/

Page 42: RESS – Responsive Webdesign and Server Side Components

Client meets Server

use cookie info in code

Setting a file path based on window.innerWidth

<?php // grab the cookie value $screenWidth = $_COOKIE['RESS']; // set the img path var if ($screenWidth <= 320) { $imgPath = "320"; } else if ($screenWidth < 960) { $imgPath = "640"; } else { $imgPath = "960"; } // print out our image link print "<img src='/rwd/images/".$imgPath."/car.jpg' alt='Car' />";?>

http://www.netmagazine.com/tutorials/getting-started-ress

/

Page 43: RESS – Responsive Webdesign and Server Side Components

Client meets Server

use cookie info in code

<?php if ($RESS["width"] >= 320 && $RESS["width"] <= 640) {?> <div class="mobile-ad max-320"> <?php include "/ads/320.php"?> </div><?php } ?>

/

Page 44: RESS – Responsive Webdesign and Server Side Components

Modernizr Server

Client features for the server

<?php include('modernizr-server.php'); print 'The server knows:'; foreach($modernizr as $feature=>$value) { print " $feature: "; print_r($value); }?>

The server knows:canvas: 1geolocation: 1crosswindowmessaging: 1indexeddb: 0hashchange: 1...

https://github.com/jamesgpearce/modernizr-server

/

Page 45: RESS – Responsive Webdesign and Server Side Components

RESSResponsive Web Design + Server Side Components

/

Page 46: RESS – Responsive Webdesign and Server Side Components

RESS

http://www.lukew.com/ff/entry.asp?1392

/

Page 47: RESS – Responsive Webdesign and Server Side Components

In a nutshell, RESS combines adaptivelayouts with server side component(not full page) optimization. So a singleset of page templates define an entireWeb site for all devices but keycomponents within that site havedevice-class specific implementationsthat are rendered server side.

—Luke Wroblewski @lukew

http://www.lukew.com/ff/entry.asp?1392

/

Page 48: RESS – Responsive Webdesign and Server Side Components

Sapient Nitro about RESS

http://www.youtube.com/embed/XhCXINNvmv8

/

Page 49: RESS – Responsive Webdesign and Server Side Components

Advantages of RESS

Easier to navigate: The navigation structure can be customized for thedifferent tasks.

Less page bloat: Instead of relying on display: none; or visibility:hidden; to hide page elements for mobile devices, removed from theHTML or CSS.

Faster load time: Unnecessary CSS/JavaScript can be removed fromthe HTML

·

·

·

/

Page 50: RESS – Responsive Webdesign and Server Side Components

Disadvantages of RESS

More server resources: Dynamically building the HTML will increasethe load on the server.

Caching: A need for a better caching mechanism

Requires device detection: Mobile users will need to be detected.Device detection is unreliable.

·

·

·

/

Page 51: RESS – Responsive Webdesign and Server Side Components

RESS in the wild? -> Adaptive Images

http://adaptive-images.com/

/

Page 52: RESS – Responsive Webdesign and Server Side Components

Adaptive Images

/

Page 53: RESS – Responsive Webdesign and Server Side Components

Adaptive Images

Add .htaccess and adaptive-images.php to your document-rootfolder.

Add one line of JavaScript into the <head> of your site.

Add your CSS Media Query values into $resolutions in the PHP file.

·

·

·

/

Page 54: RESS – Responsive Webdesign and Server Side Components

Adaptive Images

1. The HTML starts to load in the browser and a snippet of JS in the <head> writes asession cookie, storing the visitor's screen size in pixels.

2. The browser then encounters an <img> tag and sends a request to the server for thatimage. It also sends the cookie, because that’s how browsers work.

3. Apache receives the request for the image and immediately has a look in the website's .htaccess file, to see if there are any special instructions for serving files.

4. There are! The .htaccess says "Dear server, any request you get for a JPG, GIF, orPNG file please send to the adaptive-images.php file instead."

/

Page 55: RESS – Responsive Webdesign and Server Side Components

Adaptive Images

5. The PHP file looks for a cookie and finds that the user has a maximum screen size of480px.

6. It compares the cookie value with all $resolution sizes that were configured, anddecides which matches best. In this case, an image maxing out at 480px wide.

7. It then has a look inside the /ai-cache/480/ folder to see if a rescaled image alreadyexists.

8. We'll pretend it doesn’t - the PHP then goes to the actual requested URI to find theoriginal file.

9. It checks the image width. If that's smaller than the user's screen width it sends theimage.

10. If it's larger, the PHP creates a down-scaled copy and saves that into the /ai-cache/480/ folder ready for the next time it's needed, and sends it to the user.

http://adaptive-images.com/

/

Page 56: RESS – Responsive Webdesign and Server Side Components

Responsive Images a topic for itself…

Responsive Images [german]

/

Page 57: RESS – Responsive Webdesign and Server Side Components

Detector

/

Page 58: RESS – Responsive Webdesign and Server Side Components

Detector (PHP RESS library)

combines UA-parsing and feature testing

includes User Agent parser – record any useful information (like OS ordevice name)

includes Modernizr Server

no need for commercial device detection libraries

add your own feature tests and store the results using Modernizr'saddTest() API

·

·

/

Page 59: RESS – Responsive Webdesign and Server Side Components

Detector

1. HTTP request hits server

2. Detector compares User-Agent with database

3. if known, classify device and get back content

4. if not, Modernizr makes feature detection, stores cookie

5. reloads site, gives back specified content

6. stores UA-String/features combination for later use

·

·

·

·

·

·

How Detector works

/

Page 60: RESS – Responsive Webdesign and Server Side Components

Detector device families

The default install of Detector will categorize browsers into one of threefamilies.

// families.json{ "tablet": { "isTablet": true }, "mobile-advanced": { "isMobile": true, "features": ["cssanimations","localstorage","deviceorientation"] }, "mobile-basic": { "isMobile": true }, "desktop": { "isComputer": true }}

/

Page 61: RESS – Responsive Webdesign and Server Side Components

Detector sample

switch ads, basic sample

if ($ua->family == 'mobile-basic') { include "/ads/simple.php";} elseif ($ua->family == 'mobile-advanced') { include "/ads/responsive-ads.php";} else { include "/ads/desktop.php";}

Detector video sample

if ($ua->video->h264 || $ua->video->webm) { print $html5Embed; // YouTube's <iframe> code} else { print $simpleLink;}

/

Page 62: RESS – Responsive Webdesign and Server Side Components

RESS examples

University of Notre Dame

West Virginia University

CNN

Scientiamobile

/

Page 63: RESS – Responsive Webdesign and Server Side Components

RESS isn't the holy grail (RWD isn't either)

Page 64: RESS – Responsive Webdesign and Server Side Components

RESS – further reading

Templating with Detector & Mustache for RESS

Is Adaptive Web Design Or RESS Better Than Responsive Design ForSEO?

Adaptation: Why responsive design actually begins on the server

/

Page 65: RESS – Responsive Webdesign and Server Side Components

One more thing!

/

Page 66: RESS – Responsive Webdesign and Server Side Components

Client Hints

Client Hints is a new proposal by Ilja Grigorik and will allow clients toindicate a list of device and agent specific preferences. Spec Draft

(request)GET /img.jpg HTTP/1.1User-Agent: Awesome BrowserAccept: image/webp, image/jpgCH: dpr=2.0

(response)HTTP/1.1 200 OKServer: Awesome ServerContent-Type: image/jpgContent-Length: 124523Vary: CH

(image data)

Automating DPR switching with Client-Hints

/

Page 67: RESS – Responsive Webdesign and Server Side Components

Client Hints

For example, given the following request header:

CH: dh=598, dw=384, dpr=2.0

The server knows that the client's screen height is 598px, width is384px, as measured by density independent pixels on the device, andthat the device pixel ratio is 2.0.

/

Page 68: RESS – Responsive Webdesign and Server Side Components

Client Hints

ReSrc.it can handle client hints now

/

Page 69: RESS – Responsive Webdesign and Server Side Components

Questions?

Thanks for your attention!Sven Wolfermann | maddesigns

http://maddesigns.de/RESS/

HTML5 slideshow by Google

/