1

We just upgraded from Drupal 8 to Drupal 9, and now we have some css files not working in Chrome due to the below error.

Refused to apply style from '' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

These css files are in the private files directory for the site. I've verified that the mime_module is loaded as I can see this in the apache logs

module mime_module is already loaded, skipping

I also have .htaccess files enabled using AllowOverride All in the httpd.conf

I've tried putting the following in the .htaccess file in the parent directory of the css files, in our /etc/httpd/conf.d/drupal.conf, and in /etc/httpd/conf/httpd.conf but nothing fixes the error

AddType text/css .css
AddHandler text/css .css

and

AddType 'text/css; charset=UTF-8' css

and

<FilesMatch "(\.css)$">
    AddType text/css .css
    Header set Content-Type text/css
</FilesMatch>

Why won't apache set the correct content-type?

6
  • Have you visited one of the CSS paths directly? In my experience that browser error usually occurs when Drupal returns an HTML 403/404/500 for the path.
    – Clive
    Oct 29 '21 at 18:58
  • Yes when I visit directly it loads the css in my browser, but as text/plain. We actually have our prod environment still running Drupal 8 and the css is loading fine there. This started happening in Drupal 9
    – gary69
    Oct 29 '21 at 18:59
  • Sorry ignore me, I misread plain as html
    – Clive
    Oct 29 '21 at 19:01
  • 1
    If the CSS files are really in the private files directory this is not an Apache issue. Drupal controls access and serves the files via PHP.
    – 4k4
    Oct 29 '21 at 19:26
  • 1
    Don't know. Normally the files in the private directory are managed and the MIME type is in the database. If not you can use hook_file_download to manage it on your own. You shouldn't need the linked answer for a standard MIME type already defined in Drupal.
    – 4k4
    Oct 29 '21 at 19:50
2

If the CSS files are in the private files directory this is not an Apache issue. Drupal controls access and serves the files via PHP. Normally files stored in the private directory are managed and the MIME type is specified in the database table file_managed. If not, you can use hook_file_download to manage them on your own:

use Drupal\Core\StreamWrapper\StreamWrapperManager;

/**
 * Implements hook_file_download().
 */
function mymodule_file_download($uri) {
  $scheme = StreamWrapperManager::getScheme($uri);
  $target = StreamWrapperManager::getTarget($uri);
  if ($scheme == 'private' && substr($target, -4) == '.css') {
    return [
      'Content-Type' => 'text/css',
    ];
  }
}

This allows anybody to access CSS files in the private directory and also sets the MIME type.

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.