Skip to content

Instantly share code, notes, and snippets.

@dragontheory
Created March 20, 2025 21:18
Show Gist options
  • Save dragontheory/54285c2b0960b2c3d43099141a273a43 to your computer and use it in GitHub Desktop.
Save dragontheory/54285c2b0960b2c3d43099141a273a43 to your computer and use it in GitHub Desktop.

πŸš€ Controlling Cache on GitHub Pages for Vanilla HTML & CSS

πŸ” Understanding GitHub Pages Caching

GitHub Pages aggressively caches static files (HTML, CSS, JS), which can sometimes prevent updates from appearing immediately. Since GitHub does not support .htaccess for direct cache control, you must use alternative methods.

πŸ“ 1. Do You Need a .nojekyll File?

  • ❌ No, if you did not enable Jekyll when setting up GitHub Pages.
  • βœ… Yes, if your repository contains _config.yml, _layouts/, _includes/, or _posts/, as GitHub Pages will assume it's a Jekyll site.
  • πŸ“‚ Creating a .nojekyll file in your repo prevents GitHub from processing files as a Jekyll site but is unnecessary for pure HTML/CSS sites.

⏳ 2. GitHub Pages Cache Expiration

  • ⚑ HTML updates instantly.
  • πŸ•’ CSS and JavaScript can be cached longer (up to 10 minutes or more).
  • πŸ”„ If changes don’t appear, force refresh (Ctrl + F5).

πŸ”§ 3. Cache Busting Techniques (Recommended)

Since you cannot modify HTTP headers on GitHub Pages, the best way to force fresh loads is cache busting.

πŸ† Option 1: Query String Cache Busting (Best for frequent updates)

Modify your file URLs with a query string:

<link rel="stylesheet" href="styles.css?v=12345">
<script src="script.js?v=67890"></script>

πŸ’‘ Effect: The browser treats styles.css?v=12345 as a new file and reloads it.

πŸ— Option 2: Rename Files (Best for major updates)

Instead of modifying URLs with query strings, rename the files when making updates:

  • πŸ“„ styles.css β†’ styles-v2.css
  • πŸ“„ script.js β†’ script-v2.js
  • πŸ›  Update your HTML references accordingly.

πŸ” 4. Force Refresh in Browsers

If updates do not appear, manually force refresh:

  • πŸ–₯ Windows/Linux: Ctrl + F5 (Chrome/Edge/Firefox)
  • 🍏 Mac: Cmd + Shift + R
  • πŸ›  DevTools: Open Developer Tools (F12), go to Network Tab, check "Disable Cache", then refresh.

🌐 5. GitHub Cache Considerations

  • πŸ“Œ GitHub caches files globallyβ€”updates may take up to 10 minutes to propagate.
  • ⏳ If your changes are not appearing, wait a few minutes before testing again.
  • πŸ€– Use GitHub Actions (if applicable) to trigger automatic updates by changing version numbers.

βœ… Final Recommendations

βœ” You do NOT need .nojekyll unless Jekyll-related files exist. βœ” Use query strings (?v=12345) or rename files to force fresh loads. βœ” GitHub caches static files for ~10 minutesβ€”be patient if changes don’t appear immediately. βœ” Force refresh (Ctrl + F5) if updates are not visible.


πŸ“š References

  1. GitHub Pages Documentation - Official guide on configuring GitHub Pages.
  2. MDN Web Docs - Cache Busting - Explains cache busting techniques.
  3. MDN Web Docs - HTTP Caching - Detailed caching behavior in browsers.

⚑ Would you like an automated script to update version numbers dynamically? πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment