gitbook中的book.json设置——favicon详解

浏览器标签栏的favicon图标,有良好的用户体验和页面美观的作用,然而怎样设置这样的favicon图标呢?对于不同的浏览器,会有一定的差异,一般而言有下列方法:

在gitbook中配置book.json

{
    "plugins": [
        "favicon"
    ],
    "pluginsConfig": {
        "favicon": {
            "shortcut": "assets/images/favicon.ico",
            "bookmark": "assets/images/favicon.ico",
            "appleTouch": "assets/images/apple-touch-icon.png",
            "appleTouchMore": {
                "120x120": "assets/images/apple-touch-icon-120x120.png",
                "180x180": "assets/images/apple-touch-icon-180x180.png"
            }
        }
    }
}

你一定会好奇,为什么favicon有这么多的设置?

首先,shortcut通常可以被所有可以显示favicon的浏览器读取。

bookmark在收藏夹中显示自己的图标。

apple-touch-icon是一个类似网站favicon的图标文件,用来在iphone和iPad上创建快捷键时使用。apple-touch-icon这个文件应当

是png格式,默认:57x57像素大小。如果准备的文件不是57x57的话,它会自己缩放的。

 

 

 

可以使用`embed.FS`提供的`ReadFile`函数读取压缩文件,并使用`gzip`包对文件内容进行解压缩。以下是一个示例代码: ```go package main import ( "embed" "compress/gzip" "io/ioutil" "net/http" "github.com/gin-gonic/gin" ) //go:embed index.html.gz favicon.ico.gz assets static serverConfig.json.gz logo.svg.gz var Static embed.FS func main() { r := gin.Default() // Serve index.html htmlData, err := Static.ReadFile("index.html.gz") if err != nil { panic(err) } r.GET("/", func(c *gin.Context) { c.Header("Content-Encoding", "gzip") c.Data(http.StatusOK, "text/html", htmlData) }) // Serve favicon.ico icoData, err := Static.ReadFile("favicon.ico.gz") if err != nil { panic(err) } r.GET("/favicon.ico", func(c *gin.Context) { c.Header("Content-Encoding", "gzip") c.Data(http.StatusOK, "image/x-icon", icoData) }) // Serve assets r.StaticFS("/assets", http.FS(Static.Dir("assets"))) // Serve static r.StaticFS("/static", http.FS(Static.Dir("static"))) // Serve serverConfig.json configData, err := Static.ReadFile("serverConfig.json.gz") if err != nil { panic(err) } r.GET("/serverConfig.json", func(c *gin.Context) { c.Header("Content-Encoding", "gzip") c.Data(http.StatusOK, "application/json", configData) }) // Serve logo.svg logoData, err := Static.ReadFile("logo.svg.gz") if err != nil { panic(err) } r.GET("/logo.svg", func(c *gin.Context) { c.Header("Content-Encoding", "gzip") c.Data(http.StatusOK, "image/svg+xml", logoData) }) // Run server r.Run(":8080") } ``` 在上面的代码,我们使用`embed.FS`嵌入了多个压缩文件,并在不同的路由提供了这些文件的访问。对于每个压缩文件,我们使用`ReadFile`函数读取文件内容,然后使用`gzip`包对其进行解压缩,并将解压缩后的内容返回给客户端。注意要在响应头设置`Content-Encoding`为`gzip`,以告诉浏览器这是一个经过gzip压缩的响应。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值