• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    Ruby
  • Created almost 13 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Helps with serving font assets with Rails 3.1

Font Assets

This little gem helps serve font-face assets in Rails 3.1. It really does these two things:

  • Responds with "proper" mime types for woff, eot, tff, and svg font files, and
  • Sets Access-Control-Allow-Origin response headers for font assets, which Firefox requires for cross domain fonts.

In addition, it will also respond to the pre-flight OPTIONS requests made by supporting browsers (Firefox).

Important

This gem only works if you are NOT precompiling your assets in production, because it requires asset requests to go through the Rails app, and if you precompile your assets then you are most likely serving them through a web server like NGINX. If this is the case, don't use this gem but instead look into configuring the CORS headers for font requests. For example, here is a way to do that in NGINX.

Install

Add font_assets to your Gemfile:

gem 'font_assets'

Usage

By default, in a Rails application, this gem should Just Workβ„’. However, the default settings allow any requesting site to use the linked fonts, due to the Allowed Origin being '*', by default. This is only useful for browsers which support this feature (Firefox), but restricting it to certain domains may be beneficial.

Set the origin domain that will get set in the Access-Control-Allow-Origin header:

# in config/environments/production.rb
config.font_assets.origin = 'http://codeschool.com'

The origin domain must match the domain of the site serving the page that is requesting the font, not the host of the font. For example, if you are using a CDN to serve your assets (like CloudFront), and the full path to the font asset is http://d3rd6rvl24noqd.cloudfront.net/assets/fonts/Pacifico-webfont-734f1436e605566ae2f1c132905e28b2.woff, but the URI the user is visiting is http://coffeescript.codeschool.com/level/1, you'd want to set the origin header to this:

config.font_assets.origin = 'http://coffeescript.codeschool.com'

To match the actual Origin value, omit the trailing slash (details).

An Example Response

Below is an example response for a .woff font asset on a Rails 3.1 application running behind several proxies and caches (including CloudFront):

$ curl -i http://d1tijy5l7mg5kk.cloudfront.net/assets/ubuntu/Ubuntu-Bold-webfont-4bcb5239bfd34be67bc07901959fc6e1.woff
HTTP/1.0 200 OK
Server: nginx
Date: Sat, 14 Jan 2012 19:45:19 GMT
Content-Type: application/x-font-woff
Last-Modified: Sat, 14 Jan 2012 16:58:14 GMT
Cache-Control: public, max-age=31557600
Access-Control-Allow-Origin: http://www.codeschool.com
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: x-requested-with
Access-Control-Max-Age: 3628800
X-Content-Digest: 66049433125f563329c4178848643536f76459e5
X-Rack-Cache: fresh
Content-Length: 17440
X-Varnish: 311344447
Age: 289983
X-Cache: Hit from cloudfront
X-Amz-Cf-Id: 9yzifs_hIQF_MxPLwSR8zck3eZVXJ8LFKpMUpXnu2SmMuEmyrUbHdQ==,Lbh9kfjr0YRm77seSmOSQ6oFkUEMabvtFStJLhTOy9BfGrIXVneoKQ==
Via: 1.1 varnish, 1.0 2815dd16e8c2a0074b81a6148bd8aa3a.cloudfront.net:11180 (CloudFront), 1.0 f9e7403ca14431787835521769ace98a.cloudfront.net:11180 (CloudFront)
Connection: close

In it, you can see where this middleware has injected the Content-Type and Access-Control-* headers into the response.

And below is an example OPTIONS request response:

$ curl -i -X OPTIONS http://www.codeschool.com/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 18 Jan 2012 04:13:25 GMT
Connection: keep-alive
Access-Control-Allow-Origin: http://www.codeschool.com
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: x-requested-with
Access-Control-Max-Age: 3628800
Vary: Accept-Encoding
X-Rack-Cache: invalidate, pass
Content-Length: 0

License

Font Assets is released under the MIT license.

Contributors

More Repositories

1

Paper-StatusBarHover-Demo

Check out how to build this feature here: http://initwithfunk.com/blog/2014/02/07/how-to-build-the-facebook-paper-status-bar-hover/
Objective-C
76
star
2

PhotoTransitionsExample

Objective-C
49
star
3

spdy-with-server-push-tutorial

A short tutorial on creating a SPDY server and using server push
JavaScript
48
star
4

MarkyMark

Using iOS 7 new HTML attributed string document type to easily render markdown
Objective-C
40
star
5

express-mock-request

The "Mock Request" object extracted from express
JavaScript
20
star
6

merb-oauth

Provides Merb apps with OAuth Provider functionality
Ruby
16
star
7

twitter-text-js

A javascript implementation of http://github.com/mzsanford/twitter-text-conformance
JavaScript
13
star
8

execjs-async

Enables asynchronous Javascript execution in ExecJS
Ruby
12
star
9

Azebiki

Cut into the Wood - A DSL for Webrat matchers
Ruby
11
star
10

merb-oauth-example

An example merb app that uses the merb-oauth plugin
Ruby
10
star
11

RubyCop

A Semantic Analyzer for Ruby 1.9
Ruby
8
star
12

WellCached

An NSCache subclass that provides keyed-subscripting, time-based expiration, and a Rails-like 'fetch' API for caching asynchronous data
Objective-C
8
star
13

P2PWalkieTalkie

Peer to Peer walkie talkie on iOS
Objective-C
7
star
14

twitter-userstream

A node.js client for the Twitter Userstream
JavaScript
6
star
15

try-objectivec-book

HTML version of the Try Objective-C course
JavaScript
6
star
16

hairball

A Haml clone built using the Treetop parser generator
Ruby
4
star
17

me-moustache

3
star
18

OpenBadges

Ruby
3
star
19

NodeStream

Stream to the NODES
JavaScript
3
star
20

xfactor

Refactor Xcode projects from the command line
3
star
21

docrails

Documentation Project for Rails
Ruby
3
star
22

nodejs-epipe-error

Demonstrating an EPIPE exception in node.js when using spawn
JavaScript
3
star
23

touchpickurl

Spiking adding a url for twitfire
Objective-C
2
star
24

dynamodb-spec-generator

A command line tool to generate documentation for a DynamoDB table and its access patterns.
JavaScript
2
star
25

Make-a-Scene

JavaScript
2
star
26

NoComments

Block comments from around the web. Be Happier.
JavaScript
2
star
27

devtools-console

JavaScript
2
star
28

node-ajax-file-upload

Tutorial for how to do Ajax file uploads with Node.JS and XMLHttpRequest 2
JavaScript
2
star
29

Engine-App

A rails edge engine created to demonstrate https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6388
JavaScript
1
star
30

EASwizzler

Objective-C class used for Method Swizzling
1
star
31

refinerycms-catxmlimport

Ruby
1
star
32

Presentation-ActiveSupport

Ruby
1
star
33

github-stars-staging-1

TypeScript
1
star
34

aws-sam-yarn-builder

Command to build AWS SAM apps that use nodejs with yarn and local file dependencies
Ruby
1
star
35

XcodeCodeSnippets

1
star
36

site

1
star
37

rubymaverick.github.com

JavaScript
1
star
38

TumblrDomains

Ruby
1
star
39

Parent-App

A Rails Edge app created to demonstrate https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6388
JavaScript
1
star
40

AppleDocsAPI

Ruby
1
star
41

TwitterFriends

Just an app to mess around with the twitter api
Objective-C
1
star
42

node-git-runner

Provides an easy way to run git commands against fixture git repos and get the result
CoffeeScript
1
star
43

Captionr

Making it easy to build caption (srt) files for HTML5 videos
Ruby
1
star
44

xcode_rename_file

Command line interface for renaming a file in an xcode project
Ruby
1
star
45

Testing-Issue-1822

This app reproduces https://github.com/rails/rails/issues/1822#issuecomment-1647612
Ruby
1
star