Modifying HTTP headers in an Inertia.js Laravel application

When you return an Inertia response using Inertia::render(), it doesn’t return a standard Laravel response object directly. Instead, it returns an instance of Inertia\Response, which is then converted to a standard Laravel response when it’s actually sent back to the client.

Modifying Headers in an Inertia Response

To modify headers in an Inertia response, you need to:

  1. Create an Inertia Response: First, create your Inertia response using Inertia::render().
  2. Convert to Laravel Response: Convert the Inertia response to a standard Laravel response using the toResponse() method. This method takes the current request as an argument and converts the Inertia response to a Laravel response.
  3. Modify Headers: Once you have the Laravel response, you can modify its headers using the headers->set() method.
  4. Return the Response: Finally, return the modified response.

Example Controller

Here’s an example of a Laravel controller method that modifies headers in an Inertia response:

use Inertia\Inertia;

class ExampleController extends Controller
    public function show()
        // Step 1: Create an Inertia response
        $inertiaResponse = Inertia::render('ExampleComponent', [
            'data' => 'some data'

        // Step 2: Convert the Inertia response to a Laravel response
        $response = $inertiaResponse->toResponse(request());

        // Step 3: Modify the headers
        $response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
        $response->headers->set('Pragma', 'no-cache');
        $response->headers->set('Expires', '0');

        // Step 4: Return the response
        return $response;

Leave a Reply

Your email address will not be published. Required fields are marked *