Magento 2 – Refused to Execute Script After Moving from Dev to Production

magento2production-mode

When moved to production, with enabled js file bundling, i start to get these types of errors in browser console:

Refused to execute script from 'http://ultra.com/static/frontend/RocketTeam/ultra/ru_RU/requirejs/require.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

Well, i check my files and it seems like i have them all in pub/static folder. So, for this http://ultra.com/static/frontend/RocketTeam/ultra/ru_RU/requirejs/require.min.js i have a file in my filesystem here:

magento/pub/static/frontend/RocketTeam/ultra/ru_RU/requirejs/require.js

When i try to reach these files via links from the browser console, i get 404.

As i understand my server returns me the wrong MIME type for js files. But why this happens? In developer mode everything works fine.

I just executed php bin/magento deploy:mode:set production and had output in the console similar to what is shown in documentation.

Magento 2.2 running locally in a docker container,
php 7.0.18

UPD: Ok, i was stupid, i don't have minified version of files in my filesystem. Ok, but why minification didn't happen?

UPD: Thanks to @PramodKharade, i managed to make it work.
What i did was:

after switching to production mode i cleaned and flushed caches, deleted generated folders, ran the setup, compiled code, and deployed static content:

  1. rm -rf pub/static/*
  2. rm -rf var
  3. php bin/magento c:c
  4. php bin/magento c:f
  5. php bin/magento set:up
  6. php bin/magento setup:di:compile
  7. php bin/magento setup:static-content:deploy –language ru_RU

Best Answer

1- At line no 10833

tinymce.documentBaseURL = window.location.href.replace(/[\?#].*$/, '').replace(/[\/\\][^\/]+$/, '');
if (!/[\/\\]$/.test(tinymce.documentBaseURL))
tinymce.documentBaseURL += '/';

Replace below Code

tinymce.documentBaseURL = ADMIN_BASE_URL+"/tiny_mce";

tinymce.baseURL = ADMIN_BASE_URL+"/tiny_mce";

if(IS_MINIFY == 1) tinymce.suffix = '.min'; else tinymce.suffix = '';

2- At line No 11391

sl.add(tinymce.baseURL + '/langs/' + s.language + '.js');

Replace with

sl.add(tinymce.baseURL + '/langs/' + s.language + tinymce.suffix + '.js');

3- Near at line No 10758

tinymce.ScriptLoader.add(this.urls[n] + '/langs/' + s.language + '.js');

Replace with

tinymce.ScriptLoader.add(this.urls[n] + '/langs/' + s.language + tinymce.suffix + '.js');

Override file in admin theme:

vendor/magento/module-backend/view/adminhtml/templates/page/js/require_js.phtml

4- Find below code

<script>

var BASE_URL = '<?php /* @escapeNotVerified */ echo $block->getUrl('*') ?>';

var FORM_KEY = '<?php /* @escapeNotVerified */ echo $block->getFormKey() ?>';

var require = {
"baseUrl": "<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('/') ?>"
};
</script>

Replace code with:

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$scopeConfig = $objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface');

$isMinify = $scopeConfig->getValue('dev/js/minify_files', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);    

?>

<script>

var BASE_URL = '<?php /* @escapeNotVerified */ echo $block->getUrl('*') ?>';

var FORM_KEY = '<?php /* @escapeNotVerified */ echo $block->getFormKey() ?>';

var ADMIN_BASE_URL = '<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('/') ?>';

var IS_MINIFY = '<?php /* @escapeNotVerified */ echo $isMinify ?>';

var require = {
"baseUrl": "<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('/') ?>"
};
</script>

5- Run below Commands

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy
Related Topic