From 63ec0d559b2ee014c6ddbc2008439ad093f7a722 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Mon, 29 Aug 2022 01:30:08 +0200 Subject: [PATCH] Miglior gestione dei file statici --- server/fiber.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/server/fiber.go b/server/fiber.go index ed7e191..66af044 100644 --- a/server/fiber.go +++ b/server/fiber.go @@ -3,9 +3,11 @@ package server import ( "time" + "git.phc.dm.unipi.it/phc/website/config" "git.phc.dm.unipi.it/phc/website/handler" "git.phc.dm.unipi.it/phc/website/model" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/etag" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/redirect/v2" @@ -52,9 +54,28 @@ func routes(h handler.Service, r fiber.Router) { }, })) + // Better static file serving setup + r.Route("/public", func(r fiber.Router) { + // Cache client side files based on checksum + r.Use(etag.New()) + + staticConfig := fiber.Static{} + if config.Mode == "development" { + // if no "Cache-Control" is present the browser will cache heuristically (and we don't want that) + r.Use(func(c *fiber.Ctx) error { + c.Set("Cache-Control", "no-cache") + return c.Next() + }) + + staticConfig.CacheDuration = -1 + staticConfig.MaxAge = -1 + } + + r.Static("/", "./_frontend/out", staticConfig) + r.Static("/", "./_public", staticConfig) + }) + // Serve generated css and js files and the static "./_public" folder - r.Static("/public/", "./_frontend/out") - r.Static("/public/", "./_public") // Process all request and add user to the request context if there is a session cookie r.Use(UserMiddleware(h))