Chapter 3

Caching

Control when social card images refresh

OpenGraph+ reads HTTP cache headers from your Laravel responses to decide when to re-render social card images. Laravel gives you several ways to control these headers.

This page covers the Laravel side. For how OpenGraph+ handles caching at the HTTP level, see the HTTP Caching guide.

Response headers

Set cache headers directly in your controller methods:

public function show(Post $post)
{
    return response()
        ->view('posts.show', ['post' => $post])
        ->header('Cache-Control', 'public, max-age=86400');
}

This tells OpenGraph+ the content is valid for 24 hours before re-rendering.

Middleware

Create a reusable middleware for cache headers across routes:

// app/Http/Middleware/CacheControl.php
public function handle($request, Closure $next, $maxAge = 3600)
{
    $response = $next($request);
    $response->headers->set('Cache-Control', "public, max-age={$maxAge}");
    return $response;
}

Apply it to routes:

Route::get('/posts/{post}', [PostController::class, 'show'])
    ->middleware('cache.control:86400');

ETags

Laravel doesn’t have built-in ETag helpers like some frameworks, but you can set them manually:

public function show(Post $post)
{
    return response()
        ->view('posts.show', ['post' => $post])
        ->header('Cache-Control', 'public, max-age=3600')
        ->header('ETag', '"' . md5($post->updated_at) . '"');
}

If your content hasn’t changed, OpenGraph+ serves the cached image without re-rendering.

Recommendations

Page type Strategy
Static pages Cache-Control: public, max-age=604800 (1 week)
Blog posts Cache-Control: public, max-age=86400 (1 day)
Index pages Cache-Control: public, max-age=3600 (1 hour)
User dashboards No caching or Cache-Control: no-store

Meta tag overrides

If you can’t control HTTP headers, use meta tags instead. See the HTTP Caching guide for the full list of meta tag overrides.

Purging cached images

When you need to force a refresh immediately, go to your website dashboard, find the page, and click purge. This clears the cached image and triggers a re-render on the next request.