A CDN, or Content Delivery Network, is a distributed network of servers located around the world that helps deliver website content, such as images, videos, scripts, and stylesheets, to users more efficiently. It improves website performance by reducing latency, optimizing file delivery, and increasing availability. CDNs store cached copies of files closer to users, resulting in faster loading times, reduced server load, and improved overall user experience.
One of the notable features of jsDelivr is its ability to serve files directly from GitHub repositories. This means that you can use a GitHub repository as a CDN, making it easy to host and distribute your project files. It allows you to specify file versions using GitHub release tags or commit hashes. This enables you to control which version of a file is served, providing better control over your project dependencies. By leveraging jsDelivr, you can easily eliminate the need for setting up and managing a separate CDN infrastructure, making it a convenient solution for developers. It’s a cost-effective approach. GitHub provides free hosting for open-source repositories, allowing you to serve your files without incurring any additional costs for hosting and distribution using jsDelivr CDN.
How to use a GitHub repository as a CDN with jsDelivr
Let’s say you have the following GitHub URL:
To distribute that file through the jsDelivr CDN you have to convert the URL to:
The above URL is not recommended for production environments because the code from that branch may change, especially if it is the main branch. Rather than
<branch> it’s encouraged to use
The CDN version will be:
Eventually you can use also
@latest in the CDN URL. This option is not recommended for production environments since the fallback will be the default branch if there are not tagged releases which makes unpredictable.
Optionally, you may generate the URLs from this tool provided by jsDelivr itself: https://www.jsdelivr.com/github
How to purge a file from jsDelivr CDN
Static Versions and commit hashes are cached forever. Versions (including
last) are cached during 7 days and branches 12 hours.
jsDelivr provides an easy way to purge a file from its CDN. Just go to https://www.jsdelivr.com/tools/purge and paste the URL you want. It will be generated again once the URL is requested.
The purge process might take a few moments to complete, depending on the number of files you have requested to be cleared from the cache. Once the purge is complete, the cache for the specified files will be cleared, and subsequent requests for those files will retrieve the latest version from your GitHub repository.
Minification support in jsDelivr
jsDelivr provides a easy-to-use way to generate the
min version. Just add
.min to any JS/CSS/SVG file to get a minified version. From the URL we used before, this is how it looks like with minification.
jsDelivr provides comprehensive documentation to help developers effectively utilize its features and functionalities. It includes examples, explanations of common scenarios to assist developers in using jsDelivr effectively. Access to the jsDelivr documentation at https://www.jsdelivr.com/documentation. It is a valuable resource for understanding the capabilities of jsDelivr and how to integrate it into your projects successfully.