StudyBlog

CDN or Local for JS Libraries?

2025-07-14

When using a JS library, there are generally two options.

① Load from CDN

Just add one line to your HTML and you're done.

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

Pros

No npm setup needed. One line in HTML and it works. The simplicity is the biggest advantage.

Cons

Won't work if the user has no internet connection. Also, using latest as the version can cause unexpected breakage if the library updates.

② Manage Locally with npm

Install with npm install jquery and reference from node_modules/.

Pros

Version is locked in package.json, so management is reliable. Works offline too.

Cons

Slightly more complex to manage. Since node_modules/ is typically excluded via .gitignore, deploying to services like Cloudflare Pages requires extra consideration.

The Answer for Cloudflare Pages

Set npm install as the Build Command. Cloudflare will then generate node_modules/ at deploy time, keeping the setup consistent with local development.

That said, for a simple static site like this one, CDN is usually sufficient. Local management makes more sense for offline apps or systems where strict version control is required.

← Back to list