A gem for building mobile optimized Rails applications using semantic, media query-based device detection and server side component optimization.
A system for building mobile optimized Rails applications using semantic,
media query-based device detection and server side component optimization.
RESS is an extension of the devicejs
library written by Boris Smus. It adds a back end for
adapting server responses based on client side feature detection. RESS allows
you to specify alternate versions of your website, along with media queries
for which devices should be redirected to which version.
When you register alternate mobile versions of your website, RESS adds annotations
to the <head>
of your document that describe where these pages are located and
which devices should be redirected to them.
For example, a typical alternate version for a mobile site might include a tag
like this:
<link rel="alternate" media="only screen and (max-width: 640px)" href="http://m.example.com/page-1" >
The mobile version of the page would then have a link pointing back the canonical
version:
<link rel="canonical" href="http://www.example.com/page-1" >
These annotations conform to SEO best practices for mobile optimized websites
as documented by Google.
When a request comes into your site, the javascript included with ress will parse
all of the [rel="alternate"]
links in your markup, and evalute their media queries
to determine if there is an alternate version available that matches the client.
If there is, the user is redirected to the url for that version.
Ress allows you to customize how your Rails application responds to mobile requests in
two ways:
<% if mobile_request? %>
<%= image_tag 'low-res.png' %>
<% else %>
<%= image_tag 'high-res.png' %>
<% end %>
app/mobile_views/users/_form.html.erb
and Ress would have Railsapp/views/users/_form.html.erb
when a request comes in to theAdd this line to your application’s Gemfile:
gem 'ress'
And then execute:
$ bundle install
Run the generator:
$ rails g ress:install
Alternate versions of an application are registered using the #add_alternate
method in the
ress.rb
initializer that is generated by the ress:install
generator. The configurabe options
available are all documented in the comments of that file.
You can manually override the version detector javascript and allow mobile
clients to visit the canonical version of the app by passing in a the GET
url parameter force_canonical=1
. This sets a session cookie in a before_filter
that stops the version detection scipt from redirecting users, so it only has to be
done once per session. Ress includes a helper / controller method force_canonical
that returns
a link back to the canonical version of the current page with this query param appended.
For, example you may include something like this in your <footer>
to let mobile users
access the canonical site.
<!-- Let mobile devices access the canonical site -->
<footer>
<% unless canonical_request? %>
<div>
You are currently viewing the mobile version of this site.
<%= link_to 'View the desktop version', force_canonical_url %>
</div>
<% end %>
</footer>
There are a couple of Modernizr features that must be included in order for
Ress’s javascript feature detection to function. If you are not already
using Modernizr in your application you can automatically include a build that
has been packaged with the gem by setting config.include_modernizr = true
in
config/initializers/ress.rb
. If you include your own build (recommended),
make sure that it includes “Touch Events” and “Media Queries”, eg:
http://modernizr.com/download/#-touch-mq-teststyles-prefixes
In order to share sessions and cookies between the different subdomains used by the
alternate versions of your app, you need to configure the :domain
option both in
the config/initializers/session_store.rb
and when setting cookies. For more
information about how this works see this
Railscast.
Because RESS uses subdomains, while developing alternate versions you cannot
load your site via localhost
or an IP address. If you want to test on
the same machine you are running your rails app on, you can load it through
http://lvh.me or install pow and set up a .dev
domain
for your app. If you need to test on a mobile device you might want to try
http://xip.io/.
The javascript included by Ress does some checks and will use client-side
redirection to point users to the right version of your webapp. Client-side
redirection can have a performance overhead (though I haven’t measured it).
If you find this is true, you can keep your DOM the same, still using the
SEO-friendly <link rel="alternate">
tags, but simply remove the
ress.js script and do your own server-side UA-based redirection.
The feature detection javascript should work in all browsers that support
document.querySelectorAll
. Notably, this excludes IE7. If you want it
to work in IE7 and below, please include a polyfill.
Given how many browsers and devices we have these days, there are bound
to be bugs. If you find them, please report them and (ideally) fix them
in a pull request.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Ress is the compilation of a few different ideas packaged up for Ruby on Rails. You
may want to look at the following articles for more info: