Laravel: Middleware SetCacheHeaders

Tram Ho

There are many useful middleware registered within Laravel, such as authentication, authorization, throttler and even route model binding. There are also a few mentioned middleware is SetCacheHeaders that alias is cache.headers . This middleware can be used to add cache headers to the response.

What are cache headers?

If you do not know cache headers, then you do not know what HTTP Cache Control is.

Put simply, Cache Control is a way to inform the browser of the response that the application sent should be cached or not and under what conditions. Thereby determining whether to request a full response again.

This is a browser-centered mechanism. You will understand the reason after reading this article.

Once you have a rough understanding of HTTP Cache Control and what the SetCacheHeaders middleware does, learn more in the next section.

How does SetCacheHeaders middleware work?

Let’s take a look at which source code :

As you can see, there are six main statements in the handle() of this middleware. Analyze them a little bit:

  1. Check if the request is a GET or HEAD. You cannot cache a POST response, as this method means that something has been changed.
  2. Check if you passed any parameters to the middleware and parse them for the Response headers using the parseOptions() .
  3. If there is an etag option, it will hash the response content so that it can be compared with the etag the request sent.
  4. If there is the last_modified option, it will add last_modified to the header.
  5. Assign Cache-Control options to Response headers.
  6. Finally, it checks to see if the response has changed. Otherwise, it will ignore the response and return only the etag .

Steps 3 and 6 are the most important. The browser will send the request to the application with the response’s etag cached, if the original response contains the etag . Your application will receive the etag , process the request , and eventually hash the content into the new etag . By comparing the two etag mentioned above, your application can know whether to resend the response or just resend the etag , to notify the browser that the content of the response has not changed.

Handling requests is also an important part. Because the application or the browser will not be able to know if the response content has changed until the response is ready, meaning all logic in the application must still be run from start to finish.

If you want to implement caching for your application, you can think about caching the response at the server and also using the etag header to optimize both server and client.

Use the SetCacheHeaders middleware

We already know what this middleware does. Let’s do it, a simple example of it.

Below is a Google diagram of the cache-control policy:

Here is an example from the Laravel homepage:

  • etag allows users to refresh the page without reloading the content if the content has not changed.
  • public allows the response to be cached in any way, even when not normally cached ( see more ).
  • max-age = 2592000 will set the cache expiration to 30 days from the last time the cache was created.

See more about the Cache-Control header .

That’s all I want to convey in this article. You do not need to manually create middleware or config in Apache or NGINX. Thank you for reading.

Share the news now

Source : Viblo