Tormy Van Cool
Tormy Van Cool

Reputation: 801

Server is caching JS files or embedded HTML scripts?

I got an anomaly on the server of one of my sites, and I need for your help to find a solution.

If I open a PHP file downloaded by FtpZilla and I edit any PHP code, once I save and re-upload the file, the new content in active. The same for any HTML code

If on the same file I edit some HTML code in which there is a <script></script> tag and inside it javascript code, when I upload the new version of the file, the JS code is served to the browser is the old one. I tries with CTRL+F5, several browsers: no way out. The same disappointing result, cleaning the cache of each browser since "the beginning of the time"

Re-opening the just new uploaded file, astonishing I find my new code. It's like the server is serving old files. But why only the JS part?

Not only: I got the same identical issue for any JS external file. I mean the files are embedded with the tag <script src=""></script> into the HTML page

I tried to deactivate any caching command into .htaccess as you can see here below. But also in this case nothing better occurred.

What can I do further please?

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
# Enable expirations.
  ExpiresActive On

      # Cache all files for 2 weeks after access (A).
      #ExpiresDefault A1209600

      <FilesMatch \.php$>
        # Do not allow PHP scripts to be cached unless they explicitly send cache
        # headers themselves. Otherwise all scripts would have to overwrite the
        # headers set by mod_expires if they want another caching behavior. This may
        # fail if an error occurs early in the bootstrap process, and it may cause
        # problems if a non-Drupal PHP file is installed in a subdirectory.
        ExpiresActive Off
      </FilesMatch>

        #ExpiresByType image/jpg "access 1 year"
        #ExpiresByType image/jpeg "access 1 year"
        #ExpiresByType image/gif "access 1 year"
        #ExpiresByType image/png "access 1 year"
        #ExpiresByType text/css "access 1 month"
        #ExpiresByType application/pdf "access 1 month"
        #ExpiresByType text/x-javascript "access 1 month"
        #ExpiresByType application/x-shockwave-flash "access 1 month"
        #ExpiresByType image/x-icon "access 1 year"
        #ExpiresDefault "access 2 days"
    </IfModule>

EDIT: I just noticed that: if I create a symbolic link to the original JS file, when I go to read the file via Symbolic link (thus in the browser I call the SymLink instead of the file): I still get the old version. Even opening it (editing) by FileZilla. it's like the pointer is pointing to old version permanently. While if I go to read the JS file directly (calling it directly, not even via an embedded link) I can get the new version I'm getting lost more.

Upvotes: 1

Views: 2026

Answers (1)

Koval
Koval

Reputation: 145

ExpiresActive Off will actually enable Your browser to cache the content, as no caching rules are set by the server. If You want to disable caching You could use something like this:

<FilesMatch "\.(html)$">
    Header set Cache-Control "private, no-cache, must-revalidate"
    Header set Pragma "no-cache"
</FilesMatch>

This will tell Your browser to check for new content on every request. If content is not dynamically created (by php for example) and file was not modified since You last recieved it, apache will send a 304 Not Modified header with no data (since Your browser allready has the latest version)

Upvotes: 1

Related Questions