Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

feat: implement favicon fetching API and update Link model for favicon support

daniel31x13703f84403e59
diff --git a/apps/web/components/LinkViews/LinkComponents/LinkIcon.tsx b/apps/web/components/LinkViews/LinkComponents/LinkIcon.tsx
index ce2f965f..fa0e30b2 100644
--- a/apps/web/components/LinkViews/LinkComponents/LinkIcon.tsx
+++ b/apps/web/components/LinkViews/LinkComponents/LinkIcon.tsx
@@ -45,17 +45,17 @@ function LinkIcon({
) : link.type === "url" && url ? (
<>
<Image
- src={`https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${link.url}&size=64`}
+ src={`/api/v1/getFavicon?url=${encodeURIComponent(url.origin)}`}
width={64}
height={64}
alt=""
+ unoptimized
className={clsx(
iconClasses,
faviconLoaded ? "" : "absolute opacity-0"
)}
draggable="false"
onLoad={() => setFaviconLoaded(true)}
- onError={() => setFaviconLoaded(false)}
/>
{!faviconLoaded && (
<LinkPlaceholderIcon
diff --git a/apps/web/next.config.js b/apps/web/next.config.js
index b1e186e2..728a7af2 100644
--- a/apps/web/next.config.js
+++ b/apps/web/next.config.js
@@ -8,9 +8,6 @@ const nextConfig = {
staticPageGenerationTimeout: 1000,
images: {
remotePatterns: [
- // For fetching the favicons
- { hostname: "t2.gstatic.com" },
-
// For profile pictures (Google OAuth)
{ hostname: "*.googleusercontent.com" },
],
diff --git a/apps/web/pages/api/v1/getFavicon/index.ts b/apps/web/pages/api/v1/getFavicon/index.ts
new file mode 100644
index 00000000..0c39b5d0
--- /dev/null
+++ b/apps/web/pages/api/v1/getFavicon/index.ts
@@ -0,0 +1,91 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+import { Readable } from "node:stream";
+
+function isImage(ct: string | null) {
+ return !!ct && ct.toLowerCase().startsWith("image/");
+}
+
+async function fetchImage(src: string, timeoutMs = 1500) {
+ const controller = new AbortController();
+ const t = setTimeout(() => controller.abort(), timeoutMs);
+
+ try {
+ const r = await fetch(src, {
+ method: "GET",
+ headers: { Accept: "image/*" },
+ redirect: "follow",
+ signal: controller.signal,
+ });
+
+ if (!r.ok || !r.body) return null;
+
+ const ct = r.headers.get("content-type");
+ if (!isImage(ct)) return null;
+
+ return { body: r.body, contentType: ct! };
+ } catch {
+ return null;
+ } finally {
+ clearTimeout(t);
+ }
+}
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method !== "GET") return res.status(405).end();
+
+ const raw = req.query.url;
+ const urlStr = Array.isArray(raw) ? raw[0] : raw;
+ if (!urlStr) return res.status(400).end();
+
+ let u: URL;
+ try {
+ u = new URL(decodeURIComponent(urlStr));
+ } catch {
+ return res.status(204).end();
+ }
+
+ if (u.protocol !== "http:" && u.protocol !== "https:") {
+ return res.status(204).end();
+ }
+
+ const origin = u.origin;
+ const hostname = u.hostname;
+
+ const canonical = `/api/v1/getFavicon?url=${encodeURIComponent(origin)}`;
+ if (req.url !== canonical) {
+ res.setHeader("Cache-Control", "public, max-age=3600");
+ return res.redirect(308, canonical);
+ }
+
+ const sources = [
+ `https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${encodeURIComponent(
+ origin
+ )}&size=64`,
+ `https://icons.duckduckgo.com/ip3/${hostname}.ico`,
+ ];
+
+ for (const src of sources) {
+ const hit = await fetchImage(src);
+ if (!hit) continue;
+
+ res.status(200);
+ res.setHeader("Content-Type", hit.contentType);
+ res.setHeader(
+ "Cache-Control",
+ "public, max-age=86400, s-maxage=2592000, stale-while-revalidate=604800, immutable"
+ );
+
+ Readable.fromWeb(hit.body as any).pipe(res);
+ return;
+ }
+
+ res.status(204);
+ res.setHeader(
+ "Cache-Control",
+ "public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800"
+ );
+ return res.end();
+}
diff --git a/apps/worker/worker.ts b/apps/worker/worker.ts
index 32c6c678..cffe65a4 100644
--- a/apps/worker/worker.ts
+++ b/apps/worker/worker.ts
@@ -1,5 +1,6 @@
import { startIndexing } from "./workers/linkIndexing";
import { linkProcessing } from "./workers/linkProcessing";
+import { migrationWorker } from "./workers/migrationWorker";
import { startRSSPolling } from "./workers/rssPolling";
import { trialEndEmailWorker } from "./workers/trialEndEmailWorker";
@@ -7,6 +8,8 @@ const workerIntervalInSeconds =
Number(process.env.ARCHIVE_SCRIPT_INTERVAL) || 10;
async function init() {
+ await migrationWorker();
+
console.log("\x1b[34m%s\x1b[0m", "Initializing the worker...");
startRSSPolling();
linkProcessing(workerIntervalInSeconds);
diff --git a/apps/worker/workers/migrationWorker.ts b/apps/worker/workers/migrationWorker.ts
new file mode 100644
index 00000000..6e227495
--- /dev/null
+++ b/apps/worker/workers/migrationWorker.ts
@@ -0,0 +1,80 @@
+import { prisma } from "@linkwarden/prisma";
+import { AppMigrationStatus } from "@linkwarden/prisma/client";
+
+export async function migrationWorker() {
+ console.log("\x1b[34m%s\x1b[0m", "Checking for migrations...");
+
+ // go through all the migrations one by one in order, first see where it needs to start
+ try {
+ const dbMigrations = await prisma.appMigration.findMany();
+
+ const statusByName = new Map(dbMigrations.map((m) => [m.name, m.status]));
+
+ // sort by id
+ const ordered = [...migrations].sort((a, b) => a.id - b.id);
+
+ // find the first migration that's not APPLIED
+ const firstIdx = ordered.findIndex(
+ (m) => statusByName.get(m.name) !== AppMigrationStatus.APPLIED
+ );
+
+ if (firstIdx === -1) {
+ // console.log("\x1b[32m%s\x1b[0m", "No pending migrations."); // Uncomment later
+ return;
+ }
+
+ for (let i = firstIdx; i < ordered.length; i++) {
+ const m = ordered[i];
+ const status = statusByName.get(m.name);
+
+ if (status === AppMigrationStatus.APPLIED) continue;
+
+ console.log("\x1b[34m%s\x1b[0m", `Applying ${m.name}...`);
+
+ await prisma.appMigration.upsert({
+ where: { name: m.name },
+ create: { name: m.name, status: AppMigrationStatus.PENDING },
+ update: { status: AppMigrationStatus.PENDING, finishedAt: null },
+ });
+
+ try {
+ await m.run();
+
+ await prisma.appMigration.update({
+ where: { name: m.name },
+ data: { status: AppMigrationStatus.APPLIED, finishedAt: new Date() },
+ });
+
+ statusByName.set(m.name, AppMigrationStatus.APPLIED);
+ console.log("\x1b[32m%s\x1b[0m", `Applied ${m.name}`);
+ } catch (err) {
+ await prisma.appMigration.update({
+ where: { name: m.name },
+ data: { status: AppMigrationStatus.FAILED, finishedAt: new Date() },
+ });
+
+ console.error("\x1b[31m%s\x1b[0m", `FAILED ${m.name}`);
+ throw err;
+ }
+ }
+
+ console.log("\x1b[32m%s\x1b[0m", "All migrations applied.");
+ } catch (e) {
+ throw e;
+ }
+}
+
+type AppMigrationDef = {
+ id: number;
+ name: string;
+ run: () => Promise<void>;
+};
+
+const migrations: AppMigrationDef[] = [
+ // {
+ // id: 1,
+ // name: "0001_first_migration",
+ // run: async () => {},
+ // },
+ // to create a new `AppMigrationDef`, make sure to have the `id` and the `name` field to be unique (incremental id)
+];
diff --git a/packages/lib/generatePreview.ts b/packages/lib/generatePreview.ts
index 2bf16ff8..26e37cdc 100644
--- a/packages/lib/generatePreview.ts
+++ b/packages/lib/generatePreview.ts
@@ -24,7 +24,7 @@ export const generatePreview = async (
1024 * 1024 * Number(process.env.PREVIEW_MAX_BUFFER || 10)
) {
console.log("Error generating preview: Buffer size exceeded");
- prisma.link.update({
+ await prisma.link.update({
where: { id: linkId },
data: {
preview: "unavailable",
diff --git a/packages/prisma/migrations/20251216062639_add_favicon_field_to_link_model/migration.sql b/packages/prisma/migrations/20251216062639_add_favicon_field_to_link_model/migration.sql
deleted file mode 100644
index d30b2679..00000000
--- a/packages/prisma/migrations/20251216062639_add_favicon_field_to_link_model/migration.sql
+++ /dev/null
@@ -1,2 +0,0 @@
--- AlterTable
-ALTER TABLE "Link" ADD COLUMN "favicon" TEXT;
diff --git a/packages/prisma/migrations/20251216090848_add_app_migration_table/migration.sql b/packages/prisma/migrations/20251216090848_add_app_migration_table/migration.sql
new file mode 100644
index 00000000..7778bd31
--- /dev/null
+++ b/packages/prisma/migrations/20251216090848_add_app_migration_table/migration.sql
@@ -0,0 +1,16 @@
+-- CreateEnum
+CREATE TYPE "AppMigrationStatus" AS ENUM ('APPLIED', 'PENDING', 'FAILED');
+
+-- CreateTable
+CREATE TABLE "AppMigration" (
+ "id" SERIAL NOT NULL,
+ "name" TEXT NOT NULL,
+ "status" "AppMigrationStatus" NOT NULL,
+ "finishedAt" TIMESTAMP(3),
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+
+ CONSTRAINT "AppMigration_pkey" PRIMARY KEY ("id")
+);
+
+-- CreateIndex
+CREATE UNIQUE INDEX "AppMigration_name_key" ON "AppMigration"("name");
diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma
index 1f69b676..b9959914 100644
--- a/packages/prisma/schema.prisma
+++ b/packages/prisma/schema.prisma
@@ -180,7 +180,6 @@ model Link {
color String?
url String?
textContent String?
- favicon String?
preview String?
image String?
pdf String?
@@ -291,4 +290,18 @@ enum DashboardSectionType {
RECENT_LINKS
PINNED_LINKS
COLLECTION
-}
\ No newline at end of file
+}
+
+model AppMigration {
+ id Int @id @default(autoincrement())
+ name String @unique
+ status AppMigrationStatus
+ finishedAt DateTime?
+ createdAt DateTime @default(now())
+}
+
+enum AppMigrationStatus {
+ APPLIED
+ PENDING
+ FAILED
+}