Graceful degradation is an important asset of Drupal's core behaviour and best practices. I understand that AIS does not yet have found a way to accomplish this.

Since, on the other hand, responsiveness actually IS becoming more and more important, I would be willing to support AIS and implement a fallback solution. Basically I would take an approach I wanted to implement standalone some time ago:

- Conditionally add some inline styles as long as there is no AIS cookie to evaluate.
- Use them to define CSS media queries which trigger loading whatever file (bg-image, virtual css file etc.) from the Server
- Recognize the request, evaluate it and generate a cookie the REST way.

If you sympathize with my idea, let me know; I will be willing to help out with a patch doing just this.

br
david

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

spotzero’s picture

Status: Active » Needs work

Sounds great, I'd love to have a javascript fall back in AIS. If you can implement a graceful way of adding this to AIS, I'll be happy to include it.

Thanks,

doitDave’s picture

Ok, great! I will have some time later in spring. I will use the .dev branch as a base for patches, right?

doitDave’s picture

Assigned: Unassigned » doitDave
spotzero’s picture

Yes, use the 7.x-1.x branch.

One the multistyle branch is completed, depending on where this feature is, we'll port it in.

Thanks,

doitDave’s picture

So, here we go. Sorry for the delay, business took me off ;)

"Brief" description of what this first attempt does:

* Build combined @import/@media queries depending on each existing adaptive style (taken from the variables array).
* Issue these as an inline style on every page. This forces a (suitable) browser to import the "file" corresponding with its actual viewport dimensions.
* When the browser requests the "file" (actually a page callback with a wildcard which is nothing other than the matching style's machine name), it returns a dummy file and sends a cookie to the client.
* On the next (see below) request, the user gets the image styles best matching his viewport.

How to get rid of the delay issue in general:
At a glance, it looks as if without JS, there could be no instant matching. But extending the above approach, here's a basic idea of how to solve it completely without JS (see even more below). As you actually alter the image style callback anyhow by redirecting "image_style_deliver", why not extend this scheme. Roughly (pseudo code):

if (!find_any_ais_cookie()) {
  // Replace the image href with a common callback function (and an image ID or whatever).
  replace_image_url('ais_gateway', $image_id);
}

/**
 * Image gateway callback.
 */
function ais_gateway($image_id) {
  $current_user_style = $_COOKIE['Drupal.visitor.ais'];
  drupal_goto(/path/to/image/$current_user_style);
}

The idea behind this is simple: If no cookie has been set, retrieve the images through a redirector. So when the browser loads the first page ever, he will
a) load the virtual CSS file and thus have a cookie.
b) load the image(s) via the redirector.
c) Once the redirect path is requested, the browser already has the cookie from the virtual css request.
The redirector will only appear if no ais cookie is found. Otherwise everything works like it does right now.

JS note:
Of course we would not drop the JS. First, the $(window).resize event may provide more recent data. Then, JS may be useful to force a replacement of images with the best-matching style derivative in realtime. And then, both JS and the fallback can coexist in peace, so why sacrifice someting.

Finally, find my first patch proposal. Looking forward to getting your opinion!

Notabene: In order to use the user_cookie_ API functions, we really need to change your ais cookie to the correct Drupal namespace (Drupal.visitor.).

doitDave’s picture

PS.

Just thought again. In the end, your RewriteRule does not do anything else at all than redirect using an HTTP 302.

So in the end, the redirector callback should really be worth a try. I will soon give it a shot, but first I am going to wait for your comments on these rather major changes (and hope you won't mind my feeling free to propose that much). :)

Good night!

doitDave’s picture

Status: Needs work » Needs review
doitDave’s picture

Forgot to change the cookie name for the hook_requirements implentation. Updated patch attached.

spotzero’s picture

While I agree that the ais cookie should be correctly namespaced, however I won't include that in AIS 7.x-1.x since it will break AIS on the site of anyone who updates the module (since the .htaccess file won't be updated). It also breaks in multi-site installs where two different sites are running different versions of AIS (if I've learned one thing in the issue queues, it's that if it can happen, it will).

Instead, can you seperate the cookie namespacing into a seperate issue. I'll included in changes for AIS 7.x-2.x, which is the branch for things that break updates or change functionality.

In the mean time, I'm still testing your patch, but the code is looking good.

Thanks for all of your hard work.

doitDave’s picture

Great to hear that you like it. So, while you continue testing and I am looking forward to your finding potential issues, I will examinate on the redirect trick (to work around the one-request-delay mentioned above).

I understand your concerns regarding the Cookie ./. .htaccess issue, you have a point. I will thus split it up the next days and create different patches alright. I will reuse the existing separate cookie issue for this purpose.

After all it is a pleasure to contribute to useful projects - like yours definitely is. :-)

doitDave’s picture

Status: Needs review » Needs work
doitDave’s picture

Status: Needs work » Needs review
FileSize
17.24 KB

I rerolled the cookie thing by now. As there is no 2.x branch yet, I left some @todo comments so we can find the problematic spots right away. I also added some more CS/API related comments. We should look after those in a free minute. Also I fixed some unnecessary double-quoted strings where single quotes suffice; why sacrifice CPU withoud need.

Here is a patch just like the one above but with your legacy cookie name. Let's just include this stuff and once it is, I will look after the redirect thing (we should open a separate issue for this IMO, in order to reduce confusion).

David

doitDave’s picture

Issue summary: View changes

Contact me if anything happens here, I am cleaning up my queue and unfollowing.