[TL;DR version: use cookies to store the results of async calls; render the results of past async calls immediately and then validate them after page-load.]
I’ve been working on SharePoint intranet site for a client that features, onder andere, a stylized secondary navigation whose menu options are managed via a regular old custom list. The idea is that the client gets to control “their” site’s menu without affecting or being affected by the global navigation put out by IT.
(there is something incredibly subversive about adding a CEWP that points to an HTML file that loads some CSS and JS to fundamentally alter almost everything about a site’s behavior… but that’s for another post)
The code for this pretty simple:
- Wait for the page to finish loading and make an async call to load up menu items from a list using REST or lists.asmx or whatever
- Using jQuery, dynamically populate a bunch if <li>’s inside a parent <Straat> (see Christian Pinder’s article here for a simplistic explanation of this tecnnique)
- Use CSS to do all the formatting
- Profit!
The sore spot here is that every time anyone hits one of the site’s pages, that user’s web browser is reaching out to get items from the list. Once dev is complete and testing has proven things to be stable and complete, this call is unnecessary more than 99% of the time since the menu rarely changes. It also has a weird UI affect which is common in this brave new world of hyper-ajaxy web sites – the page renders and only then does the menu render. It’s jittery and distracting in my view. And jittery. So, caching.
I modified the logic thusly:
- Look for a cookie in the browser that contains the menu as I last read it
- If found, render it immediately. Don’t wait for the page to finish loading. (You need to make sure your HTML is strategically placed here, but it’s not hard to do).
- Wait for the page to finish loading and make an async call to load up menu items from a list using REST or lists.asmx or whatever
- Compare what I got against the cookie
- If it matches, STOP
- Anders, using jQuery, dynamically populate a bunch if <li>’s in a <Straat>
- Use CSS to do all the formatting
- Profit!
Some of you are going to say, “hey! there’s no real caching going on here since you’re reading the menu anyway every single time." And you’re right – I’m not giving the server any kind of break. But because the call is async and happens after the page’s initial HTML payload fully renders, it “feels” more responsive to the user. The menu renders pretty much as the page draws. If the menu happens to the change, the user is subjected to a jittery re-draw of the menu, but only that one time.
There are some ways to make this caching more effective and help out the server at the same time:
- Put in a rule that the “cookie cache” is valid for a minimum of 24 hours or some other timeframe. As long as there is no expired cookie, use the cookie’s menu snapshot and never hit the server.
Well … that’s all that come to mind right now :).
If anyone has any clever ideas here I’d love to know them.
And lastly – this technique can be used for other stuff. This client’s page has a number of data-driven things on various pages, many of them changing relatively rarely (like once a week or once a month). If you target specific areas of functionality, you can give a more responsive UI by pulling content from the local cookie store and rendering immediately. It feels faster to the user even if you’re not saving the server any cycles. Jy kan save the server cycles by deciding on some conditions and triggers to invalidate this local cookie cache. That’s all situational and artsy stuff and really the most fun :).
</einde>
Volg my op Twitter http://www.twitter.com/pagalvin
Hi Paul,
Nice implementation, that’s something we use but we used the HTML5 local storage API instead of cookies. The first drive was that we needed to store more than cookie can allow. But an added benefit is that it does not increase payloads, whereas cookies are sent back and forth. From a dev point of view it’s very easy to use.
Browser support is not perfect but honestly quite good, http://caniuse.com/namevalue-storage .
Jonathan