---
title: "Discussion on \"Caching your Laravel API with ETag and Conditional Requests\" | Hashnode"
description: "Discussion on \"Caching your Laravel API with ETag and Conditional Requests\". When writing an application with a decoupled front- and backend, you'll have to start considering the requests your frontend client makes to the API. Fetching data again from the backend, even when you only want to verify that your frontend's cache i..."
url: "https://hashnode.com/posts/caching-your-laravel-api-with-etag-and-conditional-requests/60306105f51caf4448561656"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Caching your Laravel API with ETag and Conditional Requests](https://hergen.nl/caching-your-laravel-api-with-etag-and-conditional-requests?utm_source=hashnode&utm_medium=feed)

When writing an application with a decoupled front- and backend, you'll have to start considering the requests your frontend client makes to the API. Fetching data again from the backend, even when you only want to verify that your frontend's cache i...

Feb 20, 2021 · 11 min read · hergen.nl  
Read the article

-   [#laravel](https://hashnode.com/tag/laravel)
-   [#cache](https://hashnode.com/tag/cache)
-   [#caching](https://hashnode.com/tag/caching)
-   [#rest-api](https://hashnode.com/tag/rest-api)
-   [#apis](https://hashnode.com/tag/apis)

## Written by

[Hergen Dillema](https://hashnode.com/@Hergen)

@Hergen

  
[View profile](https://hashnode.com/@Hergen)

Backend developer

Backend developer from the Netherlands, working with Laravel, PHP, Go and building micro services

## Responses 3

Markdown supported · ⌘↵ to post

[Christian Widlund](https://hashnode.com/@chrillep)

[Apr 10, 2021](https://hashnode.com/posts/caching-your-laravel-api-with-etag-and-conditional-requests/60306105f51caf4448561656/comment/607160c41494367be6d29909)

[Hergen Dillema](https://hashnode.com/@Hergen) what's the major differences with using werk365/etagconditionals vs. laravel built in cache-control-middleware? I didn't get that from the article 😊

[laravel.com/docs/8.x/responses](https://laravel.com/docs/8.x/responses#cache-control-middleware)

[Hergen Dillema](https://hashnode.com/@Hergen)

[Apr 10, 2021](https://hashnode.com/posts/caching-your-laravel-api-with-etag-and-conditional-requests/60306105f51caf4448561656/comment/607160c41494367be6d29909#reply-6071ce4a8b2b857c0bdb5fe0)

Hi [Christian Widlund](https://hashnode.com/@chrillep), if you wish to just use If-None-Match in your application in the easiest way possible, I would recommend using the cache control middleware bundled with Laravel. One small difference using our package is the support for HEAD requests, the Laravel middleware uses this condition:

```
        if (! $request->isMethodCacheable() || ! $response->getContent()) {
            return $response;
        }
```

In which the first condition will check if the request is either a GET or HEAD request, but the second condition will return `false` for HEAD requests as they don't return content.

The biggest difference however is simple the If-Match support. This is not included in the Laravel middleware at all. I suspect because it is almost impossible to make a "on size fits all" solution for If-Match, but our implementation should work for most common use cases.

This package will also keep getting updated and if possible, more conditional requests might get added.

[Christian Widlund](https://hashnode.com/@chrillep)

[Apr 11, 2021](https://hashnode.com/posts/caching-your-laravel-api-with-etag-and-conditional-requests/60306105f51caf4448561656/comment/607160c41494367be6d29909#reply-6073007f8b2b857c0bdb6dea)

[Hergen Dillema](https://hashnode.com/@Hergen)

thank you for the in depth explanation! your package looks more useful. I'll try it out 😊.

[ssss94ssss](https://hashnode.com/@94)

[Mar 23, 2021](https://hashnode.com/posts/caching-your-laravel-api-with-etag-and-conditional-requests/60306105f51caf4448561656/comment/6059d8fc5771a2129446bbdf)

API request not noly for brower，for android apple and other mobile devices ，it is unuseful.

[Hergen Dillema](https://hashnode.com/@Hergen)

[Mar 24, 2021](https://hashnode.com/posts/caching-your-laravel-api-with-etag-and-conditional-requests/60306105f51caf4448561656/comment/6059d8fc5771a2129446bbdf#reply-605b4bc978c4742436d4f87d)

As this blogpost purely describes the backend implementation it is therefor frontend agnostic. In theory you could use this in your backend for any front end you want.

I would say caching mechanisms like this are especially valuable for mobile applications, where (especially in some countries) every bit of data might cost your user money if they use mobile data.

[Mahdi Pishguy](https://hashnode.com/@tux-world)

[Mar 4, 2021](https://hashnode.com/posts/caching-your-laravel-api-with-etag-and-conditional-requests/60306105f51caf4448561656/comment/60410a826a43af22af9d1f32)

thanks so much
