1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-26 18:21:01 +00:00

refactor(templates): be explicit about dependencies

Instead of blindly compiling all the common/ templates for every view/ ones,
let's be explicit about the dependencies. This should significantly decrease
the resident memory consumption, as ParseTemplate is responsible for ~10M of
the current 11M of heap memory on my instance, so any win there is interesting.
This will also allow better factorization of templates, now that everything is
explicit. Another side-effect is that it'll make testing easier, as we now have
a comprehensive list of views/ templates affected by a change in a file in
common/
This commit is contained in:
jvoisin 2025-08-08 18:00:59 +02:00 committed by Frédéric Guillot
parent b30eac4ebe
commit da8bf3890c
3 changed files with 50 additions and 34 deletions

View file

@ -7,7 +7,6 @@ import (
"bytes" "bytes"
"embed" "embed"
"html/template" "html/template"
"log/slog"
"time" "time"
"miniflux.app/v2/internal/locale" "miniflux.app/v2/internal/locale"
@ -21,9 +20,6 @@ var commonTemplateFiles embed.FS
//go:embed templates/views/*.html //go:embed templates/views/*.html
var viewTemplateFiles embed.FS var viewTemplateFiles embed.FS
//go:embed templates/standalone/*.html
var standaloneTemplateFiles embed.FS
// Engine handles the templating system. // Engine handles the templating system.
type Engine struct { type Engine struct {
templates map[string]*template.Template templates map[string]*template.Template
@ -38,43 +34,66 @@ func NewEngine(router *mux.Router) *Engine {
} }
} }
// ParseTemplates parses template files embed into the application. func (e *Engine) ParseTemplates() {
func (e *Engine) ParseTemplates() error {
funcMap := e.funcMap.Map() funcMap := e.funcMap.Map()
commonTemplates := template.Must(template.New("").Funcs(funcMap).ParseFS(commonTemplateFiles, "templates/common/*.html")) templates := map[string][]string{ // this isn't a global variable so that it can be garbage-collected.
"about.html": {"layout.html", "settings_menu.html"},
dirEntries, err := viewTemplateFiles.ReadDir("templates/views") "add_subscription.html": {"feed_menu.html", "layout.html", "settings_menu.html"},
if err != nil { "api_keys.html": {"layout.html", "settings_menu.html"},
return err "bookmark_entries.html": {"item_meta.html", "layout.html", "pagination.html"},
"categories.html": {"layout.html"},
"category_entries.html": {"item_meta.html", "layout.html", "pagination.html"},
"category_feeds.html": {"feed_list.html", "layout.html"},
"choose_subscription.html": {"feed_menu.html", "layout.html"},
"create_api_key.html": {"layout.html", "settings_menu.html"},
"create_category.html": {"layout.html"},
"create_user.html": {"layout.html", "settings_menu.html"},
"edit_category.html": {"layout.html", "settings_menu.html"},
"edit_feed.html": {"layout.html"},
"edit_user.html": {"layout.html", "settings_menu.html"},
"entry.html": {"layout.html"},
"feed_entries.html": {"item_meta.html", "layout.html", "pagination.html"},
"feeds.html": {"feed_list.html", "feed_menu.html", "item_meta.html", "layout.html", "pagination.html"},
"history_entries.html": {"item_meta.html", "layout.html", "pagination.html"},
"import.html": {"feed_menu.html", "layout.html"},
"integrations.html": {"layout.html", "settings_menu.html"},
"login.html": {"layout.html"},
"offline.html": {},
"search.html": {"item_meta.html", "layout.html", "pagination.html"},
"sessions.html": {"layout.html", "settings_menu.html"},
"settings.html": {"layout.html", "settings_menu.html"},
"shared_entries.html": {"layout.html", "pagination.html"},
"tag_entries.html": {"item_meta.html", "layout.html", "pagination.html"},
"unread_entries.html": {"item_meta.html", "layout.html", "pagination.html"},
"users.html": {"layout.html", "settings_menu.html"},
"webauthn_rename.html": {"layout.html"},
} }
for _, dirEntry := range dirEntries {
fullName := "templates/views/" + dirEntry.Name() for name, dependencies := range templates {
slog.Debug("Parsing template", slog.String("template_name", fullName)) tpl := template.New("").Funcs(funcMap)
commonTemplatesClone, err := commonTemplates.Clone() for _, dependency := range dependencies {
if err != nil { template.Must(tpl.ParseFS(commonTemplateFiles, "templates/common/"+dependency))
panic("Unable to clone the common template")
} }
e.templates[dirEntry.Name()] = template.Must(commonTemplatesClone.ParseFS(viewTemplateFiles, fullName)) e.templates[name] = template.Must(tpl.ParseFS(viewTemplateFiles, "templates/views/"+name))
} }
dirEntries, err = standaloneTemplateFiles.ReadDir("templates/standalone") // Sanity check to ensure that all templates are correctly declared in `templates`.
if err != nil { if entries, err := viewTemplateFiles.ReadDir("templates/views"); err == nil {
return err for _, entry := range entries {
if _, ok := e.templates[entry.Name()]; !ok {
panic("Template " + entry.Name() + " isn't declared in ParseTemplates")
}
}
} else {
panic("Unable to read all embedded views templates")
} }
for _, dirEntry := range dirEntries {
fullName := "templates/standalone/" + dirEntry.Name()
slog.Debug("Parsing template", slog.String("template_name", fullName))
e.templates[dirEntry.Name()] = template.Must(template.New(dirEntry.Name()).Funcs(funcMap).ParseFS(standaloneTemplateFiles, fullName))
}
return nil
} }
// Render process a template. // Render process a template.
func (e *Engine) Render(name string, data map[string]any) []byte { func (e *Engine) Render(name string, data map[string]any) []byte {
tpl, ok := e.templates[name] tpl, ok := e.templates[name]
if !ok { if !ok {
panic("This template does not exists: " + name) panic("The template " + name + " does not exists.")
} }
printer := locale.NewPrinter(data["language"].(string)) printer := locale.NewPrinter(data["language"].(string))
@ -89,8 +108,7 @@ func (e *Engine) Render(name string, data map[string]any) []byte {
}) })
var b bytes.Buffer var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "base", data) if err := tpl.ExecuteTemplate(&b, "base", data); err != nil {
if err != nil {
panic(err) panic(err)
} }

View file

@ -19,9 +19,7 @@ func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
middleware := newMiddleware(router, store) middleware := newMiddleware(router, store)
templateEngine := template.NewEngine(router) templateEngine := template.NewEngine(router)
if err := templateEngine.ParseTemplates(); err != nil { templateEngine.ParseTemplates()
panic(err)
}
handler := &handler{router, store, templateEngine, pool} handler := &handler{router, store, templateEngine, pool}