Hi,
I have a homepage with some links to other homepages, and I want to display the favicon to each page. Is there some way to get it?
Thanks!

Dani AI

Generated

Good question, — and thanks for the initial pointers. Practical, reliable favicon display for links usually means implementing a small server-side fetch-and-cache flow so you can (a) discover the icon a site advertises, (b) fall back to common defaults when none are declared, and (c) serve a normalized image to your pages to avoid CORS and mixed-content problems.

A minimal Node/Express example (very small, synchronous-style for clarity) that fetches a target page, looks for an icon link, falls back to a conventional path, and proxies the result:

const express = require('express');
const fetch = require('node-fetch');
const app = express();

app.get('/favicon-proxy', async (req, res) => {
  const target = req.query.url;
  if (!target) return res.status(400).send('missing url');
  const html = await fetch(target).then(r => r.text());
  const m = html.match(/<link[^>]+rel=["'](?:icon|shortcut icon)["'][^>]*href=["']([^"']+)["']/i);
  const iconUrl = m ? new URL(m[1], target).href : new URL('/favicon.ico', target).href;
  const iconResp = await fetch(iconUrl);
  res.set('Content-Type', iconResp.headers.get('content-type') || 'image/x-icon');
  iconResp.body.pipe(res);
});

Notes: add caching (memory or Redis) and TTLs, rate-limit requests, resize/convert icons to a consistent size if needed, and handle HTTPS/http mismatches. For background reading on icon declarations and best practices see the MDN favicon overview: Favicon - MDN.

Recommended Answers

All 2 Replies

In most cases there would be a file in the root of the website called favicon.ico If it is not there, you can check the html header for the meta link tag "shortcut icon".

Yes, I think I have to make a script that searches for the file.
Thanks ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.