Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify browser cache after page DOM is updated?

So far, I know that I can seperate static and dynamic content using AJAX, so if a blog post has a comment update, the article does not have to be redownloaded (it'd be 304), only the comments. Now, I'm thinking of persisting the fetched dynamic content so that next time, only new comments have to be requested. Is there any way to do this without relying on Flash/Gears/HTML5Storage or other plugins?

EDIT: Let's say the comments section on the article looks like this:

<div class='comments' id='comments'>
 <a name='comments'></a>
 <h4>43 comments:</h4>
 <dl id='comments-block'></dl>
 <p class='comment-footer'>
  <a href='http://example.org/postcomment' onclick=''>Post a Comment</a>
 </p>
</div>

on which, an AJAX call to fetch the comments will append them to the <dl id='comments-block'>, which contains the following snippet:

<div class='comments-singleblock'>
  <dt class='comment-author' id='comment-5378479254070788764'>
    <a name='comment-5378479254070788764'></a>
      Anonymous coward
      <span class='comment-timestamp'>
        <a href='#comment-5378479254070788764' title='comment permalink'>
           5 February 2012 16:52
        </a>
      </span>
    </a>
  </dt>
  <dd class='comment-body'>
    <p>
      Your lorum is my ipsum, the dolor is sit amet us.
    </p>
  </dd>
  <dd class='comment-footer'>
    Which eternal lies, that is not death.
  </dd>
</div>

which can be cached. The subsequent AJAX calls should return only new contents, appending to the above. Now when the article is loaded, the logic is as such

  1. First AJAX comment call without timestamps: fetch all comments till current timestamp A. This is cached.
  2. Time passes, on new timestamp B, send request to server for comments after timestamp A. This call may be cached, but pointless because the request url is never reused.
  3. Client now has updated comments, until timestamp B. But only content up to timestamp A is cached.
  4. Client refreshes article, which prompts the first comment request without timestamp, which should return the cached comments until timestamp A.

So the question is on how to modify the cached content to include the ones between timestamp A and B.

like image 214
syockit Avatar asked Nov 15 '22 02:11

syockit


1 Answers

create a local variable to store the JSON result of your ajax call.

create a local variable to store the Max_Last_Modified_Date.

create a javascript helper that will draw the JSON result.

create a server-side handler that will tell you MAX(LastModifiedDate) of your comments.

create a server-side handler that will return the comments in JSON for

  • All results (first fetch)
  • anything with modified date after the Max_Last_Modified_Date.
like image 92
sonjz Avatar answered Dec 04 '22 05:12

sonjz