Are you using GitHub for hosting your projects?
Do you also copy or clone the repository to another service so as the files can be served by CDN (for example css or js files)?
Would your world be better if you wouldn’t have to do that and you could just link to your files from GitHub directly?
Well, you can!
Hi guys 👋 !
Today I found out about really great integration between GitHub and JSDelivr.
What those two have in common is that by changing the hostname in a github link, for example:
to a hostname of the CDN service JSDelivr:
…your files are available from the CDN just like so 😊.
How does it work
The process of accessing your GitHub files from JSDelivr CDN is as follows:
- Find a raw version of your file on GitHub, for example:
https://cdn.jsdelivr.net/gh/(don’t forget the suffix /gh/!)
- Remove “master” from the link, because JSDelivr recignizes that automatically
What about browser caching?
That’s very good question.
Provided that you’d include a link like this one on your website:
…it would get cached and when you update the file on GitHub, it would not get updated.
That’s where git tags come into play.
So when you tag a commit and push it, you can easily modify the JSDelivr link to load your file from exact tag, for example:
Just notice that
@1.0.0 after the repository name.
What do you guys think about this? 🤔 Have you known about this GitHub feature? 🤔 Or have you already used it?
Of course, this approach will definitely NOT work on larger projects where there needs to be a system in place as far as the source files are concerned, but for personal projects I think this is a very good choice!
Let me know in the comments, what do you guys think! 🤘
Until next time, 🤓
Yours in coding,
Credits for emoji in the featured image: pinterest.com/pin/478929741602641099/