My config.ru is as follows:
use Rack::Static,
:urls => ["/images"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
When I load it locally the website looks fine, but when I run it on Heroku I get the following error message in the browser console for the CSS files:
Resource interpreted as Stylesheet but transferred with MIME type text/html.
Any idea why I am getting these errors?
Example site: http://salus8.heroku.com.
Your app currently responds to requests in two different ways. Requests starting with
/imagesare served by searching the/public/imagesfolder and returning any file matching the request. Any other request is served by running thelambdablock, which returns yourindex.htmlfile with a content type oftext/html.This applies to any other request, so when your page references a css file and the browser tries to fetch it, your app will return the
index.htmlpage with the HTML content type, hence the warning about MIME type.One way to fix this would be to add
/cssto the list of urls theStaticmiddleware handles:and put your css files in the
public/cssdirectory (as I write, it looks like you have already done this).This would solve your immediate problem, but you might have issues for example if you wanted to have more than one HTML page in the top directory.
Another solution to achieve a static site that serves
index.htmlto any requests with no path, which is what it looks like you’re trying to do here, could be to use therack-rewritegem and aRack::Fileapplication. Addgem 'rack-rewrite'to your Gemfile, and then use aconfig.rulike this:This will respond to all requests with the matching file (if it exists), and any requests that arrive with no path will get
index.html. (Note that it won’t serveindex.htmlfor requests for subdirectories under the main directory).If you’re using Heroku’s Cedar stack, you could also look into faking a php app in order to get “real” static hosting with Apache.
I don’t know why this would be working locally but not on Heroku, unless you’re just opening the files directly in the browser. Are you running a server locally (e.g. with
rackup), or looking at the files direct?