diff --git a/config/caching.go b/config/caching.go new file mode 100644 index 0000000..5a6c180 --- /dev/null +++ b/config/caching.go @@ -0,0 +1,6 @@ +package config + +import "time" + +// todo: make this configurable +const ExpiresIn = 5 * time.Minute diff --git a/doc/dev/framework-migration.md b/doc/dev/framework-migration.md index 1c0b70b..5d19436 100644 --- a/doc/dev/framework-migration.md +++ b/doc/dev/framework-migration.md @@ -14,3 +14,25 @@ - add limiter (maybe it should be in nginx) - add caching (maybe it should be in nginx) + +## Guide for caching + +The majority of the site's traffic is images, and all images on the Pixiv route can be cached forever. + Cached in browser: pixiv headers cache images for year + On the server, the reverse proxy (e.g. nginx) can be cached worry-free forever. + +JSON requests to Pixiv can be cached for some time. + todo: We should have a middleware handling that. + +Assets are already cached properly thanks to net/http. + +Every rendered page can be cached for 5 minutes or so in the browser. +However, after changing settings, the page need to be refreshed instantly. + Cache in browser: todo: read below implement in template/render.go + The reverse proxy shouldn't do anything about this. + +The only three headers that controls caching in browser and middleboxes: + +- [Age](https://httpwg.org/specs/rfc9111.html#field.age) +- [Cache-Control](https://httpwg.org/specs/rfc9111.html#field.cache-control) +- [Expires](https://httpwg.org/specs/rfc9111.html#field.expires) diff --git a/main.go b/main.go index 53804ae..ea244d5 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,10 @@ func CanRequestSkipLimiter(r *http.Request) bool { func CanRequestSkipLogger(r *http.Request) bool { // return false path := r.URL.Path - return CanRequestSkipLimiter(r) || + return strings.HasPrefix(path, "/img/") || + strings.HasPrefix(path, "/css/") || + strings.HasPrefix(path, "/js/") || + strings.HasPrefix(path, "/proxy/s.pximg.net/") || strings.HasPrefix(path, "/proxy/i.pximg.net/") } @@ -82,7 +85,6 @@ func main() { router.ServeHTTP(w, r) } - CatchError(func(w http.ResponseWriter, r *http.Request) error { err := GetUserContext(r).err if err != nil { // error handler @@ -228,7 +230,7 @@ func defineRoutes() *mux.Router { // Legacy illust URL router.HandleFunc("/member_illust.php", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/artworks/"+ routes.GetQueryParam(r, "illust_id"), http.StatusPermanentRedirect) + http.Redirect(w, r, "/artworks/"+routes.GetQueryParam(r, "illust_id"), http.StatusPermanentRedirect) }).Methods("GET") router.NewRoute().HandlerFunc(CatchError(func(w http.ResponseWriter, r *http.Request) error { diff --git a/template/render.go b/template/render.go index 0f5dc85..1ed6499 100644 --- a/template/render.go +++ b/template/render.go @@ -6,7 +6,9 @@ import ( "net/http" "reflect" "strings" + // "time" + // "codeberg.org/vnpower/pixivfe/v2/config" "codeberg.org/vnpower/pixivfe/v2/session" "codeberg.org/vnpower/pixivfe/v2/utils" @@ -35,6 +37,8 @@ func InitTemplatingEngine(DisableCache bool) { // render the template selected based on the name of type `T` func Render[T any](w http.ResponseWriter, r *http.Request, data T) error { w.Header().Set("content-type", "text/html; charset=utf-8") + // todo: think about caching a bit more + // w.Header().Set("expires", time.Now().Add(config.ExpiresIn).Format(time.RFC1123)) return RenderInner(w, GetTemplatingVariables(r), data) }