Force browser to cache file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Force browser to cache file

Post 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...

	});
	
});
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Did you have a question, or was this more of a blogger's rhetorical post?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

How to make the jquery.js.php file cacheable?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

aaronhall wrote:Are you using

Code: Select all

<script src="xxx.js"></script>
Yup.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I'm guessing your browser doesn't like the .php file extension. Are you using the "language" parameter in the script tag?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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.
Post Reply