Set Charset to UTF-8 on html pages for Middleman blog site - ruby

I have a Middleman blog hosted on Heroku (http://tomgillard.herokuapp.com) and have been trying to optimise it based on google's PageSpeed recommendations. One recommendation is that I provide a character set on the site's HTML pages.
HTML pages contain the html5 <meta charset="utf-8"> in the <head> but this doesn't seem to be enough os I thought I could set it server side.
Here is my config.ru
require 'rack/contrib'
# Modified version of TryStatic, from rack-contrib
# https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/try_static.rb
# Serve static files under a `build` directory:
# - `/` will try to serve your `build/index.html` file
# - `/foo` will try to serve `build/foo` or `build/foo.html` in that order
# - missing files will try to serve build/404.html or a tiny default 404 page
module Rack
class TryStatic
def initialize(app, options)
#app = app
#try = ['', *options.delete(:try)]
#static = ::Rack::Static.new(lambda { [404, {}, []] }, options)
end
def call(env)
orig_path = env['PATH_INFO']
found = nil
#try.each do |path|
resp = #static.call(env.merge!({'PATH_INFO' => orig_path + path}))
break if 404 != resp[0] && found = resp
end
found or #app.call(env.merge!('PATH_INFO' => orig_path))
end
end
end
# Serve GZip files to browsers that support them
use Rack::Deflater
# Custom HTTP Headers
use Rack::ResponseHeaders do |headers|
headers['Charset'] = 'UTF-8'
end
#Custom Cache Expiry
use Rack::StaticCache, :urls => ["/img", "/css", "/js", "/fonts"], :root => "build"
# Attempt to serve static HTML file
use Rack::TryStatic, :root => "build", :urls => %w[/], :try => ['.html', 'index.html', '/index.html']
# Serve 404 messages:
run lambda{ |env|
not_found_page = File.expand_path("../build/404.html", __FILE__)
if File.exist?(not_found_page)
[ 404, { 'Content-Type' => 'text/html', 'Charset' => 'UTF-8' }, [File.read(not_found_page)] ]
else
[ 404, { 'Content-Type' => 'text/html', 'Charset' => 'UTF-8' }, ['404 - page not found'] ]
end
}
I thought I could use Rack::ResponseHeaders from rack-contrib but I don't think I'm using it correctly;
# Custom HTTP Headers
use Rack::ResponseHeaders do |headers|
headers['Charset'] = 'UTF-8'
end
Like I said, I've searched high and low; consulted docs (Rack, heroku), SO questions, blog posts, github, you name it.
Any help with this is much appreciated.
Cheers,
Tom

I had a similar problem and wanted to optimize my site. You just need to manually set your Content-Type header.
Here's my complete config.ru which achieves a 98/100 on Google PageSpeed (it complains about not embedding my css in the page):
# encoding: utf-8
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
require 'rack/contrib'
require File.expand_path("../rack_try_static", __FILE__)
use Rack::ResponseHeaders do |headers|
headers['Content-Type'] = 'text/html; charset=utf-8' if headers['Content-Type'] == 'text/html'
end
use Rack::Deflater
use Rack::StaticCache, urls: ["/images", "/stylesheets", "/javascripts", "/fonts"], root: "build"
use ::Rack::TryStatic,
root: "build",
urls: ["/"],
try: [".html", "index.html", "/index.html"]
run lambda { [404, {"Content-Type" => "text/plain"}, ["File not found!"]] }
You'll also need to add rack-contrib to your Gemfile for Rack::StaticCache:
gem 'rack-contrib'
You'll also want my rack_try_static.
Edit: Note the current implementation of Rack::StaticCache removes Last-Modified and Etag headers and will break on assets ending with a - followed by a number. I have PRs open for both of these (83 and 84).

This no longer applies as I have moved my blog from heroku to github pages. Thanks for looking.

Related

Public Directory Not Being Served With Sinatra

As the title says, I cannot get Heroku to use my public assets.
Locally, when running my app with shotgun it works. But with rackup (what Heroku uses), the css and assets 404.
I've tried a bunch of answers on here (one, two, three) but none have worked.
Here's my directory structure:
My config.ru:
require 'bundler'
Bundler.require
require File.expand_path('../config/environment', __FILE__)
run BikeShareApp
And my controller:
class BikeShareApp < Sinatra::Base
get '/' do
erb :'home/index'
end
get '/stations' do
#stations = Station.all
erb :'stations/index'
end
end
EDIT: This is how I'm referencing my assets by the way
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/overwrite.css" rel="stylesheet">
Found it.
Following this guide and putting this in my config.ru worked:
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
}
]
}

Rack::Builder and Rack::TryStatic doesn't work

I have a Middleman app which I am serving using Rack::TryStatic.
Here is the config.ru.
use Rack::TryStatic,
root: 'build',
urls: %w[/],
try: ['.html', 'index.html', '/index.html']
run lambda{ |env|
four_oh_four_page = File.expand_path("../build/404.html", __FILE__)
[ 404, { 'Content-Type' => 'text/html'}, [ File.read(four_oh_four_page) ]]
}
My understanding is that when you use a config.ru with run, map or use
methods, they are converted to a Rack::Builder object.
I've tried wrapping this config in a Rack::Builder object like this:
app = Rack::Builder.new do
use Rack::TryStatic,
root: 'build',
urls: %w[/],
try: ['.html', 'index.html', '/index.html']
run lambda{ |env|
four_oh_four_page = File.expand_path("../build/404.html", __FILE__)
[ 404, { 'Content-Type' => 'text/html'}, [ File.read(four_oh_four_page) ]]
}
end
run app
When I do this, I get the 404 page for all requests.
Why doesn't this work?
It looks like there is a bug in the current released version of TryStatic that has been fixed in master, where the try array is being lost when used in way that causes the middleware to be reinitialized.
You can avoid this by making sure the app only get s initialized once by using to_app:
run app.to_app
or equivalently use app instead of new:
app = Rack::Builder.app do
# ...

modular Sinatra App, setting error handling & configuration globally

I am using Sinatra to build a small Ruby API, and I would like to get some of the errors and configurations set to work at a global level so that i don't need to set them at the start of each of the classes.
My structure is this:
content_api.rb
require 'sinatra/base'
require 'sinatra/namespace'
require 'sinatra/json'
require 'service_dependencies'
require 'api_helpers'
require 'json'
module ApiApp
class ContentApi < Sinatra::Base
helpers Sinatra::JSON
helpers ApiApp::ApiHelpers
include ApiApp::ServiceDependencies
before do
content_type :json
end
get '/' do
content = content_service.get_all_content
content.to_json
end
get '/audio' do
package =content_service.get_type 'Audio'
package.to_json
end
get '/video' do
package =content_service.get_type 'Video'
package.to_json
end
get '/document' do
package =content_service.get_type 'Document'
package.to_json
end
end
end
config.ru:
$LOAD_PATH.unshift *Dir[File.join(File.dirname(__FILE__), '/src/**')]
$LOAD_PATH.unshift *Dir[File.join(File.dirname(__FILE__), '/src/api/**')]
require 'content_api'
require 'package_api'
require 'utility_api'
require 'sinatra/base'
configure do
set :show_exceptions => false
end
error { |err| Rack::Response.new([{'error' => err.message}.to_json], 500, {'Content-type' => 'application/json'}).finish }
Rack::Mount::RouteSet.new do |set|
set.add_route ApiApp::ContentApi, {:path_info => %r{^/catalogue*}}, {}, :catalogue
set.add_route ApiApp::PackageApi, {:path_info => %r{^/package*}}, {}, :package
set.add_route ApiApp::UtilityApi, {:path_info => %r{^/health_check*}}, {}, :health_check
end
When I run this, it will run okay, but when I force a 500 error (shut down MongoDb) I get a standard html type error that states:
<p id="explanation">You're seeing this error because you have
enabled the <code>show_exceptions</code> setting.</p>
If I add the
configure do
set :show_exceptions => false
end
error { |err| Rack::Response.new([{'error' => err.message}.to_json], 500, {'Content-type' => 'application/json'}).finish }
inside the content_api.rb file, then I get a JSON error returned as I would like to receive.
however, as I am building this modular, I don't want to be repeating myself at the top of each class.
Is there a simple way to make this work?
The simplest way to do this would be to reopen Sinatra::Base and add the code there:
class Sinatra::Base
set :show_exceptions => false
error { |err|
Rack::Response.new(
[{'error' => err.message}.to_json],
500,
{'Content-type' => 'application/json'}
).finish
}
end
This is probably not advisable though, and will cause problems if your app includes other non-json modules.
A better solution might be to create a Sinatra extension that you could use in your modules.
module JsonExceptions
def self.registered(app)
app.set :show_exceptions => false
app.error { |err|
Rack::Response.new(
[{'error' => err.message}.to_json],
500,
{'Content-type' => 'application/json'}
).finish
}
end
end
You would then use it by registering it in your modules:
# require the file where it is defined first
class ContentApi < Sinatra::Base
register JsonExceptions
# ... as before
end
As an alternative, inheritance:
class JsonErrorController < Sinatra::Base
configure do
set :show_exceptions => false
end
error { |err| Rack::Response.new([{'error' => err.message}.to_json], 500, {'Content-type' => 'application/json'}).finish }
end
class ContentApi < JsonErrorController
# rest of code follows…
end
From Sinatra Up and Running p73:
Not only settings, but every aspect of a Sinatra class will be
inherited by its subclasses. This includes defined routes, all the
error handlers, extensions, middleware, and so on.

Ruby Rack Heroku : Unable to access multiple URLs

I have followed the Heroku guide on deploying static files using Ruby Rack (https://devcenter.heroku.com/articles/static-sites-ruby),
However I am unable to access any HTML file in \public apart from index.html (every URL resolves to the home page)
The config.ru file :
use Rack::Static,
:urls => ["/bootstrap", "/css", "/fonts", "/images", "/js", "/font-awesome"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
I have checked the following posts :
Ruby Rack Heroku: Serving Static Files
How to setup URLs for static site with Ruby Rack on Heroku
Still I am unable to go beyond the index.html page.
While I change the config.ru as shown below ... I get application error
use Rack::Static,
:urls => ["/bootstrap", "/css", "/fonts", "/images", "/js", "/font-awesome"],
:root => "public"
map "/" do
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
end
map "/about" do
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/about/index.html', File::RDONLY)
]
}
end
Is there a way to map multiple URLs? I have 5-6 pages in my index.html, but unable to access.
Thanks for your help.
You could create a simple controller that just renders these pages. May be easier than trying to bypass the MVC. Let me know if you need help with this :)
Update -----
Example
Controller:
class PublicController < ApplicationController
def index
render "public/index"
end
def about
render "public/about/index"
end
end
Then place your html files in the views folder in the sub-directories specified in the controller.
Also don't forget to add these methods to your routes.rb file in the config directory.

Sinatra json rendering not working as expected

I'm having a problem in Sinatra where I can't respond with just a json and I can't find good sinatra docs anywhere, most of things seems outdated.
Anyways, here's the code:
module MemcachedManager
class App < Sinatra::Base
register Sinatra::Contrib
helpers Sinatra::JSON
get '/' do
json({ hello: 'world' })
end
end
end
MemcachedManager::App.run! if __FILE__ == $0
The response that I do get is:
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>{\"hello\":\"world\"}</p></body></html>\n"
Where it should have been only the json part. Why is it rendering html tags when I didn't ask for it?
Have you seen this blog post?
require 'json'
get '/example.json' do
content_type :json
{ :key1 => 'value1', :key2 => 'value2' }.to_json
end
I would also modify this to:
get '/example.json', :provides => :json do
to stop HTML/XML calls using the route. Since you're using the sinatra-contrib gem, and since Ruby doesn't need all those parens etc, you can also simplify the code you've given as an example to:
require 'sinatra/json'
module MemcachedManager
class App < Sinatra::Base
helpers Sinatra::JSON
get '/', :provides => :json do
json hello: 'world'
end
end
end
MemcachedManager::App.run! if __FILE__ == $0
Try putting
content_type :json
before the json(...) call

Resources