How can I add expire headers for scripts that are not on my server?
Solution 1
You can only add header fields in responses to requests that go to your own server. If the request goes to another server, say Google’s server, than it’s Google’s server that answers the request.
So the only solution to your problem is hosting that external resources on your own server. But that’s only possible if that resources are static, do not change from request to request and do not depend on other things.
Solution 2
The only way is to create script which downloads contents from external site and then adds needed headers.
<script type="text/javascript" src="http://external.example.com/foo.js"></script>
To
<script type="text/javascript" src="external.php?url=http://external.example.com/foo.js"></script>
And external.php is something like
<?php
header("Expire-stuff: something");
echo file_get_contents($_GET['url']);
Of course this has security hole so I'd recommend to use identifier strings like external.php?file=foo.js and then using
$files = array('foo.js' => 'http://external/...');
if(isset($files[$_GET['file']]))
{
echo file_get_contents($files[$_GET['file']]);
}
file_get_contents() of course will take some of your bandwith so it would be recommended to cache the result also.
Solution 3
You could dynamically load the external pages using PHP, so you can send headers before outputting the original data. This is not an ideal solution but if you really have to you may want to use it.
<?php
header('expire-header');
echo file_get_contents('http://www.extern.al/website/url');
Solution 4
Thats not possible.
Not recommended (and not always possible): If its static content, prefetch it with a script and set your own headers.
Solution 5
Don't lose your mind for these page tests... some of the recommendations may be useful and some of them you can't do anything. Do whatever you can do with your own files, don't mind about external ones.
Comments
-
DanTdr over 3 years
I have a website and I added the expire headers on all pages/images and scripts but I don’t know how I could add expire headers to external scripts.
For example Google Analytics - it has expire headers set to 1 day.
Google is not my problem, some other scripts from external websites are the real problem, they don't have expire headers at all.
-
prajosh about 8 yearsThis will not work for all external scripts, I have tried for a magento site, but not working properly.
-
502_Geek over 7 yearsNope. You can only add this for your own js file. You can't add this for external js file.
-
v3nt over 7 yearsInteresting! Does this have any speed implications?
-
Zakir hussain almost 6 yearsI don't think it's a good idea because now the browser might be able to take the scripts from the cache more often, but the server has to download the scripts in every case before sending any output. Even taking into account the possibly better bandwidth of the server, I don't think this will really speed up things; it just makes the PageSpeed results look better only.
-
Zakir hussain almost 6 yearsI don't think it's a good idea because now the browser might be able to take the scripts from the cache more often, but the server has to download the scripts in every case before sending any output. Even taking into account the possibly better bandwidth of the server, I don't think this will really speed up things; it just makes the PageSpeed results look better only.
-
Zakir hussain almost 6 yearsI don't think it's a good idea because now the browser might be able to take the scripts from the cache more often, but the server has to download the scripts in every case before sending any output. Even taking into account the possibly better bandwidth of the server, I don't think this will really speed up things; it just makes the PageSpeed results look better only.
-
NitinSingh about 5 yearsTrue, also its best to ensure that owners of a script manage when that's supposed to expire, and usually they put the caching policy as such.
-
Bartłomiej Sobieszek about 4 yearsYou can't use that, you are killing the reasons why things like CDN or S3 exists by proxying these resources. It gives you no win there
-
Bartłomiej Sobieszek about 4 yearsI would rather think about some frontend web cache server