feat: favicon api always return an image

When the api cannot fetch the remote favicon, we return a placeholder image
This commit is contained in:
Sonny
2023-11-12 04:34:30 +01:00
parent 7387b4f364
commit f5c406a84c
5 changed files with 13 additions and 3 deletions

View File

@@ -23,7 +23,10 @@ const config = {
experimental: {
webpackBuildWorker: true
},
output: "standalone"
output: "standalone",
env: {
baseUrl: process.env.NEXTAUTH_URL
}
};
module.exports = config;

BIN
public/empty-image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 596 B

View File

@@ -13,6 +13,8 @@ interface LinkFaviconProps {
noMargin?: boolean;
}
// The Favicon API should always return an image, so it's not really useful to keep the loader nor placeholder icon,
// but for slow connections and other random stuff, I'll keep this
export default function LinkFavicon({
url,
size = 32,

View File

@@ -1,5 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import { parse } from "node-html-parser";
import { createReadStream } from "node:fs";
import { resolve } from "node:path";
const USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0";
@@ -62,7 +64,10 @@ export default async function handler(
} catch (error) {
const errorMessage = error?.message || "Unable to retrieve favicon";
console.log("[Favicon]", `[second: ${faviconRequestUrl}]`, errorMessage);
res.status(404).send({ error: errorMessage, text: documentAsText });
const readStream = createReadStream(resolve(process.cwd(), "./public/empty-image.png"));
res.writeHead(206);
readStream.pipe(res);
}
}

View File

@@ -1,3 +1,3 @@
export function faviconLinkBuilder(origin: string) {
return `http://localhost:3000/api/favicon?url=${origin}`;
return `${process.env.baseUrl}/api/favicon?url=${origin}`;
}