Skip to content

Commit 12b811f

Browse files
Respect favicon provider setting (#53)
1 parent 0f946d5 commit 12b811f

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

docs/utils-reference/getting-started.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ npm install --save @raycast/utils
1616

1717
## Changelog
1818

19+
### v2.1.0
20+
21+
- `getFavicon` will now respect the user's setting for the favicon provider. Note that the `Apple` provider isn't supported since it relies on a native API.
22+
1923
### v2.0.1
2024

2125
- Fix types for ESM extensions

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@raycast/utils",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "Set of utilities to streamline building Raycast extensions",
55
"author": "Raycast Technologies Ltd.",
66
"homepage": "https://developers.raycast.com/utils-reference",

src/icon/favicon.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,53 @@ export function getFavicon(
3535
},
3636
): Image.ImageLike {
3737
try {
38-
const urlObj = typeof url === "string" ? new URL(url) : url;
39-
const hostname = urlObj.hostname;
40-
return {
41-
source: `https://www.google.com/s2/favicons?sz=${options?.size ?? 64}&domain=${hostname}`,
42-
fallback: options?.fallback ?? Icon.Link,
43-
mask: options?.mask,
38+
const sanitize = (url: string) => {
39+
if (!url.startsWith("http")) {
40+
return `https://${url}`;
41+
}
42+
return url;
4443
};
44+
45+
const urlObj = typeof url === "string" ? new URL(sanitize(url)) : url;
46+
const hostname = urlObj.hostname;
47+
48+
const faviconProvider: "none" | "raycast" | "apple" | "google" | "duckDuckGo" | "duckduckgo" | "legacy" =
49+
(process.env.FAVICON_PROVIDER as any) ?? "raycast";
50+
51+
switch (faviconProvider) {
52+
case "none":
53+
return {
54+
source: options?.fallback ?? Icon.Link,
55+
mask: options?.mask,
56+
};
57+
case "apple":
58+
// we can't support apple favicons as it's a native API
59+
return {
60+
source: options?.fallback ?? Icon.Link,
61+
mask: options?.mask,
62+
};
63+
case "duckduckgo":
64+
case "duckDuckGo":
65+
return {
66+
source: `https://icons.duckduckgo.com/ip3/${hostname}.ico`,
67+
fallback: options?.fallback ?? Icon.Link,
68+
mask: options?.mask,
69+
};
70+
case "google":
71+
return {
72+
source: `https://www.google.com/s2/favicons?sz=${options?.size ?? 64}&domain=${hostname}`,
73+
fallback: options?.fallback ?? Icon.Link,
74+
mask: options?.mask,
75+
};
76+
case "legacy":
77+
case "raycast":
78+
default:
79+
return {
80+
source: `https://api.ray.so/favicon?url=${hostname}&size=${options?.size}`,
81+
fallback: options?.fallback ?? Icon.Link,
82+
mask: options?.mask,
83+
};
84+
}
4585
} catch (e) {
4686
console.error(e);
4787
return Icon.Link;

0 commit comments

Comments
 (0)