Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails returns .js with MIME type 'text/html'

I build a site. Under the development mode, it serves .js files nicely. However, under the production mode, the following error occurs:

Refused to execute script from 'http://bowuzhi.herokuapp.com/javascripts/items.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

And the content of http://bowuzhi.herokuapp.com/javascripts/items.js is html:

<div class='page-header'>
<a class="btn btn-primary" href="/zh-TW/items/new"><span class='glyphicon glyphicon-plus'></span>
New
</a><h1>Catalog</h1>
</div>
<div class='row' id='items'>
</div>
<div class='row'>
<div class='col-md-12'>

</div>
</div>
<script src="/javascripts/items.js"></script>

This site is on heroku: http://bowuzhi.herokuapp.com.

Why .js turns out to be html?

like image 454
Trantor Liu Avatar asked Apr 13 '14 07:04

Trantor Liu


1 Answers

I found the answer. The problem is that /javascripts/items.js was a controller-specified asset. It was included by using the javascript_include_tag helper on the view instead of included in manifest files (application.js), so it would not be precompiled.

To solve the problem, I added the line config.assets.precompile += %w(items.js) to my config/application.rb.

You can find more information in this question: rails-3-1-asset-pipeline-how-to-load-controller-specific-scripts.

like image 87
Trantor Liu Avatar answered Sep 21 '22 14:09

Trantor Liu