Quick and Easy Favicons
Add all the favions you need with one icon.svg and this template. Inkscape and SVGO CLI tools required.
Used 124 times
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/zamsKD"
Template Source
Review the code before running this template on your machine.
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
def tool_installed?(tool)
system("which #{tool} > /dev/null 2>&1")
end
abort "ImageMagick is required to continue." unless tool_installed?("magick")
abort "Inkscape is required to continue." unless tool_installed?("inkscape")
abort "SVGO is required to continue" unless tool_installed?("svgo")
abort "icon.svg is not present in the root folder" unless File.exist?("icon.svg")
app_name = ask("What is the name of your Rails app?") || "Unnamed"
say "Creating the favicon.ico"
run "inkscape ./icon.svg --export-width=32 --export-filename='./tmp.png'"
run "magick ./tmp.png ./public/favicon.ico"
run "rm ./tmp.png"
say "Creating the PNG files"
run "inkscape ./icon.svg --export-width=512 --export-filename='./public/icon-512.png'"
run "inkscape ./icon.svg --export-width=192 --export-filename='./public/icon-192.png'"
run "inkscape ./icon.svg --export-width=180 --export-filename='./public/apple-touch-icon.png'"
say "Optimizing the SVG file"
run "npx svgo --multipass icon.svg"
say "Create additional files"
create_file "public/manifest.webmanifest", <<~JSON
{
"name": "#{app_name}",
"icons": [
{ "src": "/icon-192.png", "type": "image/png", "sizes": "192x192" },
{ "src": "/icon-512.png", "type": "image/png", "sizes": "512x512" }
]
}
JSON
create_file "app/views/shared/_favicons.html.erb", <<~ERB
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/manifest.webmanifest">
ERB
say "Moving the source icon.svg to the /public/ folder"
run "mv icon.svg public/"
say "Inserting the favicons partial with the application's layout <head>"
if APPLICATION_LAYOUT_PATH.exist?
application_content = File.read(APPLICATION_LAYOUT_PATH)
if File.open(APPLICATION_LAYOUT_PATH).read =~ /<\/head>/
insert_into_file APPLICATION_LAYOUT_PATH.to_s, <<~ERB.indent(2), before: /^\s*<\/head>/
<%= render partial: "shared/favicons" %>
ERB
else
say "The <head> tag is missing in your application layout", :red
say %( Add <%= render partial: "shared/favicons" %> within the <head> of your layout.)
end
else
say "Default application.html.erb is missing!", :red
say %( Add <%= render partial: "shared/favicons" %> within the <head> of your layout.)
end
say "All done! ✨", :green
# Needed so `bundle install` does not run
def run_bundle ; end
Comments
Eelco from Rails Designer
See all the manual steps outlined here: https://railsdesigner.com/favicons-in-rails/
Eelco from Rails Designer
See all the manual steps outlined here: https://railsdesigner.com/favicons-in-rails/