I have an existing rails
application where I am working on a separate branch to implement yarn
for managing my vendor assets. My app stack is:
- ruby-2.4.0
- rails 5.1.4
- node 9.4.0
- yarn 1.3.2
After installing yarn, I ran yarn init
and it generated a package.json in the project root. After a few packages added, it looks like:
# package.json
{
"name": "my-project-name",
"version": "1.0.0",
"private": true,
"repository": "my-repo",
"author": "me",
"license": "MIT",
"dependencies": {
"bootstrap": "3",
"font-awesome": "^4.7.0",
"jquery": "^3.3.1",
"jquery-backstretch": "^2.1.16",
"jquery-ujs": "^1.2.2",
"waypoints": "^4.0.1"
}
}
I created under app/assets a couple of files for including stylesheets and javascripts from node_modules
:
# app/assets/javascripts/node_modules.js
//= require jquery
//= require jquery-ujs
//= require bootstrap/dist/js/bootstrap.min.js
//= require waypoints
//= require jquery-backstretch
# app/assets/stylesheets/font_path_overwrite.scss
$fa-font-path: "font-awesome/fonts/";
# app/assets/stylesheets/node_modules.css.scss
/*
*= require bootstrap/dist/css/bootstrap.min.css
*= require font_path_overwrite
*= require font-awesome/scss/font-awesome
*/
I also changed:
# config/initializers/assets.rb
# ...
Rails.application.config.assets.paths << Rails.root.join('node_modules')
Rails.application.config.assets.precompile += %w(*.js *.scss)
Rails.application.config.assets.precompile += %w(*.png *.woff2 *.woff *.ttf *.eot *.jpg *.gif *.svg *.ico)
# Rakefile
# ...
Rake::Task['assets:precompile'].enhance [:js_deps_install]
task :js_deps_install do
sh 'yarn install'
end
As per SO-43170792, I added a new
heroku buildpacks:set heroku/ruby
heroku buildpacks:add --index 1 heroku/nodejs
In my staging environment I deployed these changes and I suddenly realized that something strange is happening with my assets precompilation node_modules/
folder: whilst everything seems to work fine for js and css files, I am having some issues with fonts (especially font-awesome's fonts).
Debugging in the network inspector the file assets/font-awesome/fonts/fontawesome-webfont.ttf?v=4.7.0
is not found since it was precompiled and not just copied into:
public/assets/font-awesome/fonts/fontawesome-webfont-[hash].ttf
I understand why I am facing this situation (which is very similar to this unaswered question from two months ago).
I am looking for a clean solution or smart workaround (e.g. create a middleware to redirect the plain .ttf
request to the rails complaint hashed version and cache the answer for x hours. Would you have any suggestion?