Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Update favicon placeholder for sites (#6671)

Sanne de Vries5b0a66720bb3
diff --git a/assets/js/dashboard/components/icons.tsx b/assets/js/dashboard/components/icons.tsx
index e9a28e4b8d..a4c2726471 100644
--- a/assets/js/dashboard/components/icons.tsx
+++ b/assets/js/dashboard/components/icons.tsx
@@ -1,29 +1,5 @@
import React from 'react'
-export const GlobeIcon = ({ className }: { className?: string }) => (
- <svg
- xmlns="http://www.w3.org/2000/svg"
- fill="none"
- viewBox="0 0 24 24"
- className={className}
- >
- <path
- stroke="currentColor"
- strokeLinecap="round"
- strokeLinejoin="round"
- strokeWidth="1.5"
- d="M22 12H2M12 22c5.714-5.442 5.714-14.558 0-20M12 22C6.286 16.558 6.286 7.442 12 2"
- />
- <path
- stroke="currentColor"
- strokeLinecap="round"
- strokeLinejoin="round"
- strokeWidth="1.5"
- d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10Z"
- />
- </svg>
-)
-
export const FilterIcon = ({ className }: { className?: string }) => (
<svg
xmlns="http://www.w3.org/2000/svg"
diff --git a/assets/js/dashboard/components/site-icon.tsx b/assets/js/dashboard/components/site-icon.tsx
new file mode 100644
index 0000000000..eee0d6e4d7
--- /dev/null
+++ b/assets/js/dashboard/components/site-icon.tsx
@@ -0,0 +1,69 @@
+/**
+ * @prettier
+ */
+import React from 'react'
+import classNames from 'classnames'
+import { useTheme } from '../theme-context'
+
+const siteIconClassName = 'shrink-0 size-5.5 rounded-md'
+
+/**
+ * A site is drawn as its favicon, proxied through PlausibleWeb.Favicon. Sites
+ * without a favicon get priv/site_favicon_placeholder.svg from that plug, in
+ * answer to `placeholder=site`. The plug must be told the UI mode, because an
+ * image cannot see the `dark` class on the page.
+ */
+export const Favicon = ({ domain }: { domain: string }) => {
+ const { mode } = useTheme()
+
+ return (
+ <img
+ aria-hidden="true"
+ alt=""
+ src={`/favicon/sources/${encodeURIComponent(domain)}?placeholder=site&ui-mode=${mode}`}
+ onError={(e) => {
+ const target = e.target as HTMLImageElement
+ target.onerror = null
+ target.src = `/favicon/placeholders/site?ui-mode=${mode}`
+ }}
+ referrerPolicy="no-referrer"
+ className={siteIconClassName}
+ />
+ )
+}
+
+/**
+ * The same drawing as priv/site_favicon_placeholder.svg in indigo, so the
+ * geometry of the two must change together.
+ */
+export const ConsolidatedViewIcon = () => (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 20 20"
+ aria-hidden="true"
+ className={classNames(siteIconClassName, 'text-white')}
+ >
+ <rect width="20" height="20" rx="5" className="fill-indigo-600" />
+ <rect
+ x=".5"
+ y=".5"
+ width="19"
+ height="19"
+ rx="4.5"
+ fill="none"
+ strokeWidth="1.8"
+ className="stroke-white/15"
+ />
+ <g
+ transform="translate(3 3) scale(.5833)"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.8"
+ >
+ <path d="M22 12H2M12 22c5.714-5.442 5.714-14.558 0-20M12 22C6.286 16.558 6.286 7.442 12 2" />
+ <path d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10Z" />
+ </g>
+ </svg>
+)
diff --git a/assets/js/dashboard/nav-menu/top-bar.tsx b/assets/js/dashboard/nav-menu/top-bar.tsx
index 6229644100..aba9aa7355 100644
--- a/assets/js/dashboard/nav-menu/top-bar.tsx
+++ b/assets/js/dashboard/nav-menu/top-bar.tsx
@@ -49,7 +49,7 @@ function TopBarInner({ showCurrentVisitors }: TopBarProps) {
return (
<div className="flex min-w-0 flex-nowrap items-center gap-x-1 md:gap-x-2.5 overflow-x-auto md:overflow-visible w-full touch-pan-x md:touch-auto [scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden py-1 -my-1 md:py-0 md:my-0">
<div
- className="flex shrink-0 items-center gap-x-1 md:gap-x-2.5"
+ className="flex shrink-0 items-center sm:gap-x-1 md:gap-x-2.5"
ref={leftActionsRef}
>
<SiteSwitcher />
diff --git a/assets/js/dashboard/site-switcher.tsx b/assets/js/dashboard/site-switcher.tsx
index 8dd48bacbc..d684fb6415 100644
--- a/assets/js/dashboard/site-switcher.tsx
+++ b/assets/js/dashboard/site-switcher.tsx
@@ -8,7 +8,7 @@ import { Cog8ToothIcon, ArrowLeftIcon } from '@heroicons/react/24/outline'
import classNames from 'classnames'
import { isModifierPressed, isTyping, Keybind, KeybindHint } from './keybinding'
import { popover, BlurMenuButtonOnEscape } from './components/popover'
-import { GlobeIcon } from './components/icons'
+import { ConsolidatedViewIcon, Favicon } from './components/site-icon'
import { useQuery } from '@tanstack/react-query'
import { Role, useUserContext } from './user-context'
import { PlausibleSite, useSiteContext } from './site-context'
@@ -19,31 +19,11 @@ import { get } from './api'
import { ErrorPanel } from './components/error-panel'
import { useRoutelessModalsContext } from './navigation/routeless-modals-context'
-const Favicon = ({
- domain,
- className
-}: {
- domain: string
- className?: string
-}) => (
- <img
- aria-hidden="true"
- alt=""
- src={`/favicon/sources/${encodeURIComponent(domain)}`}
- onError={(e) => {
- const target = e.target as HTMLImageElement
- target.onerror = null
- target.src = '/favicon/sources/placeholder'
- }}
- referrerPolicy="no-referrer"
- className={className}
- />
-)
-
const menuItemClassName = classNames(
popover.items.classNames.navigationLink,
popover.items.classNames.selectedOption,
- popover.items.classNames.hoverLink
+ popover.items.classNames.hoverLink,
+ 'gap-x-2'
)
const buttonLinkClassName = classNames(
@@ -83,7 +63,7 @@ const SiteSwitcherStatic = () => {
)}
title={currentSite.domain}
>
- <Favicon domain={currentSite.domain} className="block size-4" />
+ <Favicon domain={currentSite.domain} />
<span className="truncate hidden sm:block sm:mr-1 lg:mr-0 font-semibold">
{currentSite.domain}
</span>
@@ -181,14 +161,14 @@ export const SiteSwitcher = () => {
className={classNames(
popover.toggleButton.classNames.rounded,
popover.toggleButton.classNames.ghost,
- '!pl-1.5'
+ '!px-1.5 sm:!pl-1.5 sm:!pr-2.5'
)}
title={currentSite.domain}
>
{currentSite.isConsolidatedView ? (
- <GlobeIcon className="size-4 block mx-1 h-4 w-4 text-indigo-600 dark:text-white" />
+ <ConsolidatedViewIcon />
) : (
- <Favicon domain={currentSite.domain} className="block size-4" />
+ <Favicon domain={currentSite.domain} />
)}
<span
data-testid="site-switcher-current-site"
@@ -258,7 +238,7 @@ export const SiteSwitcher = () => {
}
onClick={() => closePopover()}
>
- <GlobeIcon className="size-4 block mr-2 text-indigo-600 dark:text-white" />
+ <ConsolidatedViewIcon />
<span className="truncate mr-auto">All sites</span>
<KeybindHint>0</KeybindHint>
</a>
@@ -281,7 +261,7 @@ export const SiteSwitcher = () => {
: () => {}
}
>
- <Favicon domain={domain} className="h-4 w-4 block mr-2" />
+ <Favicon domain={domain} />
<span className="truncate mr-auto">{domain}</span>
{sitesInDropdown.length > 1 && (
<KeybindHint>{index + 1}</KeybindHint>
diff --git a/extra/lib/plausible_web/live/customer_support/team/components/sites.ex b/extra/lib/plausible_web/live/customer_support/team/components/sites.ex
index 09a7b2c254..3b8c6b31c7 100644
--- a/extra/lib/plausible_web/live/customer_support/team/components/sites.ex
+++ b/extra/lib/plausible_web/live/customer_support/team/components/sites.ex
@@ -123,8 +123,8 @@ defmodule PlausibleWeb.CustomerSupport.Team.Components.Sites do
<div class="flex items-center">
<img
src="/favicon/sources/{site.domain}"
- onerror="this.onerror=null; this.src='/favicon/sources/placeholder';"
- class="w-4 h-4 flex-shrink-0 mt-px mr-2"
+ onerror="this.onerror=null; this.src='/favicon/placeholders/source';"
+ class="size-4 flex-shrink-0 mt-px mr-2"
/>
<.styled_link
patch={Routes.customer_support_site_path(PlausibleWeb.Endpoint, :show, site.id)}
diff --git a/lib/plausible_web/live/sites.ex b/lib/plausible_web/live/sites.ex
index 6ee11103c3..1dc3d950cb 100644
--- a/lib/plausible_web/live/sites.ex
+++ b/lib/plausible_web/live/sites.ex
@@ -442,7 +442,7 @@ defmodule PlausibleWeb.Live.Sites do
>
<div class="flex flex-col flex-1 justify-between gap-y-5">
<div class="flex flex-col gap-y-2 mb-auto">
- <span class="size-8 sm:size-10 bg-indigo-600 text-white p-1.5 sm:p-2 rounded-lg sm:rounded-xl">
+ <span class="size-8 sm:size-10 bg-indigo-600 ring-2 ring-inset ring-white/25 dark:ring-white/15 text-white p-1.5 sm:p-2 rounded-lg sm:rounded-xl">
<.globe_icon />
</span>
<h3 class="text-gray-900 font-medium text-md sm:text-lg leading-tight dark:text-gray-100">
@@ -811,11 +811,15 @@ defmodule PlausibleWeb.Live.Sites do
end
def favicon(assigns) do
- src = "/favicon/sources/#{assigns.domain}"
- assigns = assign(assigns, :src, src)
+ assigns =
+ assign(assigns,
+ light_src: "/favicon/sources/#{assigns.domain}?placeholder=site&ui-mode=light",
+ dark_src: "/favicon/sources/#{assigns.domain}?placeholder=site&ui-mode=dark"
+ )
~H"""
- <img src={@src} class="size-[18px] shrink-0" />
+ <img src={@light_src} alt="" class="shrink-0 size-6 rounded-md dark:hidden" />
+ <img src={@dark_src} alt="" class="shrink-0 size-6 rounded-md hidden dark:block" />
"""
end
diff --git a/lib/plausible_web/plugs/favicon.ex b/lib/plausible_web/plugs/favicon.ex
index 4f9c4f2aae..d490d1883a 100644
--- a/lib/plausible_web/plugs/favicon.ex
+++ b/lib/plausible_web/plugs/favicon.ex
@@ -29,9 +29,17 @@ defmodule PlausibleWeb.Favicon do
import Plug.Conn
alias Plausible.HTTPClient
- @placeholder_icon_location "priv/link_favicon.svg"
- @placeholder_icon File.read!(@placeholder_icon_location)
- @external_resource @placeholder_icon_location
+ @source_placeholder_icon_location "priv/link_favicon.svg"
+ @source_placeholder_icon File.read!(@source_placeholder_icon_location)
+ @external_resource @source_placeholder_icon_location
+
+ @site_placeholder_icon_location "priv/site_favicon_placeholder.svg"
+ @site_placeholder_icon File.read!(@site_placeholder_icon_location)
+ @external_resource @site_placeholder_icon_location
+
+ @site_dark_placeholder_icon_location "priv/site_favicon_placeholder_dark.svg"
+ @site_dark_placeholder_icon File.read!(@site_dark_placeholder_icon_location)
+ @external_resource @site_dark_placeholder_icon_location
@custom_icons %{
"Brave" => "search.brave.com",
"Kagi" => "kagi.com",
@@ -81,6 +89,17 @@ defmodule PlausibleWeb.Favicon do
but we filter that out. When the icon request fails, we show a placeholder
favicon instead. The placeholder is an svg from [https://heroicons.com/](https://heroicons.com/).
+ There are two placeholders, and `?placeholder=` picks one:
+
+ - `source` is `#{@source_placeholder_icon_location}`, a plain
+ link icon for the referrer rows in the reports.
+ - `site` is `#{@site_placeholder_icon_location}`, a globe on a rounded grey
+ background, for the rows that list sites.
+
+ `?ui-mode=light|dark` picks the colours and defaults to `light`. An SVG served
+ as an image cannot see the `dark` class on the page, so the caller must say
+ which mode it needs.
+
DuckDuckGo favicon service has some issues with [SVG favicons](https://css-tricks.com/svg-favicons-and-all-the-fun-things-we-can-do-with-them/).
For some reason, they return them with `content-type=image/x-icon` whereas SVG
icons should be returned with `content-type=image/svg+xml`. This Plug detects
@@ -104,8 +123,8 @@ defmodule PlausibleWeb.Favicon do
"""
def call(conn, favicon_domains: favicon_domains) do
case conn.request_path do
- "/favicon/sources/placeholder" ->
- send_placeholder(conn)
+ "/favicon/placeholders/" <> name ->
+ send_placeholder(conn, name)
"/favicon/sources/" <> domain ->
domain = URI.decode_www_form(domain)
@@ -134,14 +153,21 @@ defmodule PlausibleWeb.Favicon do
end
end
- defp send_placeholder(conn) do
+ defp send_placeholder(conn, name \\ nil) do
+ conn = fetch_query_params(conn)
+ name = name || conn.query_params["placeholder"]
+
conn
|> put_resp_content_type("image/svg+xml")
|> put_resp_header("cache-control", "public, max-age=2592000")
- |> send_resp(200, @placeholder_icon)
+ |> send_resp(200, placeholder_icon(name, conn.query_params["ui-mode"]))
|> halt
end
+ defp placeholder_icon("site", "dark"), do: @site_dark_placeholder_icon
+ defp placeholder_icon("site", _ui_mode), do: @site_placeholder_icon
+ defp placeholder_icon(_source, _ui_mode), do: @source_placeholder_icon
+
@forwarded_headers ["content-type", "cache-control", "expires"]
defp forward_headers(%Plug.Conn{} = conn, headers) do
headers_to_forward = Enum.filter(headers, fn {k, _} -> k in @forwarded_headers end)
diff --git a/priv/globe_favicon.svg b/priv/globe_favicon.svg
deleted file mode 100644
index 7508112411..0000000000
--- a/priv/globe_favicon.svg
+++ /dev/null
@@ -1,6 +0,0 @@
-<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
- <path d="M21 11H1" stroke="#a1a1aa" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
- <path d="M11 21C16.7143 15.5578 16.7143 6.44218 11 1" stroke="#a1a1aa" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
- <path d="M11.0006 21C5.28627 15.5578 5.28627 6.44218 11.0006 1" stroke="#a1a1aa" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
- <path d="M11 21C16.5228 21 21 16.5228 21 11C21 5.47715 16.5228 1 11 1C5.47715 1 1 5.47715 1 11C1 16.5228 5.47715 21 11 21Z" stroke="#a1a1aa" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
-</svg>
\ No newline at end of file
diff --git a/priv/site_favicon_placeholder.svg b/priv/site_favicon_placeholder.svg
new file mode 100644
index 0000000000..a4334cd7a5
--- /dev/null
+++ b/priv/site_favicon_placeholder.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><rect width="20" height="20" rx="5" fill="#f4f4f5"></rect><rect x=".5" y=".5" width="19" height="19" rx="4.5" fill="none" stroke="#71717b" stroke-width="1.8" stroke-opacity=".1"></rect><g transform="translate(3 3) scale(.5833)" fill="none" stroke="#71717b" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.8"><path d="M22 12H2M12 22c5.714-5.442 5.714-14.558 0-20M12 22C6.286 16.558 6.286 7.442 12 2"></path><path d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10Z"></path></g></svg>
diff --git a/priv/site_favicon_placeholder_dark.svg b/priv/site_favicon_placeholder_dark.svg
new file mode 100644
index 0000000000..5aec4931dd
--- /dev/null
+++ b/priv/site_favicon_placeholder_dark.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><rect width="20" height="20" rx="5" fill="#3f3f47"></rect><rect x=".5" y=".5" width="19" height="19" rx="4.5" fill="none" stroke="#ffffff" stroke-width="1.8" stroke-opacity=".1"></rect><g transform="translate(3 3) scale(.5833)" fill="none" stroke="#D4D4D8" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.8"><path d="M22 12H2M12 22c5.714-5.442 5.714-14.558 0-20M12 22C6.286 16.558 6.286 7.442 12 2"></path><path d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10Z"></path></g></svg>
diff --git a/test/plausible_web/plugs/favicon_test.exs b/test/plausible_web/plugs/favicon_test.exs
index 97db7d6dbf..de5c191282 100644
--- a/test/plausible_web/plugs/favicon_test.exs
+++ b/test/plausible_web/plugs/favicon_test.exs
@@ -172,7 +172,9 @@ defmodule PlausibleWeb.FaviconTest do
end
describe "Fallback to placeholder icon" do
- @placeholder_icon File.read!("priv/link_favicon.svg")
+ @source_placeholder_icon File.read!("priv/link_favicon.svg")
+ @site_placeholder_icon File.read!("priv/site_favicon_placeholder.svg")
+ @site_dark_placeholder_icon File.read!("priv/site_favicon_placeholder_dark.svg")
test "falls back to placeholder when DDG returns a non-2xx response", %{plug_opts: plug_opts} do
expect(
@@ -190,7 +192,7 @@ defmodule PlausibleWeb.FaviconTest do
assert conn.halted
assert conn.status == 200
- assert conn.resp_body == @placeholder_icon
+ assert conn.resp_body == @source_placeholder_icon
end
test "falls back to placeholder in case of a network error", %{plug_opts: plug_opts} do
@@ -208,7 +210,7 @@ defmodule PlausibleWeb.FaviconTest do
assert conn.halted
assert conn.status == 200
- assert conn.resp_body == @placeholder_icon
+ assert conn.resp_body == @source_placeholder_icon
end
test "falls back to placeholder when DDG returns a broken image response", %{
@@ -228,7 +230,91 @@ defmodule PlausibleWeb.FaviconTest do
assert conn.halted
assert conn.status == 200
- assert conn.resp_body == @placeholder_icon
+ assert conn.resp_body == @source_placeholder_icon
+ end
+
+ test "falls back to the site placeholder when requested", %{plug_opts: plug_opts} do
+ expect(
+ Plausible.HTTPClient.Mock,
+ :get,
+ fn "https://icons.duckduckgo.com/ip3/plausible.io.ico" ->
+ {:error, %Finch.TransportError{reason: :closed}}
+ end
+ )
+
+ conn =
+ conn(:get, "/favicon/sources/plausible.io?placeholder=site")
+ |> Favicon.call(plug_opts)
+
+ assert conn.halted
+ assert conn.status == 200
+ assert conn.resp_body == @site_placeholder_icon
+ end
+
+ test "falls back to the dark site placeholder when requested", %{plug_opts: plug_opts} do
+ expect(
+ Plausible.HTTPClient.Mock,
+ :get,
+ fn "https://icons.duckduckgo.com/ip3/plausible.io.ico" ->
+ {:error, %Finch.TransportError{reason: :closed}}
+ end
+ )
+
+ conn =
+ conn(:get, "/favicon/sources/plausible.io?placeholder=site&ui-mode=dark")
+ |> Favicon.call(plug_opts)
+
+ assert conn.halted
+ assert conn.status == 200
+ assert conn.resp_body == @site_dark_placeholder_icon
+ end
+
+ test "serves the site placeholder on its own URL", %{plug_opts: plug_opts} do
+ conn =
+ conn(:get, "/favicon/placeholders/site")
+ |> Favicon.call(plug_opts)
+
+ assert conn.halted
+ assert conn.status == 200
+ assert conn.resp_body == @site_placeholder_icon
+ end
+
+ test "serves the dark site placeholder on its own URL", %{plug_opts: plug_opts} do
+ conn =
+ conn(:get, "/favicon/placeholders/site?ui-mode=dark")
+ |> Favicon.call(plug_opts)
+
+ assert conn.halted
+ assert conn.status == 200
+ assert conn.resp_body == @site_dark_placeholder_icon
+ end
+
+ test "serves the source placeholder on its own URL", %{plug_opts: plug_opts} do
+ conn =
+ conn(:get, "/favicon/placeholders/source")
+ |> Favicon.call(plug_opts)
+
+ assert conn.halted
+ assert conn.status == 200
+ assert conn.resp_body == @source_placeholder_icon
+ end
+
+ test "serves a favicon for a source named placeholder", %{plug_opts: plug_opts} do
+ expect(
+ Plausible.HTTPClient.Mock,
+ :get,
+ fn "https://icons.duckduckgo.com/ip3/placeholder.ico" ->
+ {:ok, %Finch.Response{status: 200, body: "favicon", headers: []}}
+ end
+ )
+
+ conn =
+ conn(:get, "/favicon/sources/placeholder")
+ |> Favicon.call(plug_opts)
+
+ assert conn.halted
+ assert conn.status == 200
+ assert conn.resp_body == "favicon"
end
end
end