Skip to content

Commit

Permalink
add caching to the static files
Browse files Browse the repository at this point in the history
  • Loading branch information
emad-elsaid committed May 11, 2024
1 parent 359fe90 commit 8c96311
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion extensions/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
}

func manifest(w Response, r Request) Output {
return Render("manifest", Locals{"sitename": SITENAME})
return Cache(Render("manifest", Locals{"sitename": SITENAME}))
}

func head(_ Page) template.HTML {
Expand Down
2 changes: 1 addition & 1 deletion fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func staticHandler(r Request) (Output, error) {
return nil, err
} else {
f.Close()
return server.ServeHTTP, nil
return Cache(server.ServeHTTP), nil
}
}
36 changes: 30 additions & 6 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

emojiAst "github.com/yuin/goldmark-emoji/ast"
Expand Down Expand Up @@ -49,7 +50,11 @@ type Page interface {

type page struct {
name string
ast ast.Node

l sync.Mutex
lastUpdate time.Time
ast ast.Node
content *Markdown
}

func (p *page) Name() string {
Expand All @@ -66,11 +71,11 @@ func (p *page) Exists() bool {
}

func (p *page) Render() template.HTML {
content := p.Content()
content = PreProcess(content)
content := p.preProcessedContent()
ast := p.AST()

var buf bytes.Buffer
if err := MarkDownRenderer.Convert([]byte(content), &buf); err != nil {
if err := MarkDownRenderer.Renderer().Render(&buf, []byte(content), ast); err != nil {
return template.HTML(err.Error())
}

Expand All @@ -85,6 +90,22 @@ func (p *page) Content() Markdown {
return Markdown(dat)
}

func (p *page) preProcessedContent() Markdown {
p.l.Lock()
defer p.l.Unlock()

modtime := p.ModTime()

if p.content == nil || modtime.Equal(p.lastUpdate) {
c := p.Content()
c = PreProcess(c)
p.content = &c
p.lastUpdate = p.ModTime()
}

return Markdown(*p.content)
}

func (p *page) Delete() bool {
defer Trigger(AfterDelete, p)

Expand Down Expand Up @@ -123,8 +144,11 @@ func (p *page) ModTime() time.Time {
}

func (p *page) AST() ast.Node {
if p.ast == nil {
p.ast = MarkDownRenderer.Parser().Parse(text.NewReader([]byte(p.Content())))
lastModified := p.lastUpdate
content := p.preProcessedContent()

if p.ast == nil || p.lastUpdate != lastModified {
p.ast = MarkDownRenderer.Parser().Parse(text.NewReader([]byte(content)))
}

return p.ast
Expand Down
8 changes: 8 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,11 @@ func requestLoggerHandler(h http.Handler) http.Handler {
h.ServeHTTP(w, r)
})
}

// Cache wraps Output and adds header to instruct the browser to cache the output
func Cache(out Output) Output {
return func(w Response, r Request) {
w.Header().Add("Cache-Control", "max-age=604800")
out(w, r)
}
}

0 comments on commit 8c96311

Please sign in to comment.