Commit 6ef4f0ceb7

6ef4f0ceb78f4489e94b10943defd4480f71e744

parent: 61fd569e7a

Unsigned

Alicia Sykes <alicia@omg.lol> · 2026-03-28 11:15 UTC

Handle missing icons client-side

Layout: unified · split

web/src/components/things/ListingHero.astro +3 −11
@@ -4,6 +4,7 @@ import { slugify } from '@utils/fetch-data';
44import { parseMarkdown, formatLink } from '@utils/parse-markdown';
55import FontAwesome from '@components/form/FontAwesome.svelte';
66import SaveListing from '@components/things/SaveListing.svelte';
7import ServiceIcon from '@components/things/ServiceIcon.astro';
78
89interface Props {
910 service: Service;
@@ -143,16 +144,7 @@ const highlights = [
143144 />
144145 </div>
145146 <div class="intro">
146 <img
147 width="60"
148 height="60"
149 loading="lazy"
150 decoding="async"
151 class="service-icon"
152 alt={`${name} Icon`}
153 data-service-url={formatLink(url)}
154 src={icon || `https://icon.horse/icon/${formatLink(url)}`}
155 />
147 <ServiceIcon name={name} url={url} icon={icon} size={60} />
156148 <div>
157149 <p class="description" set:html={parseMarkdown(description)} />
158150 <ul>
@@ -267,7 +259,7 @@ const highlights = [
267259 @media (max-width: 768px) {
268260 flex-direction: column;
269261 }
270 .service-icon {
262 :global(.service-icon-wrap) {
271263 border-radius: var(--curve-md);
272264 margin-top: var(--space-md);
273265 @media (max-width: 768px) {
web/src/components/things/ServiceCard.astro +2 −31
@@ -3,6 +3,7 @@ import { formatLink } from '@utils/parse-markdown';
33import type { Service } from 'src/types/Service';
44import FontAwesome from '@components/form/FontAwesome.svelte';
55import SaveListing from '@components/things/SaveListing.svelte';
6import ServiceIcon from '@components/things/ServiceIcon.astro';
67import { slugify } from '@utils/fetch-data';
78
89interface Props {
@@ -14,27 +15,6 @@ interface Props {
1415const { service, sectionName, categoryName } = Astro.props;
1516---
1617
17<script>
18 document.addEventListener('DOMContentLoaded', () => {
19 const serviceIcons =
20 document.querySelectorAll<HTMLImageElement>('.service-icon');
21 const broke = '/broken-image.png';
22
23 serviceIcons.forEach(function (icon) {
24 icon.onerror = function () {
25 const imgElement = this as HTMLImageElement;
26 const serviceUrl = imgElement.getAttribute('data-service-url');
27 const newSrcAttribute = imgElement.src.includes('on.ho')
28 ? broke
29 : `https://icon.horse/icon/${serviceUrl}`;
30 imgElement.src =
31 imgElement.src !== newSrcAttribute ? newSrcAttribute : broke;
32 imgElement.onerror = null;
33 };
34 });
35 });
36</script>
37
3818<div class="service" id={slugify(service.name)}>
3919 <div class="service-head">
4020 <a
@@ -56,16 +36,7 @@ const { service, sectionName, categoryName } = Astro.props;
5636 </div>
5737
5838 <div class="service-body">
59 <img
60 width="40"
61 height="40"
62 loading="lazy"
63 decoding="async"
64 class="service-icon"
65 alt={`${service.name} Icon`}
66 data-service-url={formatLink(service.url)}
67 src={service.icon || `https://icon.horse/icon/${formatLink(service.url)}`}
68 />
39 <ServiceIcon name={service.name} url={service.url} icon={service.icon} />
6940 <div class="service-body">
7041 <p set:html={service.description} />
7142 </div>
web/src/components/things/ServiceIcon.astro added +128
@@ -0,0 +1,128 @@
1---
2import { formatLink } from '@utils/parse-markdown';
3
4interface Props {
5 name: string;
6 url: string;
7 icon?: string;
8 size?: number;
9}
10
11const { name, url, icon, size = 40 } = Astro.props;
12const src = icon || `https://icon.horse/icon/${formatLink(url)}`;
13const horseSrc = `https://icon.horse/icon/${formatLink(url)}`;
14const iconSize = Math.round(size * 0.45);
15---
16
17<span class="service-icon-wrap" style={`width:${size}px;height:${size}px`}>
18 <svg
19 class="icon-state loading"
20 xmlns="http://www.w3.org/2000/svg"
21 viewBox="0 0 640 640"
22 >
23 <path
24 d="M320.3 93.7C322.8 76.2 339 64.1 356.5 66.6C480.6 84.4 576 191 576 320C576 353.5 569.5 385.6 557.8 415.1C551.2 431.5 532.6 439.5 516.2 432.9C499.8 426.3 491.8 407.7 498.4 391.3C507.2 369.3 512.1 345.3 512.1 320C512.1 223.3 440.6 143.3 347.6 129.9C330.1 127.4 318 111.2 320.5 93.7z"
25 ></path>
26 </svg>
27 <svg
28 class="icon-state broken"
29 xmlns="http://www.w3.org/2000/svg"
30 width={iconSize}
31 height={iconSize}
32 viewBox="0 0 320 512"
33 >
34 <path
35 fill="currentColor"
36 d="M80 160c0-35.3 28.7-64 64-64h32c35.3 0 64 28.7 64 64v3.6c0 21.8-11.1 42.1-29.4 53.8l-42.2 27.1A87.98 87.98 0 0 0 128 320v16c0 13.3 10.7 24 24 24s24-10.7 24-24v-16c0-17.4 8.8-33.7 23.5-43.1l42.2-27.1A111.97 111.97 0 0 0 288 163.6V160c0-61.9-50.1-112-112-112h-32C82.1 48 32 98.1 32 160c0 13.3 10.7 24 24 24s24-10.7 24-24zm80 288a32 32 0 1 0 0-64 32 32 0 1 0 0 64z"
37 ></path>
38 </svg>
39 <img
40 width={size}
41 height={size}
42 loading="lazy"
43 decoding="async"
44 class="service-icon"
45 alt={name}
46 data-horse={horseSrc}
47 src={src}
48 onload="this.parentNode.classList.add('loaded')"
49 onerror={`if(!this.src.includes('icon.horse')){this.src=this.dataset.horse}else{this.remove();this.parentNode?.classList.add('broken')}`}
50 />
51</span>
52
53<script>
54 document.addEventListener('astro:page-load', () => {
55 const obs = new IntersectionObserver((entries) => {
56 entries.forEach((e) => {
57 if (!e.isIntersecting) return;
58 obs.unobserve(e.target);
59 setTimeout(() => {
60 const wrap = e.target as HTMLElement;
61 if (
62 wrap.classList.contains('loaded') ||
63 wrap.classList.contains('broken')
64 )
65 return;
66 const img = wrap.querySelector('img');
67 if (img) img.remove();
68 wrap.classList.add('broken');
69 }, 8000);
70 });
71 });
72 document
73 .querySelectorAll('.service-icon-wrap')
74 .forEach((el) => obs.observe(el));
75 });
76</script>
77
78<style is:global>
79 .service-icon-wrap {
80 display: inline-flex;
81 align-items: center;
82 justify-content: center;
83 flex-shrink: 0;
84 border-radius: var(--curve-sm);
85 position: relative;
86 &:not(.loaded) {
87 border: var(--border-heavy);
88 box-shadow: var(--shadow-xs);
89 background: var(--accent-3-translucent);
90 }
91 .icon-state {
92 color: var(--accent-3);
93 }
94 .icon-state.broken {
95 display: none;
96 }
97 .icon-state.loading {
98 animation: spin 1s linear infinite;
99 animation-timing-function: cubic-bezier(0, 0, 0.2, 0);
100 fill: var(--accent-3);
101 opacity: 0.5;
102 }
103 .service-icon {
104 position: absolute;
105 inset: 0;
106 border-radius: var(--curve-sm);
107 font-size: 0;
108 }
109 &.loaded {
110 .icon-state {
111 display: none;
112 }
113 }
114 &.broken {
115 .icon-state.loading {
116 display: none;
117 }
118 .icon-state.broken {
119 display: block;
120 }
121 }
122 }
123 @keyframes spin {
124 to {
125 transform: rotate(360deg);
126 }
127 }
128</style>
web/src/components/things/ServiceList.astro +6 −36
@@ -4,6 +4,7 @@ import { parseMarkdown, formatLink } from '@utils/parse-markdown';
44import type { Service } from 'src/types/Service';
55import FontAwesome from '@components/form/FontAwesome.svelte';
66import DeleteListing from '@components/things/DeleteListing.svelte';
7import ServiceIcon from '@components/things/ServiceIcon.astro';
78import { slugify } from '@utils/fetch-data';
89
910import GitHubMetrics from '@components/things/ItemGitHubMetrics.astro';
@@ -28,27 +29,6 @@ const {
2829} = Astro.props;
2930---
3031
31<script>
32 document.addEventListener('DOMContentLoaded', () => {
33 const serviceIcons =
34 document.querySelectorAll<HTMLImageElement>('.service-icon');
35 const broke = '/broken-image.png';
36
37 serviceIcons.forEach(function (icon) {
38 icon.onerror = function () {
39 const imgElement = this as HTMLImageElement;
40 const serviceUrl = imgElement.getAttribute('data-service-url');
41 const newSrcAttribute = imgElement.src.includes('on.ho')
42 ? broke
43 : `https://icon.horse/icon/${serviceUrl}`;
44 imgElement.src =
45 imgElement.src !== newSrcAttribute ? newSrcAttribute : broke;
46 imgElement.onerror = null;
47 };
48 });
49 });
50</script>
51
5232<section>
5333 {
5434 services && services.length > 0 ? (
@@ -70,18 +50,10 @@ const {
7050 />
7151 </div>
7252 <div class="service-head">
73 <img
74 width="40"
75 height="40"
76 loading="lazy"
77 decoding="async"
78 class="service-icon"
79 alt={`${service.name} Icon`}
80 data-service-url={formatLink(service.url)}
81 src={
82 service.icon ||
83 `https://icon.horse/icon/${formatLink(service.url)}`
84 }
53 <ServiceIcon
54 name={service.name}
55 url={service.url}
56 icon={service.icon}
8557 />
8658
8759 <a
@@ -251,12 +223,10 @@ const {
251223 }
252224 }
253225
254 .service-icon {
226 :global(.service-icon-wrap) {
255227 width: 2.5rem;
256228 height: 2.5rem;
257229 border-radius: var(--curve-sm);
258
259 font-size: 10px;
260230 overflow: hidden;
261231 color: var(--accent);
262232 }
web/src/components/things/service-card.scss −5
@@ -42,11 +42,6 @@
4242 display: flex;
4343 align-items: center;
4444 gap: var(--space-sm);
45 img {
46 border-radius: var(--curve-sm);
47 font-size: 0.6rem;
48 color: var(--accent);
49 }
5045 :global(p) {
5146 @include line-clamp(3);
5247 margin: var(--space-sm) 0;