Technical article
Cloudflare caching for Laravel: safely faster without stale content
Cache stateless HTML at the edge with origin headers, cache tags, safe query rules and targeted invalidation after deployment.
- Performance
- Cloudflare
- caching
- Laravel
- safely
- faster

In this article
Start Cloudflare caching with static assets and explicitly public responses. Personal HTML, session forms and authentication responses do not belong in shared cache. A cache-everything rule is not a safe default.
Cacheability belongs to the response
Cloudflare does not cache HTML or JSON by default. A header does not automatically make an HTML route an edge hit; Cache Rules also determine eligibility. Rules can influence default handling of Set-Cookie and Cache-Control. Read application headers and zone configuration together.
- Content-hashed asset: long retention, new URL when content changes.
- Public stateless page: explicit eligibility, TTL and targeted invalidation.
- Form with a CSRF token, login, portal or personal API: bypass and suitable no-store policy.
Illustration: two lifetimes
The fragment below is only for reviewed public.* routes without personalisation. The browser retains for at most 60 seconds; Cloudflare receives a separate illustrative one-hour policy. The longer stale window assumes old public content is acceptable. Do not use it for stock, payments or permissions. Inspect the final response after session middleware.
if (
$request->isMethod('GET')
&& $request->routeIs('public.*')
&& $request->user() === null
&& ! $request->headers->has('Cookie')
&& ! $request->headers->has('Authorization')
&& $response->getStatusCode() === 200
&& ! $response->headers->has('Set-Cookie')
&& ! $response->headers->hasCacheControlDirective('private')
&& ! $response->headers->hasCacheControlDirective('no-store')
&& ! $response->headers->hasCacheControlDirective('no-cache')
) {
$response->headers->set(
'Cache-Control',
'public, max-age=60, stale-while-revalidate=300'
);
$response->headers->set(
'Cloudflare-CDN-Cache-Control',
'public, max-age=3600, stale-while-revalidate=86400'
);
}Verify isolation between visitors
Test the same URL without cookies, with a session cookie and with Authorization. Also use two test accounts on a portal route. Compare content, Cache-Control, Set-Cookie, Age and CF-Cache-Status. A HIT does not prove cache safety. After an edit, verify targeted purge and the next response; switch the rule to bypass if isolation or freshness fails.
Example verification: PHP fragments were syntax-checked with PHP 8.5.11. Laravel execution and hosting behaviour were not tested; use a suitable test application and configuration.
Last updated:


