Menu

Laravel responses are uncacheable by default. Here's why and how to fix it
📰

Laravel responses are uncacheable by default. Here's why and how to fix it

DEV Community: laravel·nocache.dev·6 days ago
#xPI8pCqb
#devtolaravel#devto#dev#cache#cookie
Reading 0:00
15s threshold

Put a CDN in front of a fresh Laravel app and the hit rate for HTML will be zero. Nothing is broken. The framework is being careful on purpose, in two separate places, and both have to be dealt with before any shared cache will help.

I read the current framework source and ran live header checks on two well-known Laravel sites today. First, where the defaults come from.

The header comes from Symfony

Laravel's Response extends Symfony's, and Symfony's ResponseHeaderBag fills in Cache-Control whenever you haven't set one. From computeCacheControlValue() :

if ( ! $this -> cacheControl ) { if ( $this -> has ( 'Last-Modified' ) || $this -> has ( 'Expires' ) ) { return 'private, must-revalidate' ; }

// conservative by default return 'no-cache, private' ; }

Enter fullscreen mode

Exit fullscreen mode

So a plain return view('home') leaves with Cache-Control: no-cache, private . private keeps it out of every shared cache, and no-cache makes the browser revalidate before reuse.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.