Last Updated:
Rails 4: How to Fix 'Failed to Decode Downloaded Font' OTS Parsing Error (Invalid Version Tag) – Icons Showing Squares After Assets Precompile
If you’re working with Ruby on Rails 4 and using icon fonts (e.g., Font Awesome, Material Icons), you might encounter a frustrating issue after deploying your application: instead of crisp icons, you see empty squares. Checking the browser’s developer console reveals an error like:
Failed to decode downloaded font: [URL]
OTS parsing error: Invalid version tag
This error occurs when the browser’s OpenType Sanitizer (OTS) rejects your font files, often due to corruption or misconfiguration during the Rails asset precompilation process. In this blog, we’ll demystify the root causes and walk through step-by-step solutions to restore your icons.
Table of Contents#
Understanding the Error#
What is OTS?#
The OpenType Sanitizer (OTS) is a browser component that validates OpenType font files to prevent security vulnerabilities. If OTS detects invalid data (e.g., corrupt headers, incorrect version tags, or truncated files), it rejects the font, leading to the "Failed to decode downloaded font" error.
Why After Asset Precompile?#
Rails’ Asset Pipeline (powered by Sprockets) processes and compresses assets during assets:precompile. For fonts, this processing can sometimes corrupt files (e.g., via over-compression or incorrect MIME types), triggering OTS errors. Icons appear as squares because the browser can’t render the unreadable font files.
Common Causes#
Before diving into fixes, let’s identify the most likely culprits:
- Overzealous Asset Compression: The Asset Pipeline may compress font files (binary data) using text-based compressors (e.g., Uglifier), corrupting them.
- Missing MIME Types: Rails may not recognize font file extensions (e.g.,
.woff2), leading to incorrect HTTP headers when serving fonts. - Outdated Sprockets: Rails 4 uses Sprockets 2.x/3.x by default; older versions mishandle font processing.
- Corrupted Font Files: Truncated or invalid font files (e.g., from a bad download) fail OTS validation.
- Incorrect CSS Paths:
@font-facedeclarations may use hardcoded paths that break after precompilation.
Step-by-Step Fixes#
Let’s resolve the issue with targeted solutions, starting with the most common fixes.
1. Exclude Fonts from Asset Compression#
Fonts are binary files and should not be compressed by the Asset Pipeline. By default, Rails may apply compression (e.g., Gzip) to all assets, including fonts.
Fix: Skip Compression for Fonts#
Tell Sprockets to skip compression for font file types. Create/modify config/initializers/assets.rb:
# config/initializers/assets.rb
Rails.application.config.assets.configure do |env|
# Skip compression for font files (binary data)
env.skip_compression += %w[.woff .woff2 .ttf .eot .svg]
endThis ensures Sprockets doesn’t compress .woff, .woff2, etc., preventing corruption.
2. Correct MIME Types for Fonts#
Rails serves assets with MIME types defined in config/initializers/mime_types.rb. Missing MIME types for fonts cause browsers to misinterpret the files (e.g., as text/plain instead of font/woff2), triggering OTS errors.
Fix: Add Font MIME Types#
Update config/initializers/mime_types.rb to include font formats:
# config/initializers/mime_types.rb
Rack::Mime::MIME_TYPES.merge!({
'.woff' => 'font/woff',
'.woff2' => 'font/woff2',
'.ttf' => 'application/x-font-truetype',
'.eot' => 'application/vnd.ms-fontobject',
'.svg' => 'image/svg+xml'
})Restart your server after making this change.
3. Update Sprockets and Dependencies#
Rails 4 ships with older Sprockets versions (e.g., Sprockets 2.12.x), which have known issues with font processing. Upgrading to newer compatible versions often resolves these bugs.
Fix: Update Gems#
In your Gemfile, update Sprockets and related gems to the latest versions compatible with Rails 4:
# Gemfile
gem 'sprockets', '~> 3.7.2' # Latest Sprockets 3.x (compatible with Rails 4)
gem 'sprockets-rails', '~> 3.2.2' # Sprockets-Rails bridge
gem 'sass-rails', '~> 5.0.7' # For SCSS processing (handles asset_path)Run bundle update sprockets sprockets-rails sass-rails to install updates.
4. Verify Font File Integrity#
Corrupted font files (e.g., truncated downloads, permission issues) will fail OTS validation regardless of pipeline config.
Fix: Validate and Repair Fonts#
- Re-download Fonts: Get fresh copies of your fonts (e.g., from Font Awesome or Google Fonts).
- Check File Permissions: Ensure fonts in
app/assets/fonts/are readable by the server:chmod 644 app/assets/fonts/* # Readable by owner/group, readable by others - Verify File Types: Use the
filecommand to confirm fonts are valid:file app/assets/fonts/fontawesome-webfont.woff2 # Expected output: "WOFF2 font data"
5. Fix CSS @font-face Declarations#
Incorrect @font-face paths are a common culprit. If your CSS uses hardcoded paths (e.g., url('fonts/fontawesome.woff2')), Sprockets may fail to resolve them after precompilation, leading to broken font URLs.
Fix: Use asset_path Helper#
In SCSS/CSS, use Rails’ asset_path helper to dynamically resolve font paths. This ensures Sprockets generates correct URLs during precompilation.
Example (SCSS):
// app/assets/stylesheets/application.scss
@font-face {
font-family: 'FontAwesome';
src: url(asset_path('fontawesome-webfont.eot')) format('embedded-opentype'),
url(asset_path('fontawesome-webfont.woff2')) format('woff2'),
url(asset_path('fontawesome-webfont.woff')) format('woff'),
url(asset_path('fontawesome-webfont.ttf')) format('truetype');
font-weight: normal;
font-style: normal;
}Why this works: asset_path generates a fingerprinted URL (e.g., fontawesome-webfont-abc123.woff2) after precompilation, ensuring the browser loads the correct file.
6. Temporary Workaround: Serve Fonts from public/#
If all else fails, bypass the Asset Pipeline by serving fonts directly from the public/ directory (Rails serves public/ files without processing).
Steps:#
- Move fonts to
public/fonts/:mkdir -p public/fonts mv app/assets/fonts/* public/fonts/ - Update
@font-faceto use/fonts/paths (noasset_pathneeded):@font-face { src: url('/fonts/fontawesome-webfont.woff2') format('woff2'); }
Caveat: This skips the Asset Pipeline, so fonts won’t be fingerprinted (risk of caching issues). Use this only as a temporary fix.
Preventive Measures#
To avoid future font issues:
- Test Precompilation Locally: Run
RAILS_ENV=production bundle exec rake assets:precompilelocally to catch errors before deployment. - Pin Dependency Versions: Lock Sprockets and
sass-railsversions inGemfileto avoid unexpected updates. - Use Font Subsets: For icons, use subsets (e.g., Font Awesome’s
all.min.css) to reduce file size and corruption risk. - Leverage CDNs: Serve fonts via CDNs (e.g., Font Awesome CDN) to bypass the Asset Pipeline entirely.
Conclusion#
The "Failed to Decode Downloaded Font" error in Rails 4 is almost always caused by Asset Pipeline misconfiguration or corrupted fonts. By excluding fonts from compression, fixing MIME types, updating dependencies, and validating @font-face paths, you can resolve the issue and restore your icons.
Start with the simplest fixes (MIME types and asset_path), then move to dependency updates or file validation. With these steps, your icons will render correctly—no more squares!