How can I add expire headers for scripts that are not on my server?
Asked Answered
M

9

20

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.

Mediatory answered 17/9, 2009 at 13:55 Comment(0)
B
22

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.

Balkin answered 17/9, 2009 at 14:3 Comment(1)
True, 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.Hydrangea
V
21

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.

Venessavenetia answered 17/9, 2009 at 14:8 Comment(4)
Interesting! Does this have any speed implications?Turnery
I 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.Iormina
You 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 thereSonyasoo
I would rather think about some frontend web cache serverSonyasoo
B
2

Thats not possible.

Not recommended (and not always possible): If its static content, prefetch it with a script and set your own headers.

Bilbo answered 17/9, 2009 at 14:5 Comment(0)
R
2

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');
Ratiocinate answered 17/9, 2009 at 14:6 Comment(2)
This will not work for all external scripts, I have tried for a magento site, but not working properly.Retardant
I 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.Iormina
D
1

I have made a version of that code that let you specify different expire dates for each scripts:

<?php

$files = array(
    'ga.js' => 'https://ssl.google-analytics.com/ga.js',
    'bsa.js' => 'https://s3.buysellads.com/ac/bsa.js',
    'pro.js' => 'https://s3.buysellads.com/ac/pro.js'
);

if(isset($files[$_GET['file']])) {
    if ($files[$_GET['file']] == 'ga.js'){
        header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + ((60 * 60) * 48))); // 2 days for GA
    } else {
        header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // Default set to 1 hour
    }

    echo file_get_contents($files[$_GET['file']]);
}

?>

More info: https://www.catswhocode.com/blog/php-how-to-add-expire-headers-for-external-scripts

Destruct answered 26/8, 2017 at 10:3 Comment(1)
I 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.Iormina
F
1

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.

Fendley answered 27/10, 2018 at 10:22 Comment(0)
B
0

You can't.

Try e-mailing the one(s) hosting the file and try to get them to apply expires-headers to it.

Buckie answered 17/9, 2009 at 14:2 Comment(0)
Q
-4

The following may be useful for you.

ExpiresActive On

ExpiresDefault "access plus 1 seconds"

ExpiresByType image/x-icon "access plus 2692000 seconds"

ExpiresByType image/jpeg "access plus 2692000 seconds"

ExpiresByType image/png "access plus 2692000 seconds"

ExpiresByType image/gif "access plus 2692000 seconds"

ExpiresByType application/x-shockwave-flash "access plus 2692000 seconds"

ExpiresByType text/css "access plus 2692000 seconds"

ExpiresByType text/javascript "access plus 2692000 seconds"

ExpiresByType application/x-javascript "access plus 2692000 seconds"

ExpiresByType text/html "access plus 600 seconds"

ExpiresByType application/xhtml+xml "access plus 600 seconds"

Quar answered 7/7, 2016 at 6:0 Comment(1)
Nope. You can only add this for your own js file. You can't add this for external js file.Helladic
C
-10

You may be able to add a query string parameter to fool the browser into thinking it's requesting a different resource. For example, if you want the browser to never cache a CSS, you can add a question mark followed by a random number to the end of the URL. This usually works but can be made to not work by the server hosting the file. Try it and see.

Conner answered 19/9, 2009 at 3:29 Comment(1)
This is done for versioning a file so as to ensure that browser ignores presence of previous versions and downloads latest version. Not applicable for this requirement though.Hydrangea

© 2022 - 2024 — McMap. All rights reserved.