60

In one of my project I'm using an external image link to display a screenshot via the GitHub page, which automatically parses the README.rst

GitHub page of ROyWeb

I updated this screenshot on my web server several times in the last weeks and I just realised that it is not updated on the GitHub page since... well I think it never updated ;-) GitHub somehow downloaded the image and loads it from it's cache servers.

Within the README.rst, there is clearly the correct link:

Actual link to the ROyWeb screenshot

Which you can confirm, when you load the raw file:

raw README.txt on GitHub

But when I check the URL of the displayed image on the GitHub page, I get:

GitHub cached screenshot of ROyWeb

Does anyone know how to force a "recache"?

tamasgal
  • 24,826
  • 18
  • 96
  • 135

4 Answers4

85
curl -X PURGE {url of cached badge image}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Alex Skrypnyk
  • 1,331
  • 13
  • 12
  • 5
    This doesn't work for me. Is there another approach? My readme is a mess of missing images (mostly shields.io badges). Is this a known bug with any prospect of a fix? – orome Jan 19 '17 at 15:25
  • Doesn't do a thing for me either. – TheChymera Jul 05 '17 at 23:15
  • 1
    Thats amazing! Out of curiosity, where did you find this information. I dont think PURGE is a standard http method? – Michael Aquilina May 11 '18 at 09:08
  • 5
    @MichaelAquilina I don't know how he finds it but Github mentions it [here](https://help.github.com/articles/about-anonymized-image-urls/#removing-an-image-from-camos-cache). – 张实唯 Aug 27 '18 at 05:17
  • 5
    This does work indeed! If it doesn't for you, press the Ctrl key while clicking refresh, you're probably getting the image from your own cache (I had to do this). – BenMorel Apr 25 '19 at 09:43
  • @MichaelAquilina it's mentioned in the middle of this long (5 year!) issue: https://github.com/atom/markdown-preview/issues/207 – Mike Honey Jun 13 '20 at 12:29
  • 1
    Make sure to refresh with Ctrl-Shift-R to clear your browser cache. – makeworld Oct 24 '20 at 16:23
  • 1
    Today this method stopped working for me (after many months of almost daily use) - its returning "Access to this site has been restricted.". I tried adding an API token - same result. The image did eventually refresh, so perhaps this is no longer necessary? – Mike Honey Nov 11 '20 at 05:21
  • For those who find this not working, note that the `PURGE` _is_ case-sensitive. – wbharding Jan 19 '22 at 16:04
  • @MikeHoney, are you still getting the restricted response, or are you still using this process? I want to do this for hundreds/thousands of images and assume I will be blocked with some type of rate limiting. It is working for me on single requests, though. – Tim Ramsey Aug 16 '23 at 20:36
  • @TimRamsey tbh I gave up on github as a platform for dynamic images – Mike Honey Aug 20 '23 at 09:31
19

There is also a trick that can be useful. It is nothing but just adding a question mark at the end of the image extension.

![This is an automated blog post image using Azure Function](https://customurl.blob.core.windows.net/github/latestpost.png?)

See the question mark (?) at the end. You can also see the live example here in my GitHub profile.

You can see more about this here.

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
7

I had a look at what shields.io does.

It sets this header (Source):

Cache-Control: max-age=2592000
User
  • 14,131
  • 2
  • 40
  • 59
  • 2
    I had seen that idea [here](https://github.com/github/markup/issues/224) but had no idea what syntax to use with a raw URL. Following your second link showed me how to do it. Thanks. – Mars Aug 01 '17 at 04:13
2

I believe I've understood how to do this now. Instead of serving a raw png file for example, serve the image through a php file (many examples on SO).

Once you're displaying the image with PHP, add the following headers:

header('Cache-Control: no-cache');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() - 36000));

By setting the no-cache and having the cache expire in the past, I have found that GitHub's CDN updates the images automatically on every refresh.

b85411
  • 9,420
  • 15
  • 65
  • 119