Favicon not used when viewing files #8711

Closed
opened 2025-11-02 08:15:15 -06:00 by GiteaMirror · 7 comments
Owner

Originally created by @fwdekker on GitHub (Mar 16, 2022).

Gitea Version

1.15.9

Operating System

FreeBSD 13

Browser Version

Firefox 98.0

Can you reproduce the bug on the Gitea demo site?

No

Description

Cannot reproduce at try.gitea.io because it does not use a non-standard favicon. See this file on my self-hosted Gitea instance as an example.

When looking at a file directly in the browser, such as an avatar or PDF file, the default favicon is used instead of the custom favicon. At least in Firefox, the browser displays the favicon at example.com/favicon.ico when viewing a file. On HTML pages, the browser correctly uses example.com/assets/img/logo.svg.

I have configured the favicon by adding the files favicon.ico, favicon.png, logo.png, and logo.svg to $GITEA_CUSTOM/public/img/.

Screenshots

Two tabs showing different favicons for the same Gitea instance.

On the left: correct favicon is used for an HTML page.
On the right: default favicon is used for an image.

Originally created by @fwdekker on GitHub (Mar 16, 2022). ### Gitea Version 1.15.9 ### Operating System FreeBSD 13 ### Browser Version Firefox 98.0 ### Can you reproduce the bug on the Gitea demo site? No ### Description Cannot reproduce at try.gitea.io because it does not use a non-standard favicon. See [this file on my self-hosted Gitea instance as an example](https://git.fwdekker.com/avatars/63edb731af35212d057736d0ff52fe6b). When looking at a file directly in the browser, such as an avatar or PDF file, the default favicon is used instead of the custom favicon. At least in Firefox, the browser displays the favicon at `example.com/favicon.ico` when viewing a file. On HTML pages, the browser correctly uses `example.com/assets/img/logo.svg`. I have configured the favicon by adding the files `favicon.ico`, `favicon.png`, `logo.png`, and `logo.svg` to `$GITEA_CUSTOM/public/img/`. ### Screenshots ![Two tabs showing different favicons for the same Gitea instance.](https://user-images.githubusercontent.com/13442533/158605101-5d579ad1-7e00-434f-97aa-6e7198bedbaf.png) On the left: correct favicon is used for an HTML page. On the right: default favicon is used for an image.
GiteaMirror added the hacktoberfestgood first issue labels 2025-11-02 08:15:15 -06:00
Author
Owner

@silverwind commented on GitHub (Mar 16, 2022):

Favicons are now SVG, so /favicon.ico route is unused and it redirects to root. It might be possible to redirect /favicon.ico to /assets/img/logo.svg file in case the browser would accept that. Or possible just serve the SVG on that route directly.

@silverwind commented on GitHub (Mar 16, 2022): Favicons are now SVG, so `/favicon.ico` route is unused and it redirects to root. It might be possible to redirect `/favicon.ico` to `/assets/img/logo.svg` file in case the browser would accept that. Or possible just serve the SVG on that route directly.
Author
Owner

@techknowlogick commented on GitHub (Mar 16, 2022):

Probably a redirect would be better so that browsers don't hit both and cache both

@techknowlogick commented on GitHub (Mar 16, 2022): Probably a redirect would be better so that browsers don't hit both and cache both
Author
Owner

@abheekda1 commented on GitHub (Mar 17, 2022):

If this isn't already being tackled and someone is willing to give a pointer I'd be happy to take a look at fixing this issue :)

@abheekda1 commented on GitHub (Mar 17, 2022): If this isn't already being tackled and someone is willing to give a pointer I'd be happy to take a look at fixing this issue :)
Author
Owner

@silverwind commented on GitHub (Mar 18, 2022):

You can add the redirect right after 04fcf23ea3/routers/web/web.go (L97-L100):

Note that I'm not sure if browser are going to accept such a redirect on favicon, but it's worth a try and won't hurt even if it's there.

@silverwind commented on GitHub (Mar 18, 2022): You can add the redirect right after https://github.com/go-gitea/gitea/blob/04fcf23ea39a0be9e9b85a10e0cee19d8e28c35b/routers/web/web.go#L97-L100: Note that I'm not sure if browser are going to accept such a redirect on favicon, but it's worth a try and won't hurt even if it's there.
Author
Owner

@abheekda1 commented on GitHub (Mar 18, 2022):

Seems that the correct favicon is stored at /assets/img/favicon.ico so I'm going to give a redirect to that location a shot.

@abheekda1 commented on GitHub (Mar 18, 2022): Seems that the correct favicon is stored at `/assets/img/favicon.ico` so I'm going to give a redirect to that location a shot.
Author
Owner

@abheekda1 commented on GitHub (Mar 18, 2022):

@silverwind it seems that the custom favicon at that path does not exist if the default one is used, meaning that redirecting to it may result in no favicon at all. Is there a better implementation that should allow me to remedy that?

Here's my current solution:

	routes.Get("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
		faviconPath := ""
		res, err := http.Get(path.Join(setting.StaticURLPrefix, "/assets/img/favicon.ico"))
		if err != nil || res.StatusCode != 200 {
			faviconPath = "/favicon.ico"
		} else {
			faviconPath = "/assets/img/favicon.ico"
		}
		http.Redirect(w, req, path.Join(setting.StaticURLPrefix, faviconPath), 301)
	})
@abheekda1 commented on GitHub (Mar 18, 2022): @silverwind it seems that the custom favicon at that path does not exist if the default one is used, meaning that redirecting to it may result in no favicon at all. Is there a better implementation that should allow me to remedy that? Here's my current solution: ```go routes.Get("/favicon.ico", func(w http.ResponseWriter, req *http.Request) { faviconPath := "" res, err := http.Get(path.Join(setting.StaticURLPrefix, "/assets/img/favicon.ico")) if err != nil || res.StatusCode != 200 { faviconPath = "/favicon.ico" } else { faviconPath = "/assets/img/favicon.ico" } http.Redirect(w, req, path.Join(setting.StaticURLPrefix, faviconPath), 301) }) ```
Author
Owner

@fwdekker commented on GitHub (Mar 20, 2022):

Awesome! Thank you, all! :-)

@fwdekker commented on GitHub (Mar 20, 2022): Awesome! Thank you, all! :-)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#8711