惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
G
Google Developers Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
P
Proofpoint News Feed
V
Visual Studio Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 叶小钗
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
H
Help Net Security
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
量子位
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Cache-busting JSON in PHP with filemtime
Odilon HUGON · 2026-05-08 · via DEV Community

Odilon HUGONNOT

The blog listing loads articles via fetch('posts.json'). Problem: the browser caches this file. When you publish a new article, visitors keep seeing the old list until they manually clear their cache — which they never do.

Why the browser caches the JSON

Without an explicit Cache-Control directive, Apache applies a heuristic: it estimates a cache duration from the file's Last-Modified header. In practice, browsers can keep the file cached for several hours, or even several days depending on their own rules.

You could solve this server-side with an HTTP header:

<FilesMatch "posts\.json$">
    Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>

Enter fullscreen mode Exit fullscreen mode

But this requires mod_headers to be enabled on Apache — which isn't guaranteed on shared hosting. And a systematic no-cache forces a server round-trip on every page load, even when the file hasn't changed.

The solution: filemtime as a query param

The principle of query string cache-busting is simple: as long as the URL doesn't change, the browser serves from its cache. As soon as the URL changes, it refetches.

Inject the file's last-modified timestamp via PHP:

fetch('posts.json?v=<?php echo filemtime(__DIR__ . "/posts.json"); ?>')

Enter fullscreen mode Exit fullscreen mode

filemtime() returns a Unix integer — the timestamp of the file's last modification on disk. The result in the generated HTML:

fetch('posts.json?v=1740268800')

Enter fullscreen mode Exit fullscreen mode

As soon as you modify posts.json (adding an article, fixing an excerpt), the timestamp changes, the URL changes, the browser treats it as a new resource and refetches. Between two publications, the URL is identical → the cache is used, no unnecessary request.

Why this is better than no-cache

With Cache-Control: no-cache, the browser must query the server on every visit to check whether the file has changed (conditional request with If-Modified-Since). It's fast, but it's still a network round-trip.

With query string cache-busting, if the file hasn't changed since the last visit, the URL is exactly the same — the browser serves from its local cache, with no network request at all. It's more aggressive in the right way: you benefit from the cache when possible, bypass it only when necessary.

The same technique for CSS and JS assets

This is exactly the principle used by all modern bundlers (Vite, Webpack) that generate filenames with hashes: main.a3f9c2.js. Here there's no build, but filemtime() plays the same role without any infrastructure:

<link rel="stylesheet" href="assets/css/styles.css?v=<?php echo filemtime(__DIR__ . '/assets/css/styles.css'); ?>">
<script src="assets/js/main.js?v=<?php echo filemtime(__DIR__ . '/assets/js/main.js'); ?>"></script>

Enter fullscreen mode Exit fullscreen mode

Browsers and CDNs ignore the query string for cache matching in some configurations, but for a standard Apache setup serving files directly, it works reliably across all modern browsers.

Limitation: intermediate proxies

Some proxies and CDNs (Cloudflare in aggressive mode, Varnish by default) ignore the query string and serve the same cached version regardless of the ?v= value. In that case, filename-based cache-busting (hash in the filename) is more robust. For a portfolio served directly by Apache with no CDN, this isn't an issue.