Skip to content

Commit

Permalink
add favicon support
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Jun 14, 2024
1 parent 74d86ef commit 14c2092
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
39 changes: 27 additions & 12 deletions js.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func JawsKeyValue(jawsKey string) uint64 {
// PreloadHTML returns HTML code to load the given resources efficiently.
func PreloadHTML(urls ...*url.URL) string {
var jsurls, cssurls []string
var faviconurl, favicontype string
var buf []byte
for _, u := range urls {
var asattr string
Expand All @@ -86,24 +87,30 @@ func PreloadHTML(urls ...*url.URL) string {
default:
if strings.HasPrefix(mimetype, "image") {
asattr = "image"
if strings.HasSuffix(strings.TrimSuffix(u.Path, ext), "favicon") {
favicontype = mimetype
faviconurl = urlstr
}
} else if strings.HasPrefix(mimetype, "font") {
asattr = "font"
}
}
buf = append(buf, `<link rel="preload" href="`...)
buf = append(buf, urlstr...)
buf = append(buf, '"')
if asattr != "" {
buf = append(buf, ` as="`...)
buf = append(buf, asattr...)
buf = append(buf, '"')
}
if mimetype != "" {
buf = append(buf, ` type="`...)
buf = append(buf, mimetype...)
if urlstr != faviconurl {
buf = append(buf, `<link rel="preload" href="`...)
buf = append(buf, urlstr...)
buf = append(buf, '"')
if asattr != "" {
buf = append(buf, ` as="`...)
buf = append(buf, asattr...)
buf = append(buf, '"')
}
if mimetype != "" {
buf = append(buf, ` type="`...)
buf = append(buf, mimetype...)
buf = append(buf, '"')
}
buf = append(buf, ">\n"...)
}
buf = append(buf, ">\n"...)
}

if len(jsurls) > 0 {
Expand All @@ -125,5 +132,13 @@ func PreloadHTML(urls ...*url.URL) string {
buf = append(buf, "\">\n"...)
}

if faviconurl != "" {
buf = append(buf, `<link rel="icon" type="`...)
buf = append(buf, favicontype...)
buf = append(buf, `" href="`...)
buf = append(buf, faviconurl...)
buf = append(buf, "\">\n"...)
}

return string(buf)
}
2 changes: 1 addition & 1 deletion js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_Javascript(t *testing.T) {
func Test_PreloadHTML(t *testing.T) {
const extraScript = "someExtraScript.js"
const extraStyle = "someExtraStyle.css"
const extraImage = "someExtraImage.png"
const extraImage = "favicon.png"
const extraFont = "someExtraFont.woff2"
th := newTestHelper(t)

Expand Down

0 comments on commit 14c2092

Please sign in to comment.