ABSimon
ABSimon

Reputation: 671

Cache always returning 200

This is my controller function

/**
* @Route(
*     path     = "/",
*     defaults = {"page": "1"},
*     name     = "products_show"
* )
* @Route(
*     path         = "/page/{page}",
*     requirements = {"page": "[1-9]\d*"},
*     name         = "products_show_by_page"
* )
* @Method("GET")
* @Cache(smaxage = "1800", public = true)
*/
public function showProducts(Request $request, int $page, PaginatorInterface $paginator, ProductRepository $products)
{
    $searchQuery = $request->query->get('q', '');
    $query = $products->findOrSearchAllJoinedToCategory($searchQuery);

    $pagination = $paginator->paginate($query, $page, Product::NUM_ITEMS);
    $pagination->setUsedRoute('products_show_by_page');

    return $this->render('product/products_show.html.twig', [
        'pagination'  => $pagination,
        'page'        => $page,
        'searchQuery' => $searchQuery
    ]);
}

and I have added an empty KernelCache.php and make changes in index.php. See https://symfony.com/doc/current/http_cache.html#symfony-reverse-proxy and set APP_ENV to prod in .env

But after thes changes, I still get a 200 Response, not a 304 Response. Are there any additional todos? (I have a simple Xamp Development System and test with Firefox)

Upvotes: 2

Views: 348

Answers (1)

dbrumann
dbrumann

Reputation: 17166

You are mixing up expiration and validation Caching. When you specify a shared max age any intermediary cache can return a cached item instead of sending the request to your server, but when the cache expires they will send a regular request that your server will answer to with a cache header specifiying for how long intermediary caches may store the response. The 304 header is only used with validation caching when the provided ETag or last modified timestamp match the one from your application. You are responsible for implementing this logic. I’m on mobile and don’t want to search for the doc page, but you have to compare the request headers with the current data, create a response manually and then mark it as not modified. There should be examples in the docs, but feel free to ping me if you can’t find them.

Upvotes: 1

Related Questions