Page 1 of 1

Force browser to cache file

Posted: Sun Dec 10, 2006 8:00 am
by GeertDD
Thanks to Firebug I'm now seeing more stuff that's going on behind the screens of my websites. I noticed that my jquery.js.php file never gets cached. After copying the output to a plain text file called jquery.js, caching works and page loading speed dramatically increases.

Code: Select all

<?php
// This is the jquery.js.php file.

header('content-type: application/x-javascript');
// I guess I need some kind of cache-control header over here.
// However, tried some things without success.

require '../config.php';
require SHARED_DOCROOT .'js/jquery/jquery.js';

?>


$(document).ready(function(){
	
	// Animate message box
	$('#message').slideDown('normal');

	// More jquery here...

	});
	
});

Posted: Sun Dec 10, 2006 8:39 am
by feyd
Did you have a question, or was this more of a blogger's rhetorical post?

Posted: Sun Dec 10, 2006 9:49 am
by GeertDD
How to make the jquery.js.php file cacheable?

Posted: Sun Dec 10, 2006 9:51 am
by aaronhall
Are you using

Code: Select all

<script src="xxx.js"></script>
Browsers won't cache if the script is printed to the HTML file.

Posted: Sun Dec 10, 2006 9:56 am
by GeertDD
aaronhall wrote:Are you using

Code: Select all

<script src="xxx.js"></script>
Yup.

Posted: Sun Dec 10, 2006 10:01 am
by aaronhall
I'm guessing your browser doesn't like the .php file extension. Are you using the "language" parameter in the script tag?

Posted: Sun Dec 10, 2006 10:10 am
by GeertDD
aaronhall wrote:I'm guessing your browser doesn't like the .php file extension.
Right, because I've tested it with a .js extension and then the file does get cached.
Are you using the "language" parameter in the script tag?
The language parameter? Do you mean the html "type" attribute?

Here's my html link to the file:

Code: Select all

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

Posted: Sun Dec 10, 2006 10:17 am
by aaronhall
I'm sorry -- you're right; "language" is deprecated. Anyway, I'm stumped. Are you sure that firebug isn't lying to you?

Posted: Sun Dec 10, 2006 10:49 am
by feyd
Why are you issuing a content type of application/x-javascript when you state in the document a content type of text/javascript?

Posted: Sun Dec 10, 2006 11:02 am
by GeertDD
The Internet media type for JavaScript source code is application/x-javascript, but the unregistered text/javascript is more commonly used.

http://en.wikipedia.org/wiki/Client-sid ... nvironment
However, changing this from one to the other doesn't make the file cacheable.