I have an error in magento 2.2 frontend and backend. Scripts and stylesheets fail to load, the console states that the MIME type is incorrect and require.js can't be loaded. Problem still occurs after deploying new static content, removing cache folders, cleaning and flushing the cache and re-indexing.
First of all I am able to recreate this issue when
<magento>/pub/static/.htaccess is deleted or missing. It is very easy to accidentally delete this file, so check this first! Download a fresh copy from github if necessary. That link is for version 2.2.4, it may differ for other versions.
Note running the command
rm -rf pub/static/* will delete all files in pub static EXCEPT
.htaccess. I made the mistake early on of running the command with just
pub/static/, which deletes the entire folder.
And now my original answer which still applies:
Sort of a hunch here, but I get similar issues when I forget to add this code to my
<VirtualHost> block in my apache configuration.
<Directory "/Users/jamil/www/mymagento2site.com"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
AllowOverride All might be the important piece here, I find this isn't the default on most of my set ups. Without it,
.htaccess files get ignored.
Whole VHost setup would look something like this:
<VirtualHost *:80> DocumentRoot "/Users/jamil/www/mymagento2site.com" ServerName mymagento2site.com ServerAlias www.mymagento2site.com <Directory "/Users/jamil/www/mymagento2site.com"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost>
Another question has an answer saying to make sure you have the correct
.htaccess file in your
pub/static/ folder. This can sometimes get deleted if you do
rm -rf pub/static in the command line, instead of the correct command:
rm -rf pub/static/*.
First of all if you're using a custom theme I would eliminate that variable and change to luma.
Regardless, the issue sounds like it's server side. I'm not an expert in apache but I've referenced my local setup and did a bit of research and found you can add mime types in a few different ways. Let me add that I'm a bit worried that this represents a larger issue with your setup.
In my apache configuration I have this declaration:
LoadModule mime_module modules/mod_mime.so # ... <IfModule mime_module> # # TypesConfig points to the file containing the list of mappings from # filename extension to MIME-type. # TypesConfig /Applications/MAMP/conf/apache/mime.types </IfModule>
Without that, I get an error:
Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost/test/style.css".
TypesConfig links to the file mime.types, a copy of which can be found at the link.
You can also define mime types one at a time in your
.htaccess like so:
AddType text/css css
In order for that to work, my note above is still true, you need the
AllowOverride directive to be defined as
All. I think this is probably already enabled. If not you would be seeing other issues, like a 404 when trying to accessing admin.
I assumed you were using VirutalHosts, but if you're not then you can add
AllowOverride All in your apache configuration like so:
<Directory "/"> AllowOverride All </Directory>
Note, most or all of the changes above require you to restart apache.
Hope this helps!