Sunday, July 01, 2007

REST, Ajax, and personalization

I didn't realise it until recently, but if you follow REST principles, then you're pretty much forced towards Ajax - the two come together. That's due to page personalization.

When I navigate to www.amazon.com, several parts of the page are personalized. It says "Hello, Chris Jay" at the top, and offers recommendations for me, presumably based on purchases I've made in the past.

Although it's a very nice touch, this bothers me. At first glance, following REST, shouldn't the URL www.amazon.com be the same for everyone? Otherwise it can't be cached!

Option 1: Separate URLs for everyone

One option would be to redirect me immediately to something like www.amazon.com/ChrisJay/, containing my personalized homepage. This would require permissions, as set by the cookie - otherwise, other people could access this URL too. But that also holds for the current Amazon site.

There are several benefits to this approach. Amazon and my ISP can now both cache my homepage, so when I access it again, I see it much more quickly. And it's obvious from the URL that the page is personalized.

Unfortunately, following this option, the main body of the page - the bit that's not personalized - is not cached separately. It has to be produced again for every user.

Option 2: Same URLs for everyone, use Ajax

Another approach would be to keep using www.amazon.com for everyone, but include some Ajax that scans the HTTP header, reads the current user id, uses it to GET another REST URL (e.g. /recommendations/ChrisJay), and displays it inline.

That way, the homepage can be cached for anyone (including the Ajax code), the personalized bits can also be cached separately, and the REST architecture is still followed.

For this reason, Option 2 is the better approach.

Designing your URLs

The first step to building REST sites is to select your URLs.

You might think the rule is "one URL for every page". But that's not necessarily true, as Option 2 shows, especially if you're offering personalized sections to a page.

A better rule would be "one URL for every page and for every personalized section".

Then you can use Ajax to tie the two up.

No comments: