Getting started
A CDN (content delivery network) serves files from servers close to your visitors. Instead of downloading a library and hosting it yourself, you point your page at a copy hosted on jscdns.net. There is nothing to install and no account to create.
The quickest way to start is to pick a library from the catalogue, copy the tag shown under Quick start and paste it into your HTML. Here is a complete page that loads Bootstrap's stylesheet and JavaScript:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My page</title>
<link rel="stylesheet" href="https://jscdns.net/libs/bootstrap/5.3.8/css/bootstrap.min.css"
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
</head>
<body>
<div class="container py-5">
<h1>Hello, world</h1>
<button class="btn btn-primary">It works</button>
</div>
<script src="https://jscdns.net/libs/bootstrap/5.3.8/js/bootstrap.bundle.min.js"
integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
</body>
</html>URL structure
Every hosted file follows the same pattern:
https://jscdns.net/libs/{library}/{version}/{file}- library is the short name shown on the library's page, such as
vue,chartjsorbootstrap. - version is an exact release number such as
3.5.44. There are no ranges or aliases like latest. - file is the path of the build, which may include a folder, for example
css/bootstrap.min.css.
Because each URL points at one exact release, the contents behind it never change. That is what makes it safe to use integrity hashes and long browser caching.
Adding a script tag
Most libraries are published as a classic browser build which creates a global variable when it loads. For example, Vue creates Vue and jQuery creates $ and jQuery. Load the library first, then run your own code:
<script src="https://jscdns.net/libs/vue/3.5.44/vue.global.prod.js"
integrity="sha384-tORx0bXmMnTC28EYQe1coaGZKSQupXQEYDBNex8xkzJ9iqUoNy9Y+VFBhVxi4kUr" crossorigin="anonymous"></script>
<div id="app">{{ message }}</div>
<script>
Vue.createApp({
data() {
return { message: 'Hello from Vue' };
}
}).mount('#app');
</script>Your own script must come after the library tag. If it runs first, the global variable will not exist yet and you will see an error such as Vue is not defined.
Prefer the files ending in .min.js or .prod.js on live sites. They are minified and smaller to download. Unminified development builds are listed where they exist and are useful while debugging.
Adding a stylesheet
Some libraries ship CSS as well as JavaScript. Stylesheets belong in the <head> so the page is styled before it is shown:
<link rel="stylesheet" href="https://jscdns.net/libs/bootstrap/5.3.8/css/bootstrap.min.css"
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">If you write your own CSS to override a library, place your stylesheet after the library's so your rules take priority.
Subresource Integrity
Subresource Integrity (SRI) is a browser security feature. The integrity attribute holds a cryptographic hash of the file you expect to receive. Before running the file, the browser hashes what it actually downloaded and compares the two. If they do not match, the file is blocked and nothing runs.
This protects your visitors if a file is ever changed, whether by accident or by an attacker, between our servers and their browser. Every tag on this site includes a SHA-384 hash:
<script src="https://jscdns.net/libs/jquery/4.0.0/jquery.min.js"
integrity="sha384-fgGyf7Mo7DURSOMnOy7ed+dkq5Job205Gnzu6QIg0BOHKaqt4D76Dt8VlDCzcMHV" crossorigin="anonymous"></script>The crossorigin="anonymous" attribute is required alongside integrity. It tells the browser to fetch the file without sending cookies, which is what allows the integrity check to take place for a file on another domain. jscdns.net sends the CORS headers needed for this to work.
You can generate a hash for any file yourself with OpenSSL:
curl -s https://jscdns.net/libs/jquery/4.0.0/jquery.min.js \
| openssl dgst -sha384 -binary | openssl base64 -ALoad order, defer and async
A plain <script> tag stops the browser from parsing the page until the file has downloaded and run. There are three common ways to handle this:
| Approach | What happens | Use when |
|---|---|---|
Before </body> | The page is parsed first, then scripts run in order. | The simplest choice and works everywhere. |
defer | Downloads in parallel, then runs in order once the page is parsed. | Scripts in the <head> that depend on each other or on the page. |
async | Downloads in parallel and runs as soon as it arrives, in any order. | Independent scripts such as analytics. Avoid for libraries your code depends on. |
Alpine.js is a good example of a library that expects defer:
<script defer src="https://jscdns.net/libs/alpinejs/3.17.5/cdn.min.js"
integrity="sha384-FYLKjwG3aohNhNZoVG8sY/5ntunm+MqylL90Q+rT3OM7AwV9/BKFjk/RjoIpIgsI" crossorigin="anonymous"></script>ES modules
Modern browsers support JavaScript modules natively. Where a library publishes a module build we host it too, labelled ES module on the library page. Instead of relying on a global variable, you import exactly what you need:
<div id="app">{{ message }}</div>
<script type="module">
import { createApp } from 'https://jscdns.net/libs/vue/3.5.44/vue.esm-browser.prod.js';
createApp({
data() {
return { message: 'Hello from a module' };
}
}).mount('#app');
</script>Modules are always deferred, run in strict mode and keep their variables private to the module. They must be loaded over HTTP or HTTPS, so open your page through a local web server (for example npx serve or python3 -m http.server) rather than double clicking the file.
Import maps
An import map lets you give a library a short name, so your code can say import from 'vue' rather than repeating the full URL everywhere. It also keeps the version in one place, which makes upgrades easier. The import map must appear before any module that uses it:
<script type="importmap">
{
"imports": {
"vue": "https://jscdns.net/libs/vue/3.5.44/vue.esm-browser.prod.js"
},
"integrity": {
"https://jscdns.net/libs/vue/3.5.44/vue.esm-browser.prod.js": "sha384-Yy+wUwjypE8OL2+tZ7lLLMsIVMFBXibFJCdzCWWWXnuVVdiuS3gViCyubnRyWnSP"
}
}
</script>
<script type="module">
import { createApp } from 'vue';
// ...
</script>The optional integrity section applies SRI to imported modules. It is supported in current versions of Chrome, Edge, Firefox and Safari. Browsers that do not understand it will still load the module, just without the integrity check.
Some libraries, such as three.js, are only published as modules and are designed to be used this way.
Local fallbacks
If you need your page to keep working when a CDN cannot be reached, for example on a network that blocks external domains, you can check whether the library loaded and fall back to a copy on your own server:
<script src="https://jscdns.net/libs/jquery/4.0.0/jquery.min.js"
integrity="sha384-fgGyf7Mo7DURSOMnOy7ed+dkq5Job205Gnzu6QIg0BOHKaqt4D76Dt8VlDCzcMHV" crossorigin="anonymous"></script>
<script>
window.jQuery || document.write('<script src="/js/jquery.min.js"><\/script>');
</script>For most sites this is not necessary, but it is a sensible precaution for internal applications or pages that must work offline.
Content Security Policy
If your site sends a Content Security Policy header, you need to allow jscdns.net as a source for scripts and styles. A minimal policy looks like this:
Content-Security-Policy: default-src 'self'; script-src 'self' https://jscdns.net; style-src 'self' https://jscdns.netFor a stricter policy you can allow individual files by their hash rather than the whole domain. Using SRI as well means a file is only run if both the policy and the hash agree.
Versions and caching
jscdns.net only serves exact versions. We deliberately do not offer a latest URL, because a library updating underneath your site can break it without warning and would make integrity hashes impossible to use.
Files are served with long lived cache headers. Once a visitor's browser has downloaded a particular version, it can reuse it without asking again. When a new release of a library is added, it appears alongside the existing versions at a new URL, and older versions remain available.
To upgrade, visit the library's page, copy the new tag (which includes the new hash) and test your site before deploying.
JSON index
A machine readable list of every library, file, URL and integrity hash is available at /libs/index.json. It is useful for scripts and build tools that need to look up the current version of a library.
const res = await fetch('https://jscdns.net/libs/index.json');
const index = await res.json();
const vue = index.libraries.find(lib => lib.slug === 'vue');
console.log(vue.version, vue.files[0].url, vue.files[0].integrity);