how to add cache tag in custom RSS feed in Drupal 8
I have tried like this

    use Symfony\Component\HttpFoundation\Response;
    $response = new Response();
    $xml_content = '<?xml version="1.0" encoding="UTF-8"?>
     <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
       <title><![CDATA[ Title there ]]></title>
       <link><![CDATA[ siteurl.com ]]></link>
       <description><![CDATA[ description there ]]></description>
    $cacheTags = ['node:nid', 'node:nid']
    $response->setCache(['etag' => $cacheTags]);
    $response->headers->set('Content-Type', 'xml');

but it not working

  • Welcome to Drupal Answers! We try to answer knowledge base questions here. So far you've only stated various facts and haven't asked an explicit question. We need an answerable question to be asked. We aren't a "debug your site" service where we're going to ask you many questions about what you need to do to fix your site.
    – Shawn Conn
    Sep 27 '18 at 20:18

Add the cache tags to the cacheable metadata of a cacheable response:

use Drupal\Core\Cache\CacheableResponse;

  $response = new CacheableResponse();
  // ...
  $response->getCacheableMetadata()->addCacheTags(['node:nid', 'node:nid']);
  • LogicException: The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\Core\Cache\CacheableResponse
    – sudishth
    Sep 25 '18 at 13:54
  • Different issue and not caused by the code of your question, see drupal.stackexchange.com/questions/225956/…
    – 4k4
    Sep 25 '18 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.