Current time: 11-12-2019, 04:48 PM Hello There, Guest! (LoginRegister)

Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Javascript "content negotiation"
07-08-2012, 04:42 AM (This post was last modified: 07-08-2012 07:30 PM by lena.)
Post: #1
Javascript "content negotiation"
For about a year google chrome supports arithmetic jpegs (on average 7% smaller than normal jpegs).

Since firefox 3.0 (and similar geckos) and opera 9.5, these browsers support APNGs. Animated gifs can be converted to apngs with a far better compression ratio.

Unfortunately the aforementioned browsers do not advertise this support via http headers.

Given that, is there a way of changing (rewrite) the image source urls using javascript before the image even start to load the following way?

image.jpg (normal jpeg) -> image.jpeg (the arithmetic encoded one)
image.gif (assuming that gifs are only used for animations) -> image.apng

The rewrite would be made based on javascript browser sniffing and executed client side.
Find all posts by this user
Quote this message in a reply
07-10-2012, 02:59 AM
Post: #2
RE: Javascript "content negotiation"
Doing the sniffing and replacing on the client side tends to bump into problems with browser's look-ahead parsers (they will start downloading the images well before your javascript gets a chance to swap them out).

It's not a great answer but this kind of thing is sadly best done by UA sniffing and server-side logic (and one of the strongest cases for needing some form of optimization automation on the server side). Data URI's for embedded images pretty much fall into the same camp.

You would want to extend the same logic to include jpeg->webp for Chrome and Opera since they are generally significantly smaller than even arithmetic jpeg's.
Visit this user's website Find all posts by this user
Quote this message in a reply
07-10-2012, 07:10 PM (This post was last modified: 07-10-2012 07:14 PM by lena.)
Post: #3
RE: Javascript "content negotiation"
Unfortunately, doing it server side means that the header Vary: User-Agent must be sent, which pretty much would negate the optimization, due to lost cacheability on proxies.

IMO the sane way of doing this, would be the browser sending an appropriate header advertising the support for certain types of images. Accept-Encoding or Expect might be good candidates, if browsers could reach a consensus... or complete what they started.

Arithmetic jpegs or APNGs are no brainers, as they are lossless transformations.
Find all posts by this user
Quote this message in a reply
07-10-2012, 11:41 PM
Post: #4
RE: Javascript "content negotiation"
With some lateral thinking, I believe that it can be done client side with javascript and with "no" delays concerning look-ahead parsers.

The trick is to use the Cookie header as a custom Accept header (assuming that no other cookies are sent to that subdomain or path), set it with javascript (for instance accept=[|jpeg|apng|jpeg,apng]) and set the header Vary: Cookie server side.

As there is a very limited number of values for the cookie, cacheability by intermediate caches shouldn't be destroyed.

Do someone know how intermediate caches behave with respect to the header Vary: Cookie?
Find all posts by this user
Quote this message in a reply
07-11-2012, 04:52 AM
Post: #5
RE: Javascript "content negotiation"
How about doing the detection server-side using the UA string on the html itself (assuming it is not cacheable in your case) and then just reference the different image types by different file names? That way the static resources are still 100% cacheable. This is pretty much what the accelerator solutions do but it shouldn't be too hard to bake the logic directly into an app.
Visit this user's website Find all posts by this user
Quote this message in a reply
07-11-2012, 07:54 PM
Post: #6
RE: Javascript "content negotiation"
That's also a good idea.

Unfortunately I'm trying to further optimize a section of a site with static pages [cacheable for 3 months], which have quite a lot of images and has google analytics. As a consequence I can't use the cookies solution, because the images are on the same domain of the HTML pages.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 2 Guest(s)