best way to invalidate CDN Cache when updating Django app?

Hello LES!

I am currently creating a django app, and trying to put the static files on S3 and serve it via CDN.

However, if I set the max-age to 1 year, and change the content on the static file, I have to manually invalidate CDN cache as well as browser cache, which can't be done on other user's browser.

The best way seems to be adding some kind of version number on the url (e.g. https://mydomain.com/static/file.js?v=3), but manually entering that on every file is a PITA.

Is there a way to efficiently do this, or is there a bettery way to achive similar results?

Thank you!

Comments

  • edited December 2020

    Most CDNs have a purge API that you can invoke to delete cached documents.
    Example: Cloudflare purge files by URL.

    The better way is still to change the URI whenever the file changes.
    Although you could purge CDN caches, the browser can still cache the documents.
    Having mixed versions would cause your webapp to behave erratically.

    You shouldn't be manually editing the version number everywhere, but should instead rely on the build pipeline to do that for you.
    Modern build tools, such as Parcel, can be configured to generate asset filenames that contain a hash of the file content, and then reference that filename wherever it's used.

    Thanked by (1)sanvit

    ServerFactory aff best VPS; HostBrr aff best storage.

  • sanvitsanvit OG
    edited December 2020

    @yoursunny said: the browser can still cache the documents

    This is why I was asking this question.

    @yoursunny said: rely on the build pipeline to do that

    So you mean this should be done on CI/CD and not the app itself? Parcel seems to be for Node.js apps. Does this apply to Django apps too?

  • This should be done on the CI/CD. If not, maybe you can make feature on your Django apps to hit CDN purge api ;)

    Curious, don't you use cloudfront? It has good integration with s3

  • @akhfa said:
    This should be done on the CI/CD. If not, maybe you can make feature on your Django apps to hit CDN purge api ;)

    Thanks, I think I should play something on Django's side for now/

    Curious, don't you use cloudfront? It has good integration with s3

    Yes, however, iirc changing the file on S3 doesn't purge cloudfront cache, and it obviously doesn't purge browser cache. :(

  • @sanvit said:

    Yes, however, iirc changing the file on S3 doesn't purge cloudfront cache, and it obviously doesn't purge browser cache. :(

    Yes, what you need is hit cloudfront invalidation after update file on s3

    Thanked by (1)sanvit
Sign In or Register to comment.