426 points · by dieulot · 12y ago · 174 comments
It seems the internet architecture won't bring us instant websites anytime soon. So here is a hack.
I released this JS library last month (today is version 2.0 release, fixing v1.0's rough edges), it makes use of HTML5's pushState and preloading to make a website instant.
It works like this: before clicking on a link, you'll hover over it. And there's a delay of 200-300 ms between these two events. InstantClick uses this delay to preload a page, when you click on a link it will display instantly in most case. (You can test on the website.)
That's similar to pjax and Turbolinks, for it uses HTML5's pushState and Ajax. And preloading is thrown into the mix.
In case preloading all the links that a user hover over is too much work for the server, there's also an option to set a delay before preloading kicks in, or to preload on "mousedown". Mousedown is when you press your mouse button (a click is when you release it). This way you still get a slight "magical" speed advantage, and pjax's benefits (notably, no recalculation of scripts/styles on every page change).
For mobile it preloads on touchstart, so you get 400 ms to preload. This is different from FastClick: with InstantClick a user can still double tap to zoom without triggering a link.
As said in the first sentence, the internet architecture can't bring us instant websites today or tomorrow. So I think this — InstantClick — could be a pretty big thing to improve the situation at scale.
The main challenge I see is that existing JS scripts may not work with it out of the box. I don't do much web development nowadays though, so I don't know how big of a problem that is, and I don't know if there isn't any other major barrier in the way to make this mainstream.
That's why I'm awaiting your opinion, smart folks of HN.
I've always hated menu dropdowns where it would close if you try to skip to the next submenu you've opened, since you hovered on the other parts on your way to the submenu.
So you'd end up having to perfectly 'trace' the menu so it won't close down on you.
The coolest approach given was when a user types in the search box, kick off the request for the full results page if they move their mouse on a motion curve towards one of the results, with some machine learned data on how people move their mouse: Not quite in a straight line, and with some acceleration.
edit: @devgutt else already mentioned this. I had this page open for a while before posting.
The actual HTML is only going to add another few KB.
But caching the skeleton of the page will work. We need browser to do it for us, just like browsers already caches the images.
OK, here it is: http://instantclick.io
And probably a ton of other application bugs as style and script stuff wont load. like they normally would
It definitely should not be a GET, since good practice says that GET is idempotent (http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html)
Nobody ever follows this since we all want stuff like www.example.com/articles/latest to respond to GET and also have different content depending on time. The distinction is at the core of the REST abstraction and it's an art.
# if you don't have a session token yet
POST /session-tokens # Accept: application/json
=> 200 {"token": "someuuid"}
# no cookies set!
# stuff the session token in localStorage instead
# now use it
PUT /sessions/someuuid # with your login details
=> 201
GET /sessions/someuuid/inbox # Accept: application/json
=> 200 {...}
GET /sessions/someuuid/inbox # again
=> 304 # wow much cached
# meanwhile
GET /sessions/otheruuid/inbox
=> 200 {...} # other user: other memcached prefix!
# pages that won't change per-user...
GET /home
=> 304 # can be cached globally
# (use JS client-side, not edge-side, includes)
# and finally...
DELETE /sessions/someuuid
=> 301 /home # log out!
Basically, a session is just another resource that you create and then manipulate using its sub-resources. This makes all sorts of things easier: creating session tokens and attaching sessions to them (by logging in, or as a guest) are separate steps, so you can throttle logins at the just by throttling access to the token generator. No cookies and no ESI means caching (reverse-)proxies can actually cache. (Note that I'm presuming TLS and hygienic CORS here, so "session stealing" isn't a valid complaint. Or rather, without those things, it's just as valid a complaint for a cookie-based approach.)Oh, but as a postscript, one little HATEOAS bonus tweak:
POST /session-tokens
=> 200 {"token": "/sessions/someuuid"} # no constructing URIs!But i do concur.
Maybe blacklist any links with the words logout, delete, etc anywhere inside the a tag or in the attributes?
If one is worried about preloading links that triggers an action, there is a whitelist mode to enable links as you review them.
I set the preload to occur on mousedown rather than mousover, as per the docs, but even with this I noticed near-instantaneous page loading.
Not sure if that's expected as part of the design?
Also please test how it goes when you press for like 5 seconds.
For many companies (Facebook, Twitter, etc) the desire for instant user gratification is paramount, so the push toward instant browsing experience is a very real possibility. One problem is that most people wouldn't really notice, because these websites load pretty quickly as it is.
One interesting direction is if there was some kind of AI in the background that knows what pages you're likely to visit and preloads them - Facebook stalking victims would become an instantclick away.
For answering your second paragraph, note that even small gains in speed have a direct effect on user engagement. Google and Amazon have noticed so: http://www.uiandus.com/blog/2009/2/4/amazon-milliseconds-mea...
[1] https://github.com/rails/turbolinks/pull/253#issuecomment-21...
There are a couple things that I had on my TODO list that could be handy though:
1) Caching - if you hover back and forth over two links, it will keep loading them every time. Dunno whether this can be alleviated or not.
2) Greater customisability. It'd be great if I could customise whether it was a hover or mousedown preload, on a per link basis. Some links benefit from hover, others it might be overkill.
3) Lastly, it would be cool if it could link up with custom actions other than just links. For example, jquery ajax loading a fragment of html to update a page. This is probably lower down on my priority list though, as the full page prefetch works remarkably fast.
Keep up the great work!
Caching can be done server-side, this is not something I plan to implement.
The last two seems like things that wouldn’t be needed very often, so they probably won’t make the cut either.
Well, in a way all sites already do that by embedding resources from different urls.
That's about 33KB for 9 blog posts (not including attachments). So about 4KB per post. I could have a hundred articles and it would still be under half a MB. To put things into perspective, the cover photo of my pup is about 818KB. There are also numerous resource files, like JS, HTML, CSS that needs to be downloaded before the site will even be ready.
If not the case, note that it’s just HTML we’re wasting. In the grand scheme of things it seems to me it would not have that much of an impact on bandwidth usage.
The main differences are that InstantClick preloads just before you need a page, so you can be more liberal in what you preload. Also pages are more recent, useful if you’re serving dynamic content.
One interesting reaction I had: things loaded so fast that I didn't notice one of the page changes and thought it was stuck. For sites like this one where different pages look very similar, maybe it could be worth experimenting with some sort of brief flashing animation (to make it look like a real page load)?
On the other hand, how wasteful is it to download tons of content that would be useless because the user might've navigated away from your site already?
For example I go to site A which uses jquery from the google cdn then I switch to site B which uses the same cdn. The browser doesn't even make a request for the jquery resource then, it just loads the cache. If the full site was loaded as a zip file we'd be downloading redundant jquery data twice.
Also, you should take into account the focus event of links, I tried and it seems you doesn't when trying on the "click test" page to tab tab tab on the test link and then hitting enter.
As you mention with JS scripts not working, I had to do things like rebind functions when pjax finished, or load new JS snippets along with each HTML (page) snippet. Not too huge a compromise.
Also you might want add some kind of tracking to avoid making repeat requests (probably within a timeframe). For e.g. hovering one link, then changing your mind, then going to another link. The browser will smartly pull it from its cache but this lib is still making the requests.
It doesn't feel that much faster, but I'd love a way to verify speed changes with data.
I had already viewed a couple pages (to read what it is about) before I started looking for a demo, and by then I wasn't sure if it was my browser caching or your script that made the pages load snappy.
Maybe also provide a 10-page Lorem ipsum "before" example, clearly marked that it does not use your library, to demonstrate the difference. I can't really tell if maybe your server is just really fast.
unless you use vimperator or similar. the demo handles this though, giving a hover time of infinity.
More than scaling, the larger headaches come from migrating all your existing logging to account for preloading versus impression hits.
(Not talking about this project in specific as it seems stats can be made to work with it, just replying to the idea in your comment.)
can you provide some specific definitions? thank you
Works great! I hope I won't have too many accidents with it.
I remember using 10 years ago a similar Windows program that preloaded all links.
Thanks for sharing :)
>Click − Mousedown = 2 ms
>Click − Touchstart = ∞
I win!
Also note that percentages like that don’t tell the whole picture; non-touch traffic hasn’t decreased by “like 40%”.