Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Hosting Your Scripts from GitHub to jsDelivr: A Step-by-Step Guide
Next article icon

Hosting Your Scripts from GitHub to jsDelivr: A Step-by-Step Guide

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Optimizing the performance and reliability of your applications is very important. One effective way to achieve this is by using a Content Delivery Network (CDN) to host and serve your scripts. jsDelivr, a free and powerful CDN, offers a seamless way to host your scripts directly from your GitHub repository.

In this article, we will guide you through the process of using jsDelivr to host your scripts, ensuring faster load times and greater global availability. Let's get started.

What is jsDelivr?

jsDelivr is a free, open-source CDN that allows you to host and serve your scripts directly from repositories on GitHub, npm, and the theme and plugin directories for WordPress. It ensures fast load time by serving your files from a global network of servers, providing high availability and performance.

Steps To Host Scripts To jsDelivr

Step 1: Creating a Repository on GitHub and Adding a JavaScript File

  1. Create a GitHub Repository:
    • Login to your GitHub account and create a repo as usual (preferred to keep the repo public) .
    • You can check a step by step guide here.
  2. Add a JavaScript Script:
    • Create a .js file and upload it to the repo.
    • For now we will create a simple js file that will print "Hello World" on the console.
    • We are creating a function and writing all the code inside it to avoid ant conflicts with global variables. Also we are immediately calling it as well.
JavaScript
(function() { function printHelloWorld() {     console.log("Hello world from script hosted on     	github repo and deliverd through jsDelivr"); } })(); 
Screenshot-2024-07-31-221206
Hosted script from github.

Step 2: Creating a CDN Link for the Script

  1. Go to jsDelivr and create the user defined format to create the CDN link to your js flie.
  2. For example, if your username is 'username', the repository name is 'my-repo' and the script is in the root directory, the URl will be: https://cdn.jsdelivr.net/gh/username/my-repo@main/script.js
  3. Replace username and my-repo with your actual GitHub username and repository name.

Step 3: Importing the js file inside HTML file

  1. Open your HTML file.
  2. Add the following script tag in the <head> or <body> section:

<script src="https://cdn.jsdelivr.net/gh/username/my-repo@main/script.js" async='true' crossorigin='anonymous'></script>

  • Here we are using crossorigin='anonymous' to ensure that the script is fetched without credentials (such as cookies, client-side SSL certificates, or HTTP authentication). For more details refer to this article.
  • Also we used asyns='true' to make it load parallel with other resources (like CSS and images).
Screenshot-2024-07-31-221012
Importing the script using script tag under head section

Version Control Best Practices

The jsDelivr used to cache the files on their server. So, in case you update your files, it might take up to 7 days to reflect on your HTML page. For checking the changes immediately onto your website, you can use versioning in your file. Tag each update with a different version in your github. And then you can call different versions of your files just by modifying the CDN link a little bit.

  • Include Version Numbers: Add version number to your filenames or commit messages (e.g. 'script-v1.0.0.js').
  • Update the CDN link: Modify the jsDelivr link to reference the new version. For example, change from' https://cdn.jsdelivr.net/gh/username/repo/script.js' to 'https://cdn.jsdelivr.net/gh/username/repo/[email protected]'.
  • Maintain Backward Compatibility: Ensure that older version remain accessible if needed, but update link to your projects to use the latest version.

Next Article
Hosting Your Scripts from GitHub to jsDelivr: A Step-by-Step Guide

A

aayush10m
Improve
Article Tags :
  • Web Technologies
  • Git
  • GitHub
  • HTML5
  • javaScript

Similar Reads

    First Open Source Contribution to GitHub - A Step By Step Guide
    Contributing to open-source projects on GitHub is a rewarding experience that allows you to collaborate with developers worldwide, improve your skills, and make a positive impact on the tech community. This step-by-step guide will walk you through the process of making your first open source contrib
    4 min read
    MERN Stack Project Deployment - A Step-by-Step Guide
    Deploying a MERN Project is an important step in making your app accessible to all. This process involves moving your application from a development environment to a production server where it can handle real traffic. Proper deployment ensures that your website runs smoothly, securely, and efficient
    3 min read
    How to Deploy Your React Websites on GitHub?
    Building a web application is always exciting for developers, especially when you step into the programming world for the first time. You build the front end of your application after a lot of struggle, and you want to showcase your skill, your creativity, and of course, your hard work to the world.
    6 min read
    How to use npm Scripts as a Build Tool ?
    In the world of JavaScript development, npm (Node Package Manager) isn't just for installing packages. It also allows you to define and run scripts to automate various tasks within your project. One can make use of npm packages within your scripts to extend functionality. For instance, you can emplo
    3 min read
    Making your first Open Source Pull Request | Github
    Open Source softwares are softwares for which the original source code is made freely available and may be redistributed and modified. As a Programmer, we are more interested in how to contribute to their codebase. A lot of newcomers find Open Source to be dreadful and daunting. But worry not, every
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences