1

I am trying to append a query string to the end of all .js files, without actually making a code change. The purpose of this would be so static files get pulled from the server rather than the cache when they are changed so we don't have any stale static files.

So when my html says

<script type="text/javascript" src="file1.js">

I want it to actually pull

<script type="text/javascript" src="file1.js?v=1">

from the server. Is this possible?

So far I have:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$ 
RewriteRule ^(.*)\.js$ /$1.js?v=1 [L,R]

But I don't think it's quite doing what I want it to...

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • 2
    If the resource is cached with an expiry date, the browser will never make the request in the first place. Why not simply set the caching rules properly instead? – Pekka Sep 26 '13 at 01:52
  • @Pekka웃: the questioner might not understand how the caching rules are actually applied - you might want to explain that in an answer. – Qantas 94 Heavy Sep 26 '13 at 01:56
  • @Qantas yeah, in the process of looking for a link – Pekka Sep 26 '13 at 01:56
  • possible duplicate of [Prevent http file caching in Apache httpd (MAMP)](http://stackoverflow.com/q/11532636) – Pekka Sep 26 '13 at 01:56
  • I don't want to not cache them, and I don't want to set them to expire after a certain amount of time. I would set them to never expire and when the files are changed, the server would effectively change their name and recache the new file. Something similar to this [force-reload-css-javascript-unique-filenames](http://www.electrictoolbox.com/force-reload-css-javascript-unique-filenames/) but without making all the changes in the html. Just making an apache change. – user2817582 Sep 26 '13 at 02:24
  • You should change the file name, or path to the file, rather than appending a query string variable - it's a more reliable way to bust the cache, and better for when you do want the file cached. – Tom Pietrosanti Jan 22 '14 at 20:08

3 Answers3

0

This doesn't make sense: if the resource is cached with an expiry date, the browser will never make the request in the first place.

The much more straighforward way is to set up proper caching rules for the JS file as outlined here: How to prevent http file caching in Apache httpd (MAMP), adjusting the filesMatch directive to your liking.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

This will double the number of requests for the files (all .js files in this case), but it should work otherwise.

I'm using a directory (effectively modifying the filename), rather than a query string because that is the preferred technique. [1][2]

First, redirect to a different folder name

ReWriteRule ^(.*\.js)$ /rev/000/$1 [L,R]

Replace the 000 part with your revision number (you should be able to automate this)

The L flag stops processing rewrites, and the R does an HTTP redirect, initiating a second request to your server using the new file name.

Then catch requests tagged using the new "folder" and point them back to the original file using a rewrite rule that does not invoke another request (your first rule will be passed over this time around)

ReWriteRule ^/rev/[0-9]+/(.*)$ $1
Tom Pietrosanti
  • 4,184
  • 2
  • 24
  • 29
0

Ok I know the question is old, but while looking for something similar I found two clever Apache plugins:

mod_pagespeed:

This thing rewrites the html page that you are serving and replaces the referenced static resource links (CSS, JS, imgs) with modified ones. When static resource on disk changes, the plugin will generate a completely new url for it. There is some content based hash included I guess. And this busts the cache because the client will have to make a new request.

So caching time of the original html page should be rather short, but all related static resources can be cached for extremely long times.

mod_proxy_html:

This one also can edit html on the fly. But it is a lot simpler and only offers rewriting links, no minification and other fancy stuff like in mod_pagespeed. But sometimes this is all what you need, as this probably mean there is less risk that page will be broken by rewriting.

tporeba
  • 867
  • 10
  • 23