How to attach a library to a page controller that returns a plain Symfony response? I created a custom module with a page controller that returns a Symfony response (Symfony\Component\HttpFoundation\Response). The thing is I don't want to render my custom theme in this controller, just the theme_base template in this module.

$build = array(
  'page' =&gt; array(
    '#theme' =&gt; 'theme_base',
    '#content' =&gt; $content,
    '#owner' =&gt; $owner,
    '#cache' =&gt; [
      'max-age' =&gt; 0,
    ],
    '#attached' =&gt; array(
      'library' =&gt; array(
        'my_module/library_name',
      ),
    ),
  ),
);

$html = \Drupal::service('renderer')-&gt;renderRoot($build);
$response = new Response();
$response-&gt;setContent($html);

return $response;


I tried attaching the module library to $build, or including it directly in Twig template but this doesn't seem to work.

Any ideas why this is not working? Or is there maybe another way I could do this?

Thanks!
After rendering of $build the attachments have bubbled up to $build['#attached'] and you can add them to a response, which implements AttachmentsInterface, for example an HtmlResponse:

$html = \Drupal::service('renderer')-&gt;renderRoot($build);
$response = new HtmlResponse();
$response-&gt;setContent($html);
$response-&gt;setAttachments($build['#attached']);
return $response;


To place the libraries on the page you have to enclose the page in an html render element

$build = [
  '#type' =&gt; 'html',
  'page' =&gt; [
     // content with attached libraries  
  ],
];


or implement the placeholders where the attachment will end up in a custom template, see template_preprocess_html().