@codewithjonam

Blog

Sharing knowledge through the art of words

How to use GitHub as CDN for assets

June 17, 2024
While building my portfolio site, embedding images and code snippets into my blog posts was bloating the source code and would likely turn into a tangled mess over time.

Hence, I was looking for a free CDN service to handle these assets and found that we can use GitHub as a CDN for hosting assets.

Here's a step-by-step guide:
  • Go to GitHub and login to your account
  • Create a new repository by clicking on the New button
  • Dashboard
  • Provide Repository name (Ex:
    my-assets-cdn
    ), select Public and click on Create repository
  • New Repository
  • Next, upload assets that you want to serve via the CDN
    • Click on the link uploading an existing file
    • Repository
    • Upload file, add commit message and click on Commit changes
    • Upload Assets
  • Now, you can access the uploaded file using the below url
  • 1https://cdn.jsdelivr.net/gh/{username}/{repositoryname}@{branchname}/{filename}
  • Replace
    {username}
    ,
    {repositoryname}
    ,
    {branchname}
    and
    {filename}
    accordingly
  • For the file that i uploaded earlier, the url will be as below
  • 1https://cdn.jsdelivr.net/gh/thejonamreddy/my-assets-cdn@main/Manoj Kumar Reddy P Resume.pdf
  • You can even access files under folders as below
  • 1https://cdn.jsdelivr.net/gh/{username}/{repositoryname}@{branchname}/{foldername}/{filename}

Using GitHub as a CDN for your assets is a great solution for developers looking for a free and reliable hosting option. However, please note that this will not replace dedicated CDN services for high-traffic websites. This is a great alternative for smaller projects, personal websites, or development purposes.

I hope this was helpful!
GitHubCDN