www.webdeveloper.com
Results 1 to 2 of 2

Thread: Handling the Cache problem

  1. #1
    Join Date
    Sep 2006
    Posts
    655

    Question Handling the Cache problem

    Hi

    As you know applications like gmail / yahoomail they constantly keep changing their js/css and when you login you dont have to clear your browser cache to see the new changes, they are automatically shown to you.

    How do they do this? I see there are two approaches:

    1) in your code where you specify the path of the js/css you append time with a querystring that will force your browser to load the lastest js/css no matter it has been modified or not.

    2) You use the "Pragma: No-cache" Tag in your page.

    But the problem in these approaches is that your application makes an HTTP request every time its called since it assumes the file is new.

    So whats the solution?


    Any help is appreciated.
    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,314
    Don't know how they do it, but I just thought of this: have the main app get the last mod time of the file in question, and use that as a (unused) query string value.
    PHP Code:
    <?php
    $jsFile 
    '/path/to/file.js';
    $jsFileUrl $jsFile "?v=" file_mtime($_SERVER['DOCUMENT_ROOT'] . $jsFile);
    ?>
    <script type='text/javascript' src='<?php echo $jsFileUrl?>'></script>
    This way the browser could cache it until the resource file is changed and gets a new mod time.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles