2

We have a website (pure html/js/css). In our site pages, we have links to PDF files, these links looks like this: <a href="../files/myFile.pdf">My file link</a>. So, the problem: sometimes we need to update our PDFs. We update these files, then go to the website, press the link - and we see old file. Ctrl+F5, or F5 in Firefox - and we see updated file. It is not good. I think that this problem caused by caching.
In our site pages (html pages, which contain links), we added such tags:

    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">

But it does not help. How can we avoid client-side caching? I read some questions here. This solution had been taken from stackoverflow) Also I know about adding random parameter into page URL, but some guys say that it is bad way.
Please, help with this problem. Can we avoid client-side caching only with JS? Maybe it is possible with .htaccess file on server side, but we have no access there.

user3533397
  • 191
  • 1
  • 2
  • 14
  • That should prevent caching You could use linking to your docs with sample.doc?abc and replace the abc with random garbage. – Grumpy Feb 04 '20 at 09:09
  • Thanks for your response, but I wrote that it is not good solution. And where is a problem: on page caching or file caching? I suppose that it is in page: because link text is old. Am I right? – user3533397 Feb 04 '20 at 09:11
  • No, it does not. Sure, if user press F5, link will be updated. The problem is: when user opens site, watches our page with link (for instance, Page1), closes tab in browser, this page goes into cache. We update file. User opens our Page1 again - and it contains old link. F5 - and it contains new link. How can we avoid this F5 pressing?) Because sometimes user doesn't know that he must press F5. – user3533397 Feb 05 '20 at 14:34

1 Answers1

1

You can disable caching in the Nginx or Apache web server:

    location /pdf_file_location {
        root /your/site/public;
        index index.html;

        # kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
    }

More informations about this you can read here

Or in the simplest way, add a variable parameter to the URL(you can generate sequence, timestamp, fixed version of the file or any other random value):

<a href="../files/myFile.pdf?v=1580807492744">My file link</a>

?v=1580807492744 - if the value changes, the cache will not work

Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42
  • Ok, thanks, I'll read this info. But are you sure that the problem is only in file name? When we go to the page which contains a link and hover cursor over the link, we see old file address. After F5 - new address. Maybe the problem is in page, not in file? – user3533397 Feb 04 '20 at 09:16
  • 1
    Yes I'm sure. When you click the link, web browser saves the cache and when you click the same link again, the browser reads the cache. When you add random value as a unused GET parameter(as I showed above) the browser considers this to be a different URL without a cache - If you don't try, you won't find out :) – Krzysztof Raciniewski Feb 04 '20 at 09:25
  • 1
    We tried to rename file (old name - myFile.pdf, new name - myFile1.pdf). And when we go to the page contains link and hover on the link, we see myFile.pdf. So the link text didn't update. – user3533397 Feb 04 '20 at 09:27
  • Do you even upload the new page? – Grumpy Feb 04 '20 at 11:28
  • I think you should disable index.html file cache: https://stackoverflow.com/questions/41631399/disable-caching-of-a-single-file-with-try-files-directive This should solve the problem – Krzysztof Raciniewski Feb 04 '20 at 11:53
  • Is it possible to do only with Javascript? We have no access to server-side code.. – user3533397 Feb 04 '20 at 13:09
  • If you use Apache2 web server then maybe try disabling cache in .htaccess file. Do you know what web server is installed on the machine? – Krzysztof Raciniewski Feb 04 '20 at 14:18
  • Unfortunately, we don't know. – user3533397 Feb 04 '20 at 15:25