Score:1

('text/plain') is not a supported stylesheet MIME type

cn flag

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?

cn flag
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.
cn flag
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
cn flag
Sorry ignore me, I misread `plain` as `html`
4uk4 avatar
cn flag
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.
cn flag
Thank you, i think this https://drupal.stackexchange.com/a/56967/102670 might be the answer
4uk4 avatar
cn flag
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.
Score:2
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.