feat: add caching rules for more files
This commit is contained in:
parent
4a66c2202f
commit
b3228794c7
@ -22,9 +22,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
indexFile = `index.html`
|
indexFile = `index.html`
|
||||||
rootPath = `dist/`
|
rootPath = `dist/`
|
||||||
cacheControlMax = `max-age=315360000, public, max-age=31536000, s-maxage=31536000, immutable`
|
cacheControlMax = `max-age=315360000, public, max-age=31536000, s-maxage=31536000, immutable`
|
||||||
|
cacheControlNone = `public, max-age=0, s-maxage=0, must-revalidate`
|
||||||
)
|
)
|
||||||
|
|
||||||
// Because the files are embedded into the final binary, we can be absolutely sure the etag will never change
|
// Because the files are embedded into the final binary, we can be absolutely sure the etag will never change
|
||||||
@ -132,38 +133,40 @@ func generateEtag(file http.File, name string) (etag string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// copied from http.serveContent
|
// copied from http.serveContent
|
||||||
func getMimeType(c echo.Context, name string, file http.File) (mimeType string, err error) {
|
func getMimeType(name string, file http.File) (mineType string, err error) {
|
||||||
var ctype string
|
mineType = mime.TypeByExtension(filepath.Ext(name))
|
||||||
ctype = c.Response().Header().Get("Content-Type")
|
if mineType == "" {
|
||||||
if ctype == "" {
|
// read a chunk to decide between utf-8 text and binary
|
||||||
ctype = mime.TypeByExtension(filepath.Ext(name))
|
var buf [512]byte
|
||||||
if ctype == "" {
|
n, _ := io.ReadFull(file, buf[:])
|
||||||
// read a chunk to decide between utf-8 text and binary
|
mineType = http.DetectContentType(buf[:n])
|
||||||
var buf [512]byte
|
_, err := file.Seek(0, io.SeekStart) // rewind to output whole file
|
||||||
n, _ := io.ReadFull(file, buf[:])
|
if err != nil {
|
||||||
ctype = http.DetectContentType(buf[:n])
|
return "", fmt.Errorf("seeker can't seek")
|
||||||
_, err := file.Seek(0, io.SeekStart) // rewind to output whole file
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("seeker can't seek")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctype, nil
|
return mineType, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveFile(c echo.Context, file http.File, info os.FileInfo, etag string) error {
|
func getCacheControlHeader(info os.FileInfo, file http.File) (header string, err error) {
|
||||||
|
// Don't cache service worker and related files
|
||||||
c.Response().Header().Set("Server", "Vikunja")
|
if info.Name() == "robots.txt" ||
|
||||||
c.Response().Header().Set("Vary", "Accept-Encoding")
|
info.Name() == "sw.js" ||
|
||||||
c.Response().Header().Set("Etag", etag)
|
info.Name() == "manifest.webmanifest" {
|
||||||
|
return cacheControlNone, nil
|
||||||
contentType, err := getMimeType(c, info.Name(), file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var cacheControl = "public, max-age=0, s-maxage=0, must-revalidate"
|
if strings.HasPrefix(info.Name(), "workbox-") {
|
||||||
|
return cacheControlMax, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType, err := getMimeType(info.Name(), file)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache everything looking like an asset
|
||||||
if strings.HasPrefix(contentType, "image/") ||
|
if strings.HasPrefix(contentType, "image/") ||
|
||||||
strings.HasPrefix(contentType, "font/") ||
|
strings.HasPrefix(contentType, "font/") ||
|
||||||
strings.HasPrefix(contentType, "~images/") ||
|
strings.HasPrefix(contentType, "~images/") ||
|
||||||
@ -178,9 +181,22 @@ func serveFile(c echo.Context, file http.File, info os.FileInfo, etag string) er
|
|||||||
contentType == "image/svg+xml" ||
|
contentType == "image/svg+xml" ||
|
||||||
contentType == "image/x-icon" ||
|
contentType == "image/x-icon" ||
|
||||||
contentType == "audio/wav" {
|
contentType == "audio/wav" {
|
||||||
cacheControl = cacheControlMax
|
return cacheControlMax, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return cacheControlNone, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveFile(c echo.Context, file http.File, info os.FileInfo, etag string) error {
|
||||||
|
|
||||||
|
c.Response().Header().Set("Server", "Vikunja")
|
||||||
|
c.Response().Header().Set("Vary", "Accept-Encoding")
|
||||||
|
c.Response().Header().Set("Etag", etag)
|
||||||
|
|
||||||
|
cacheControl, err := getCacheControlHeader(info, file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
c.Response().Header().Set("Cache-Control", cacheControl)
|
c.Response().Header().Set("Cache-Control", cacheControl)
|
||||||
|
|
||||||
http.ServeContent(c.Response(), c.Request(), info.Name(), info.ModTime(), file)
|
http.ServeContent(c.Response(), c.Request(), info.Name(), info.ModTime(), file)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user