Monorepo for Tangled tangled.org

appview/state: support /favicon.ico #1015

open opened by boltless.me targeting master from sl/wznxxmtqvxwk

apps like pdsls.dev expects favicon to exist in well-known path

Signed-off-by: Seongmin Lee git@boltless.me

Labels

None yet.

assignee

None yet.

Participants 3
AT URI
at://did:plc:xasnlahkri4ewmbuzly2rlc5/sh.tangled.repo.pull/3mda56w3xqo22
+30
Diff #2
+29
appview/pages/pages.go
··· 1436 1436 return Cache(http.StripPrefix("/static/", http.FileServer(http.FS(sub)))) 1437 1437 } 1438 1438 1439 + func (p *Pages) StaticFile(path string) http.Handler { 1440 + if p.dev { 1441 + filePath := filepath.Join("appview/pages/static", path) 1442 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 1443 + http.ServeFile(w, r, filePath) 1444 + }) 1445 + } 1446 + sub, err := fs.Sub(p.embedFS, "static") 1447 + if err != nil { 1448 + p.logger.Error("no static dir found? that's crazy", "err", err) 1449 + panic(err) 1450 + } 1451 + fsys := http.FS(sub) 1452 + return Cache(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 1453 + f, err := fsys.Open(path) 1454 + if err != nil { 1455 + http.NotFound(w, r) 1456 + return 1457 + } 1458 + defer f.Close() 1459 + stat, err := f.Stat() 1460 + if err != nil { 1461 + http.NotFound(w, r) 1462 + return 1463 + } 1464 + http.ServeContent(w, r, stat.Name(), stat.ModTime(), f.(io.ReadSeeker)) 1465 + })) 1466 + } 1467 + 1439 1468 func Cache(h http.Handler) http.Handler { 1440 1469 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 1441 1470 path := strings.Split(r.URL.Path, "?")[0]
+1
appview/state/router.go
··· 32 32 s.pages, 33 33 ) 34 34 35 + router.Get("/favicon.ico", s.pages.StaticFile("logos/dolly.ico").ServeHTTP) 35 36 router.Get("/pwa-manifest.json", s.WebAppManifest) 36 37 router.Get("/robots.txt", s.RobotsTxt) 37 38

History

3 rounds 7 comments
sign up or login to add to the discussion
1 commit
expand
appview/state: support /favicon.ico
3/3 success
expand
merge conflicts detected
expand
  • appview/state/router.go:32
expand 2 comments

hm, we already have page.Static, why an other handler? can we use the existing http handler and just give it a file path?

router.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
    r2 := r.Clone(r.Context())
    r2.URL.Path = "/static/logos/dolly.ico"
    s.pages.Static().ServeHTTP(w, r2)
})

maybe something like that?

I think it is better not to nest the http handlers like that. pages.StaticFile pattern can be easily used for other routes like:

	router.Get("/favicon.ico", s.pages.StaticFile("logos/dolly.ico").ServeHTTP)
	router.Get("/pwa-manifest.json", s.pages.StaticFile("manifest.webmanifest").ServeHTTP)
	router.Get("/robots.txt", s.pages.StaticFile("robots.txt").ServeHTTP)
	// ...
1 commit
expand
appview/state: support /favicon.ico
3/3 success
expand
expand 4 comments

Does it have to be a redirect? Can鈥檛 we simply serve the same file here?

oh wait, it was using redirect too, my bad.

perhaps we should include favicon.svg as well!

1 commit
expand
appview/state: support /favicon.ico
1/3 failed, 2/3 success
expand
expand 1 comment

lgtm barring CI!