Your server needs to support the If-Modified-Since
request header (and/or other conditional get headers). These headers are sent by the web browser with every (conditional) get request.
A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s)
In essence, your server needs to:
- Check the presence of these fields in the request header.
- Use these conditions to determine if to serve the page or return an HTTP 304 response.
A search in google will give you numerous PHP examples. Like this one.
Edit:
Separate server logic from client logic and markup.
You could do something like this:
Server:
Create a PHP script (let's name it getjs.php
) which returns the minified JS content, or 304 response if nothing has changed:
<?php
if (/*Determine if need to return 304*/){
header('HTTP/1.0 304 Not Modified');
exit;
}
$content = NULL;
$jsFiles = array();//all files
foreach($jsFiles as $file) {
$content .= file_get_contents($file);
}
$minifiedContents = jsMinify($content);
header("content-type: application/javascript");
echo $minifiedContents;
?>
Client:
<script src="getjs.php"></script>