\n\n\njsDelivr fulfills this purpose perfectly as it transforms GitHub URLs to the correct content types, facilitates efficient caching, and eliminates restrictions associated with GitHub's raw files.\n\nRemember to replace user, repo, version, and filename.js with your specific details. If the version is not specified, jsDelivr will default to serving the latest version, which might have been cached already, potentially obstructing recent updates.","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Nikita Barsukov","url":"https://explain.codes//author/nikita-barsukov"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2024-11-26T17:45:01.330Z","dateModified":"2024-11-26T17:45:02.974Z"}
Explain Codes LogoExplain Codes Logo

Link and execute external JavaScript file hosted on GitHub

javascript
cdn
github-pages
cors
Nikita BarsukovbyNikita Barsukov·Nov 26, 2024
TLDR

Embed and execute a JavaScript file from GitHub in a few simple steps using the raw content URL within a <script> tag:

<script src="https://cdn.jsdelivr.net/gh/user/repo@version/filename.js"></script>

jsDelivr fulfills this purpose perfectly as it transforms GitHub URLs to the correct content types, facilitates efficient caching, and eliminates restrictions associated with GitHub's raw files.

Remember to replace user, repo, version, and filename.js with your specific details. If the version is not specified, jsDelivr will default to serving the latest version, which might have been cached already, potentially obstructing recent updates.

Your GitHub-hosted JavaScript and the World Wide Web

Choosing your delivery vehicle

GitHub stores your repositories, but it isn't exactly designed to serve files as a Content Delivery Network (CDN). jsDelivr or GitHub Pages suit best the purpose of serving JavaScript files in your projects as they correctly set the MIME type to text/javascript, ensuring browsers execute your scripts properly.

The art of picking the right version

It's crucial to link a specific version, commit, or even a release tag in your URL, especially in production environments. This practice is your seatbelt against breaking changes that could come in future updates and jolt your site.

Handling caching like a boss

##Remember that caching is like a superpower - Great power comes with great responsibilities. Using CDNs could lead to the superhero of the web world (or rather, super-villain?) - long-term caching. So, make sure those version and release tag armor protect your site.

MIME, more than just a silent artist

Be aware of the MIME type mismatch issue when sourcing scripts directly from raw.githubusercontent.com. Browsers will throw a fit and refuse to execute these scripts! Always opt for a CDN service that addresses MIME type, like our friends jsDelivr or GitHub Pages.

Sometimes, life gives you lemons, or in this case, GitHub Pages. It's a reliable and straightforward alternative when your project needs files served with the correct MIME type. Create a gh-pages branch, and voila! MIME has never been managed better.

Options for the adventurous coder

Hosting with the GitHub Pages

Just like creating Frankenstein, build a gh-pages branch, but with more simplicity and less grave-digging:

1. Craft a `gh-pages` branch in your repository. 2. Implant your `.js` file into the newly created branch - commit and push. 3. Access the file via `https://user.github.io/repo/filename.js`.

No abnormal parts, no electricity, and MIME type is set correctly.

GitHub - A key player in your build process

In the Willy Wonka factory packed full of high-end options, why not fetch the raw file during your build process? Build tools like Webpack or Gulp can assist you in making this possible:

// Imagine harnessing the power of a build tool to fetch and include the GitHub file... // Take that, Charlie Bucket!

This approach bakes the script into your application cake, allowing for glazing with minification and bundling with other assets.

The security sentinel

Watch your back with CORS

Amidst all the fun, never forget the whispers from behind the curtains. Importing scripts from another domain like GitHub might call for the big guns – CORS (Cross-Origin Resource Sharing) or a careful Content Security Policy (CSP).

Know your environment

Understanding that GitHub isn't the perfect raw JavaScript host can avoid a potential browser brawl. Hotlinking a .js file directly from a GitHub repository might lead browsers to block script execution. Stay safe, choose a design-fit third-party service.