Skip to content

Commit 7100ec7

Browse files
committed
prefer page icons over manifest
1 parent 3741c17 commit 7100ec7

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

worker/src/url-metadata/fetch-page-icon.ts

+52-41
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,69 @@ import { fetchAsBrowser } from "./fetch-page-html";
66
import { isElement } from "./is-element";
77

88
export async function fetchPageIcon(src: URL, srcHast: Root): Promise<URL> {
9-
// <link rel="manifest" href="/manifest.json">
10-
const manifestPath: Element | undefined = find(srcHast, (node) => {
11-
return isElement(node) && node.tagName === "link" && String(node.properties.rel).includes("manifest");
12-
});
13-
149
let iconHref: URL | undefined;
1510

16-
if (manifestPath?.properties?.href) {
17-
console.log("Found manifest", manifestPath.properties.href, "for", src.href);
11+
// <link rel="shortcut icon" type="image/png" href="https://example.com/img.png">
12+
// <link rel="apple-touch-icon" sizes="96x96" href="/icons/icon-96x96.png?v=2a">
13+
const iconExtensions = [".svg", ".png", ".jpg", ".jpeg"];
14+
const favicon: Element | undefined = find(srcHast, (node) => {
15+
if (!isElement(node) || node.tagName !== "link") return false;
1816

19-
// `/manifest.json`
20-
const manifestRelativeURL = String(manifestPath.properties.href);
21-
const fullManifestURL = new URL(manifestRelativeURL, src);
17+
const rel = (node as Element).properties?.rel?.toString() ?? "";
18+
const href = (node as Element).properties?.href?.toString() ?? "";
19+
let hrefUrl: URL | undefined;
20+
try {
21+
// Need to check the file extension through URL.pathname to omit query params
22+
hrefUrl = new URL(href, src);
23+
} catch (e) {
24+
// do nothing
25+
}
2226

23-
const manifest = await fetchAsBrowser(fullManifestURL)
24-
.then((r) => r.json())
25-
.catch(() => null);
27+
const extname = path.extname(hrefUrl?.pathname ?? "");
28+
return rel.includes("icon") && iconExtensions.includes(extname);
29+
});
2630

27-
if (manifest) {
28-
const largestIcon = getLargestManifestIcon(manifest);
29-
if (largestIcon?.icon) {
30-
console.log("Found iconHref from manifest.json:", largestIcon.icon.src);
31-
iconHref = new URL(largestIcon.icon.src, src.origin);
32-
}
33-
}
31+
if (favicon?.properties?.href) {
32+
console.log("Found iconHref from <link> tags:", favicon.properties.href);
33+
iconHref = new URL(favicon.properties.href.toString(), src);
3434
}
3535

3636
if (!iconHref) {
37-
// <link rel="shortcut icon" type="image/png" href="https://example.com/img.png">
38-
// <link rel="apple-touch-icon" sizes="96x96" href="/icons/icon-96x96.png?v=2a">
39-
const iconExtensions = [".svg", ".png", ".jpg", ".jpeg"];
40-
const favicon: Element | undefined = find(srcHast, (node) => {
41-
if (!isElement(node) || node.tagName !== "link")
42-
return false;
37+
// <link rel="manifest" href="/manifest.json">
38+
const manifestPath: Element | undefined = find(srcHast, (node) => {
39+
return (
40+
isElement(node) &&
41+
node.tagName === "link" &&
42+
String(node.properties.rel).includes("manifest")
43+
);
44+
});
4345

44-
const rel = (node as Element).properties?.rel?.toString() ?? "";
45-
const href = (node as Element).properties?.href?.toString() ?? "";
46-
let hrefUrl: URL|undefined;
47-
try {
48-
// Need to check the file extension through URL.pathname to omit query params
49-
hrefUrl = new URL(href, src);
50-
} catch (e) {
51-
// do nothing
52-
}
46+
if (manifestPath?.properties?.href) {
47+
console.log(
48+
"Found manifest",
49+
manifestPath.properties.href,
50+
"for",
51+
src.href,
52+
);
5353

54-
const extname = path.extname(hrefUrl?.pathname ?? "");
55-
return rel.includes("icon") && iconExtensions.includes(extname);
56-
});
54+
// `/manifest.json`
55+
const manifestRelativeURL = String(manifestPath.properties.href);
56+
const fullManifestURL = new URL(manifestRelativeURL, src);
5757

58-
if (favicon?.properties?.href) {
59-
console.log("Found iconHref from <link> tags:", favicon.properties.href);
60-
iconHref = new URL(favicon.properties.href.toString(), src);
58+
const manifest = await fetchAsBrowser(fullManifestURL)
59+
.then((r) => r.json())
60+
.catch(() => null);
61+
62+
if (manifest) {
63+
const largestIcon = getLargestManifestIcon(manifest);
64+
if (largestIcon?.icon) {
65+
console.log(
66+
"Found iconHref from manifest.json:",
67+
largestIcon.icon.src,
68+
);
69+
iconHref = new URL(largestIcon.icon.src, src.origin);
70+
}
71+
}
6172
}
6273
}
6374

worker/src/url-metadata/image-to-s3.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export async function imageToS3(
2626
const uploadKey = `${key}-${urlHash}.svg`;
2727

2828
if (await exists(bucket, uploadKey)) {
29+
console.log(`Using existing object for ${uploadKey}`);
2930
return uploadKey;
3031
} else {
3132
await upload(bucket, uploadKey, tags, stream.Readable.from([optimizedSvg]));
@@ -47,6 +48,7 @@ export async function imageToS3(
4748

4849
const uploadKey = `${key}-${urlHash}.${extension}`;
4950
if (await exists(bucket, uploadKey)) {
51+
console.log(`Using existing object for ${uploadKey}`);
5052
return uploadKey;
5153
} else {
5254
await upload(bucket, uploadKey, tags, transformerStream);

0 commit comments

Comments
 (0)