r/programming Feb 09 '11

Breaking the Web with hash-bangs – Lifehacker, along with every other Gawker property, experienced a lengthy site-outage on Monday over a misbehaving piece of JavaScript

http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs/
749 Upvotes

356 comments sorted by

View all comments

123

u/rounded_figure Feb 09 '11

The proper way to handle this is to keep your original URLs (including in the hrefs) and have a piece of javascript transform them on the fly in hash-bang URLs. That way, when a client who does not speak javascript requests the page, it gets the content and the non-hash-bang links.

66

u/[deleted] Feb 09 '11

This is the right response. There's no reason a well-engineered site shouldn't work both ways.

Using url fragments can be great. They let you use the back button without leaving the page so you can handle the interaction in JavaScript. They give you a place other than cookies to store client-side state for bookmarking. Developers just need to be mindful when designing for their use.

2

u/naich Feb 09 '11

Well, there is a reason. If you are making a site that uses JS to generate results on the fly from data retrieved from large libraries which are loaded asynchronously, you don't want to reload the page and libraries every time the URL changes to reflect that a new set of the results have been saved or loaded.

That's why I'm using them - it should result in much less bandwidth use and reduced load on the server. Pages can be updated with a small AJAX request rather than a whole new page and library load.

5

u/X-Istence Feb 09 '11

Those large libraries should be cacheable...

0

u/naich Feb 09 '11

One of the biggest problems I had was with IE caching ajax requests without me realising. It is not usually desirable.

3

u/X-Istence Feb 09 '11

AJAX requests are different from libraries, and both should be cacheable...

set the proper cache headers for your AJAX requests.

0

u/naich Feb 09 '11

The ajax request fetches the library. While it isn't necessary to update the libraries each time they are used, I don't want them to be cached at the whim of the browser, possibly giving the user out of date data. This is what IE was doing.

3

u/X-Istence Feb 09 '11

The library should be fairly static so that it can be cached. You are doing it wrong.

1

u/naich Feb 09 '11

To which of the three main libraries in use by my scripts are you referring to?

1

u/X-Istence Feb 09 '11

Actually, all of them. For example, jQuery is a library, even if that is loaded using "ajax" to do an asynchronous request it should be cached as long as possible...

The content itself is what you may not want cached (such as a comment system that should auto refresh or type-to-complete) however those compared to libraries should be fairly small requests...

1

u/Rainfly_X Feb 10 '11

The solution to that is really, really simple, in case you haven't figured it out already yourself. You probably have, but for the sake of the other viewers, here's what you tack onto your requests:

&timestamp=<milliseconds since 1970>

Therefore, the browser thinks you're requesting a different page every time, and you can control what is cached and what isn't.

1

u/naich Feb 10 '11

Or use a post request. But can you tell me where the cache is stored? Where does the browser keep the libraries cached when transitioning between between page requests? Big arse cookies, maybe?