Blog › Jekyll

Offloading GitHub Pages blog images to jsDelivr CDN

Keep local relative image paths in VS Code while automatically rewriting production assets to jsDelivr CDN using Jekyll multi-config builds on GitHub Pages.

Offloading GitHub Pages blog images to jsDelivr CDN

GitHub Pages enforces a soft 100 GB monthly bandwidth limit, and high-resolution screenshots burn through it faster than HTML ever will. This post shows how to offload production image traffic to jsDelivr CDN using Jekyll’s --config override—while keeping local /assets/images/... paths working in VS Code and localhost:4000.

Tested with GitHub Pages, Jekyll 4.4, jekyll-theme-chirpy, and jsDelivr GitHub CDN (cdn.jsdelivr.net/gh/...).

1. The problem: GitHub Pages bandwidth limits and image weight

GitHub Pages is hard to beat for free static hosting, but the GitHub Pages usage guidelines spell out clear limits:

  • Monthly bandwidth (soft limit): 100 GB/month
  • Recommended repository size: under 1 GB
  • Build rate limit: 10 builds/hour

HTML, CSS, and JavaScript on a static blog weigh a few kilobytes per page. High-resolution PNG screenshots and architecture diagrams, on the other hand, often run 300 KB to 1 MB each.

If a single post contains three images totaling 2 MB, every reader who loads that article consumes 2 MB of bandwidth. As organic search traffic grows—or when crawlers re-index the site—100 GB disappears faster than you’d expect. Offloading image traffic to an external CDN leaves GitHub Pages serving only lightweight text assets.


2. The DX trade-off: why hardcoding CDN URLs breaks local writing

The simplest workaround—pasting full CDN URLs like https://cdn.jsdelivr.net/gh/... directly into Markdown—wrecks day-to-day writing ergonomics:

  1. Broken VS Code Markdown preview: Newly captured screenshots don’t exist on the remote branch yet, so previews render broken image icons until you push.
  2. Broken local server checks: Running bundle exec jekyll serve locally still tries to fetch images over the network from the remote CDN instead of reading local disk.
  3. Manual URL boilerplate: Every screenshot requires hand-building a long CDN URL instead of dropping a relative path.

What I wanted instead was clean environment separation:

Write /assets/images/... relative paths locally so VS Code and localhost:4000 render straight from disk, and rewrite those paths to the CDN automatically only during the GitHub Actions production build.


3. How Chirpy’s cdn setting and Jekyll multi-config solve it

Digging into jekyll-theme-chirpy’s templates, I found that the theme already has a CDN prefix pipeline built in.

1) Chirpy’s _includes/media-url.html

Whenever Chirpy renders a post image, cover thumbnail, or avatar, it passes the path through _includes/media-url.html:

1
2
3
4
<!-- Excerpt from Chirpy core: _includes/media-url.html -->
{% if site.cdn %}
  {% assign url = site.cdn | append: '/' | append: url %}
{% endif %}

Whenever site.cdn is non-empty, Chirpy automatically prepends that base URL to every root-relative media path (/assets/images/...).

2) Splitting local and production configs with --config

Jekyll accepts a comma-separated list of configuration files via --config, where later files override keys from earlier ones:

1
bundle exec jekyll build --config _config.yml,_config_prod.yml

Combining both mechanisms gives us zero-compromise environment isolation:

  • Local development (_config.yml only): Leave cdn: blank so Jekyll serves images from local disk.
  • Production CI (_config.yml,_config_prod.yml): Override cdn: with the jsDelivr endpoint only inside GitHub Actions.

Environment-isolated architecture separating local image paths and production CDN URLs Figure 1. Local builds read /assets/images/... from disk, while CI builds prepend the jsDelivr CDN origin via _config_prod.yml.


4. Why jsDelivr (and when not to use it)

Among free static asset CDNs that pull directly from public GitHub repositories, I picked jsDelivr for four reasons:

  • Zero account setup: No signup, credit card, or API token required.
  • Multi-CDN Anycast network: Routes across Cloudflare, Fastly, and Gcore edge networks for consistent global latency.
  • Direct GitHub URL syntax:
    1
    
    https://cdn.jsdelivr.net/gh/<github-user>/<repo>@<branch>/<path>
    
  • Clean HTTP defaults: Serves HTTP/2, proper Content-Type, and access-control-allow-origin: * headers out of the box.

Comparing asset offloading options

OptionCostRepo stays on GitHubExtra upload stepBest fit
GitHub Pages onlyFreeYesNoneLow-traffic or text-only blogs
jsDelivr (@main)FreeYes (public repo required)NonePublic Jekyll blogs on GitHub Pages
Cloudflare R2 / S3 + CDNFree tier / lowNo (external bucket)YesPrivate repos or > 1 GB media libraries

When not to use jsDelivr

Skip jsDelivr in two situations:

  1. Your blog repository is private: jsDelivr only mirrors public GitHub repositories.
  2. You store very large binaries or videos: jsDelivr enforces per-file size limits (50 MB) and GitHub still recommends keeping the underlying Git repository under 1 GB. For video-heavy or multi-gigabyte media archives, use object storage like Cloudflare R2 or S3 instead.

5. Implementation

Step 1: Create _config_prod.yml

Create a new file named _config_prod.yml at the repository root and set cdn to your public repository’s jsDelivr endpoint:

1
2
3
4
5
6
7
8
# _config_prod.yml
# Production-only override loaded during GitHub Actions deployment:
# bundle exec jekyll build --config _config.yml,_config_prod.yml

# Global image CDN endpoint (jsDelivr Multi-CDN)
# Automatically prepended to root-relative media paths (/assets/images/...).
# Kept empty in _config.yml so local builds read from disk.
cdn: 'https://cdn.jsdelivr.net/gh/cmsong111/blog@main'

Leave cdn: empty in _config.yml.

Step 2: Update the GitHub Actions build command

In .github/workflows/pages-deploy.yml, pass --config _config.yml,_config_prod.yml to the Jekyll build step:

1
2
3
4
5
6
7
# .github/workflows/pages-deploy.yml
      - name: Build site
        # Before: bundle exec jekyll b -d "_site${{ steps.pages.outputs.base_path }}"
        # After: load _config_prod.yml in CI to inject the CDN prefix
        run: bundle exec jekyll b -d "_site${{ steps.pages.outputs.base_path }}" --config _config.yml,_config_prod.yml
        env:
          JEKYLL_ENV: "production"

GitHub Actions runner log showing Jekyll multi-config build Figure 2. GitHub Actions loading both _config.yml and _config_prod.yml during the production build.

Because CI runs htmlproofer with --disable-external, rewritten https://cdn.jsdelivr.net/... image URLs won’t trigger false failures on newly added images before the commit is pushed to main.


6. Verifying cache headers and edge behavior

After deploying, inspect the response headers from jsDelivr with curl:

1
curl -ILs "https://cdn.jsdelivr.net/gh/cmsong111/blog@main/assets/images/2025-03-18/dns-records-setup.png" | head -n 12

Terminal output showing jsDelivr HTTP/2 200 response and Cache-Control headers Figure 3. Verifying HTTP/2 200 status and cache headers returned by jsDelivr.

1
2
3
4
5
6
7
8
HTTP/2 200
date: Sun, 04 Oct 2026 14:30:04 GMT
content-type: image/png
content-length: 34652
access-control-allow-origin: *
cache-control: public, max-age=604800, s-maxage=43200
x-jsd-version: main
x-jsd-version-type: branch

Two headers here do the heavy lifting:

  • cache-control: public, max-age=604800, s-maxage=43200:
    • Browsers cache the image locally for 7 days (max-age=604800), eliminating repeat downloads on return visits.
    • jsDelivr edge POPs cache the file for 12 hours (s-maxage=43200), shielding GitHub from origin requests.
  • x-jsd-version: main: Confirms the asset is resolved from the main branch tip.

7. Purging stale images immediately (Purge API)

Because @main files are cached at the edge for 12 hours, overwriting an existing image under the same filename won’t update immediately for visitors unless you either rename the file or purge the edge cache.

To invalidate a single file across jsDelivr’s edge nodes right after pushing a replacement, call the official Purge API:

1
2
# Immediately invalidate a single cached image on jsDelivr
curl -X POST "https://purge.jsdelivr.net/gh/cmsong111/blog@main/assets/images/2025-04-08/sample.png"

jsDelivr clears the cached object and fetches the updated file from GitHub on the next request.


Summary and caveats

Splitting _config.yml and _config_prod.yml solved the bandwidth problem without touching my writing workflow:

  1. Zero image bandwidth on GitHub Pages: All post images serve from jsDelivr’s edge network instead of counting against GitHub’s 100 GB/month limit.
  2. Faster global delivery: Visitors pull images from the nearest Anycast edge POP with 7-day browser caching.
  3. Unchanged local workflow: Markdown files still reference /assets/images/..., so VS Code previews and jekyll serve work offline.

Production checklist

  • Repository is public (required for cdn.jsdelivr.net/gh/...)
  • cdn: left blank in _config.yml and set only in _config_prod.yml
  • .github/workflows/pages-deploy.yml passes --config _config.yml,_config_prod.yml
  • All Markdown images use clean root-relative /assets/images/... paths (no duplicate slashes)
  • Image files kept under 1 MB (WebP or optimized PNG) to keep the Git repository lean—see Localizing external blog images and adding static QA for the full image pipeline
This post is licensed under CC BY 4.0 by the author.