diff --git a/modules/version/version.go b/modules/version/version.go index efedb39d..08dca7f4 100644 --- a/modules/version/version.go +++ b/modules/version/version.go @@ -20,6 +20,12 @@ var ( GitCommit string ) +func init() { + if GitCommit == `` { + GitCommit = `HEAD` + } +} + // AddCommands ... func AddCommands(rootCmd *cobra.Command) { versionCmd := &cobra.Command{ diff --git a/theme/blog/statics/scripts/header.js b/theme/blog/statics/scripts/header.js index 91528c32..266ac255 100644 --- a/theme/blog/statics/scripts/header.js +++ b/theme/blog/statics/scripts/header.js @@ -104,7 +104,7 @@ TaoBlog.fn.fadeIn = function(elem, callback) { elem.style.display = 'block'; if (typeof callback == 'function') { elem.addEventListener('animationend', function(event) { - console.log('fade-in animationend'); + // console.log('fade-in animationend'); callback(); }, { once: true}); } @@ -113,7 +113,7 @@ TaoBlog.fn.fadeIn = function(elem, callback) { TaoBlog.fn.fadeOut = function(elem, callback) { elem.classList.remove('fade-in'); elem.addEventListener('animationend', function(event) { - console.log('fade-out animationend'); + // console.log('fade-out animationend'); elem.style.display = 'none'; if (typeof callback == 'function') { callback(); diff --git a/theme/modules/handle304/handle304.go b/theme/modules/handle304/handle304.go index 308c5238..d3e80f0b 100644 --- a/theme/modules/handle304/handle304.go +++ b/theme/modules/handle304/handle304.go @@ -1,13 +1,14 @@ package handle304 import ( + "fmt" "net/http" "time" "github.com/movsb/taoblog/modules/version" ) -const httpgmtFormat = "Mon, 02 Jan 2006 15:04:05 GMT" +const httpGmtFormat = "Mon, 02 Jan 2006 15:04:05 GMT" // Requester ... type Requester interface { @@ -19,11 +20,15 @@ type Responder interface { Response(w http.ResponseWriter) } -// ArticleRequest ... +// ArticleRequest 判断是否符合 304 请求。 +// 如果符合,返回 true;否则,返回 false。 func ArticleRequest(w http.ResponseWriter, req *http.Request, modified time.Time) bool { handlers := []Requester{ NotModified{modified}, - CommitMatch{}, + EntityTag{ + modified: modified, + version: version.GitCommit, + }, } for _, handler := range handlers { if !handler.Request(req) { @@ -38,7 +43,10 @@ func ArticleRequest(w http.ResponseWriter, req *http.Request, modified time.Time func ArticleResponse(w http.ResponseWriter, modified time.Time) { handlers := []Responder{ NotModified{modified}, - CommitMatch{}, + EntityTag{ + modified: modified, + version: version.GitCommit, + }, } for _, handler := range handlers { handler.Response(w) @@ -53,40 +61,34 @@ type NotModified struct { // Request ... func (nm NotModified) Request(req *http.Request) bool { h := req.Header.Get(`If-Modified-Since`) - t, _ := time.ParseInLocation(httpgmtFormat, h, time.UTC) + t, _ := time.ParseInLocation(httpGmtFormat, h, time.UTC) return t.Equal(nm.Modified) } // Response ... func (nm NotModified) Response(w http.ResponseWriter) { - w.Header().Add(`Last-Modified`, nm.Modified.UTC().Format(httpgmtFormat)) + w.Header().Add(`Last-Modified`, nm.Modified.UTC().Format(httpGmtFormat)) } -// CommitMatch ... -type CommitMatch struct { +// 格式:文章的修改日期-博客版本号 +// Typically, the ETag value is a hash of the content, a hash of the last modification timestamp, or just a revision number. +// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag +type EntityTag struct { + modified time.Time + version string +} + +func (cm EntityTag) format() string { + return fmt.Sprintf(`"%d-%s"`, cm.modified.Unix(), cm.version) } // Request ... -func (cm CommitMatch) Request(req *http.Request) bool { - commitCookie, err := req.Cookie(`commit`) - if err != nil { - return false - } - return commitCookie.Value == version.GitCommit +func (cm EntityTag) Request(req *http.Request) bool { + haystack := req.Header.Get(`If-None-Match`) + return haystack == cm.format() } // Response ... -func (cm CommitMatch) Response(w http.ResponseWriter) { - v := version.GitCommit - if v == `` { - v = `HEAD` - } - http.SetCookie(w, &http.Cookie{ - Name: `commit`, - Value: v, - Path: `/`, - MaxAge: 0, - Secure: false, - HttpOnly: true, - }) +func (cm EntityTag) Response(w http.ResponseWriter) { + w.Header().Add(`ETag`, cm.format()) }