SVG

Tools for caching and serving SVGs.

Installation

Add to your mix.exs as usual:

def deps do
[{:svg, "~> 1.0.0"}]
end

If you're not using application inference, then add :svg to your applications list.

Then configure it in your config.exs:

config :svg, otp_app: :my_app # Replace :my_app

Using encoded SVGs at runtime

Run the background server:

defmodule YourApp do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
# .. Ecto, Repo, etc.
worker(SVG.Server, [])
]
# ...
end
end

The server will build up cache when the main application starts. It will read all svgs in your priv/static/images folder. You can then get the base64 encoded svg like so:

<img src="<%=raw SVG.Server.get("icons/add") %>">

Where icons/add corresponds to priv/static/icons/add.svg on the filesystem. You may want to alias the server when using in templates:

alias SVG.Server, as: SVG

and then you can use SVG.get/1 directly.

About

This project is sponsored by Heresy. We're always looking for great engineers to join our team, so if you love Elixir, open source and enjoy some challenge, drop us a line and say hello!

License