mirror of
https://github.com/plausible/analytics.git
synced 2024-12-27 19:47:26 +03:00
Add plug for source favicons
This commit is contained in:
parent
39f5bc6afb
commit
6758931431
@ -59,7 +59,7 @@ class AllSources extends React.Component {
|
||||
to={{search: query.toString()}}
|
||||
>
|
||||
<img
|
||||
src={`https://icons.duckduckgo.com/ip3/${referrer.url}.ico`}
|
||||
src={`/favicon/sources/${referrer.name}`}
|
||||
referrerPolicy="no-referrer"
|
||||
className="inline w-4 h-4 mr-2 -mt-px align-middle"
|
||||
/>
|
||||
|
29
lib/mix/tasks/generate_referrer_favicons.ex
Normal file
29
lib/mix/tasks/generate_referrer_favicons.ex
Normal file
@ -0,0 +1,29 @@
|
||||
defmodule Mix.Tasks.GenerateReferrerFavicons do
|
||||
use Mix.Task
|
||||
use Plausible.Repo
|
||||
require Logger
|
||||
|
||||
# coveralls-ignore-start
|
||||
|
||||
def run(_) do
|
||||
entries =
|
||||
:yamerl_constr.file(Application.app_dir(:plausible, "priv/ref_inspector/referers.yml"))
|
||||
|> List.first()
|
||||
|> Enum.map(fn {_key, val} -> val end)
|
||||
|> Enum.concat()
|
||||
|
||||
domains =
|
||||
Enum.reduce(entries, %{}, fn {key, val}, domains ->
|
||||
domain =
|
||||
Enum.into(val, %{})['domains']
|
||||
|> List.first()
|
||||
|
||||
Map.put_new(domains, List.to_string(key), List.to_string(domain))
|
||||
end)
|
||||
|
||||
File.write!(
|
||||
Application.app_dir(:plausible, "priv/referer_favicon_domains.json"),
|
||||
Jason.encode!(domains)
|
||||
)
|
||||
end
|
||||
end
|
@ -7,6 +7,7 @@ defmodule PlausibleWeb.Endpoint do
|
||||
# You should set gzip to true if you are running phx.digest
|
||||
# when deploying your static files in production.
|
||||
plug PlausibleWeb.Tracker
|
||||
plug PlausibleWeb.Favicon
|
||||
|
||||
plug Plug.Static,
|
||||
at: "/",
|
||||
|
37
lib/plausible_web/plugs/favicon.ex
Normal file
37
lib/plausible_web/plugs/favicon.ex
Normal file
@ -0,0 +1,37 @@
|
||||
defmodule PlausibleWeb.Favicon do
|
||||
import Plug.Conn
|
||||
|
||||
def init(_) do
|
||||
domains =
|
||||
case File.read(Application.app_dir(:plausible, "priv/referer_favicon_domains.json")) do
|
||||
{:ok, contents} ->
|
||||
Jason.decode!(contents)
|
||||
|
||||
_ ->
|
||||
%{}
|
||||
end
|
||||
|
||||
[favicon_domains: domains]
|
||||
end
|
||||
|
||||
def call(conn, favicon_domains: favicon_domains) do
|
||||
case conn.path_info do
|
||||
["favicon", "sources", source] ->
|
||||
clean_source = URI.decode_www_form(source)
|
||||
domain = Map.get(favicon_domains, clean_source, clean_source)
|
||||
res = HTTPoison.get!("https://icons.duckduckgo.com/ip3/#{domain}.ico")
|
||||
|
||||
conn =
|
||||
Enum.filter(res.headers, fn {key, _val} -> key != "Transfer-Encoding" end)
|
||||
|> Enum.reduce(conn, fn {key, val}, conn ->
|
||||
put_resp_header(conn, key, val)
|
||||
end)
|
||||
|
||||
send_resp(conn, 200, res.body)
|
||||
|> halt
|
||||
|
||||
_ ->
|
||||
conn
|
||||
end
|
||||
end
|
||||
end
|
1
priv/referer_favicon_domains.json
Normal file
1
priv/referer_favicon_domains.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user