Public Directory Not Being Served With Sinatra - heroku

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'
}
]
}

Related

my Rack::Static middleware works with root url in urls parameter and without

I have this in my config.ru:
require 'rack'
require './app.rb'
use(Rack::Static,
:urls => %w(/css /fonts /images /js),
:root => '../static',
:index => 'index.html')
end
run MyApp
it correctly serves static files for all four of the above URLs, but in addition, it also serves index.html from the root URL as if I had :urls => %w(/ /css /fonts /images /js) and I do not understand why this is happening. I could not find any explanation in the documentation.

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
# ...

How to get Liquid template partials (includes) working with Sinatra?

I am new to ruby and I am trying to get template partials working in my Sinatra + Liquid project.
I have several template partials in my /includes directory.
How can I get all of these templates working as liquid partials so I can use them with liquid include tag?
What I actually have done:
# Sinatra First App
require 'sinatra'
require 'sinatra/config_file'
require 'liquid'
config_file 'config.yml'
# WebRick
set :run, true
set :server, %w[webrick]
# App Paths
set :root, File.dirname(__FILE__)
set :views, File.dirname(__FILE__) + '/views'
set :controlers, File.dirname(__FILE__) + '/controlers'
set :public_folder, Proc.new { File.join(root, "static") }
# Includes Folder
includes = File.dirname(__FILE__) + '/includes'
get '/' do
Liquid::Template.file_system = Liquid::LocalFileSystem.new(includes)
Liquid::Template.parse(includes).render
liquid :index, :locals => { :title => "My Sinatra App"}
end
get '/test' do
"This is the test page."
end
But I am still getting: Liquid error: Illegal template name ''.
Here is my index template:
<html>
{% include 'header' %}
<body>
<h1>{{ title }}</h1>
{{content}}
</body>
</html>
and here is the header part template:
<head>
<title>{{ title }}</title>
</head>
How can I fix it? Thanks for every response.
Of the get '/' route, it seems to me that the first line should be in a configuration block, and the second line shouldn't be needed at all as Tilt/Sinatra should take care of calling render, e.g.
configure do
set :views, File.join(File.dirname(__FILE__),'/includes')
# or just put these in the views dir
end
get '/' do
liquid :index, :locals => { :title => "My Sinatra App"}
end
or, if you want a views directory with sub-directories for partials/includes etc, something like "./views/includes", you could pass the view folder as an option, e.g.
get '/' do
liquid :index, :locals => { :title => "My Sinatra App"}, :views => File.join(File.dirname(__FILE__),'views/includes')
end
or you could try Sinatra Partial (I'm the maintainer).
Since the above didn't work (see comments), I looked at the Sinatra tests for Liquid and played around with the OP's code. I found that the following worked for me:
# ./app.rb
# Sinatra First App
require 'sinatra'
require 'sinatra/config_file'
require 'liquid'
config_file 'config.yml'
configure do
# WebRick
set :run, true
set :server, %w[webrick]
# App Paths
set :root, File.dirname(__FILE__)
set :views, File.dirname(__FILE__) + '/views'
set :controlers, File.dirname(__FILE__) + '/controlers'
set :public_folder, Proc.new { File.join(root, "static") }
Liquid::Template.file_system = Liquid::LocalFileSystem.new(File.join(File.dirname(__FILE__),'views/includes'))
end
get '/' do
liquid :index, :locals => { :title => "My Sinatra App" }
end
get '/test' do
"This is the test page."
end
#./Gemfile
source "https://rubygems.org"
gem "sinatra"
gem "liquid"
gem "sinatra-contrib"
#./views/includes/_header.liquid
<head>
<title>{{ title }}</title>
</head>
#./views/index.liquid
<h1>{{ title }}</h1>
{{content}}
#./views/layout.liquid
<html>
{% include "header" %}
<body>
{{ yield }}
</body>
</html>

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

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.

Sinatra public assets not available when uploading to Heroku

I've just started working on a simple Sinatra app and when uploading it to Heroku. None of the files in the public folder seem to be available but it works fine locally.
Are there any obvious reasons this might be happening?
Right now the code is really simple:
require 'rubygems'
require 'sinatra'
require 'bundler/setup'
require 'haml'
require 'rdiscount'
set :static, true
set :public_folder, "#{File.dirname(__FILE__)}/public"
get '/' do
haml :landing
end
__END__
## layout
%html
%head
%meta{charset: "utf-8"}/
%meta{content: "width=device-width, initial-scale=1.0", name: "viewport"}/
%meta{content: "", name: "description"}/
%meta{content: "", name: "author"}/
%title TIL
%link{href: "http://yui.yahooapis.com/pure/0.3.0/pure-min.css", rel: "stylesheet"}
%link{rel: "stylesheet", href: "/styles.css"}
%body
= yield
##landing
%section.hero
.container
.pure-g-r
.pure-u-1
.logo
...
.container
%hr/
.pure-g-r
.pure-u-2-3
.padding-box
:markdown
...
.pure-u-1-3
.padding-box
..
%hr/
.pure-g-r
.pure-u-1
.padding-box
:markdown
...
%hr/
.pure-g-r
.pure-u-1
.padding-box
%h2 ...
.pure-u-1-3
.padding-box
%img.img-rounded{src: "GD-thumbnail.png"}/
:markdown
...
.pure-u-1-3
.padding-box
%img.img-rounded{src: "AL-thumbnail.png"}/
:markdown
...
.pure-u-1-3
.padding-box
%img.img-rounded{src: "BP-thumbnail.png"}/
:markdown
...
%hr/
%footer
.row
.col-lg-12
%p
Local file structure is:
TIL (folder)
- app.rb
- Gemfile
- Procfile
- public (folder)
- AL-thumbnail.png
- BP-thumbnail.png
- GD-thumbnail.png
- logo.png
- styles.css
Have a look in your Heroku log file:
heroku logs
If you can see something like
Rack::Flash::SessionUnavailable - Rack::Flash depends on session middleware.:
Then add
gem "rack-flash-session"
to you Gemfile.
Also add 'require 'rack/flash/test'' to you main file.
This will force heroku to load the desired middelware.

Resources