mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 20:17:32 +00:00
change caching for console (service worker) (#261)
This commit is contained in:
@@ -54,6 +54,8 @@ export ZITADEL_CSRF_DEV=true
|
||||
#CACHE
|
||||
export ZITADEL_CACHE_MAXAGE=12h
|
||||
export ZITADEL_CACHE_SHARED_MAXAGE=168h
|
||||
export ZITADEL_SHORT_CACHE_MAXAGE=5m
|
||||
export ZITADEL_SHORT_CACHE_SHARED_MAXAGE=15min
|
||||
|
||||
#Console
|
||||
export ZITADEL_CONSOLE_ENV_DIR=../../console/src/assets/
|
||||
|
@@ -205,7 +205,10 @@ Admin:
|
||||
Console:
|
||||
Port: 50050
|
||||
EnvOverwriteDir: $ZITADEL_CONSOLE_ENV_DIR
|
||||
Cache:
|
||||
ShortCache:
|
||||
MaxAge: $ZITADEL_SHORT_CACHE_MAXAGE
|
||||
SharedMaxAge: $ZITADEL_SHORT_CACHE_SHARED_MAXAGE
|
||||
LongCache:
|
||||
MaxAge: $ZITADEL_CACHE_MAXAGE
|
||||
SharedMaxAge: $ZITADEL_CACHE_SHARED_MAXAGE
|
||||
CSPDomain: $ZITADEL_DEFAULT_DOMAIN
|
||||
|
@@ -17,7 +17,8 @@ import (
|
||||
type Config struct {
|
||||
Port string
|
||||
EnvOverwriteDir string
|
||||
Cache middleware.CacheConfig
|
||||
ShortCache middleware.CacheConfig
|
||||
LongCache middleware.CacheConfig
|
||||
CSPDomain string
|
||||
}
|
||||
|
||||
@@ -28,8 +29,17 @@ type spaHandler struct {
|
||||
const (
|
||||
envRequestPath = "/assets/environment.json"
|
||||
envDefaultDir = "/console/"
|
||||
)
|
||||
|
||||
manifestFile = "/manifest.webmanifest"
|
||||
var (
|
||||
paths = []string{
|
||||
"/index.html",
|
||||
"/manifest.webmanifest",
|
||||
"/ngsw.json",
|
||||
"/ngsw-worker.js",
|
||||
"/safety-worker.js",
|
||||
"/worker-basic.min.js",
|
||||
}
|
||||
)
|
||||
|
||||
func (i *spaHandler) Open(name string) (http.File, error) {
|
||||
@@ -50,7 +60,12 @@ func Start(ctx context.Context, config Config) error {
|
||||
if config.EnvOverwriteDir != "" {
|
||||
envDir = config.EnvOverwriteDir
|
||||
}
|
||||
cache := AssetsCacheInterceptorIgnoreManifest(config.Cache.MaxAge.Duration, config.Cache.SharedMaxAge.Duration)
|
||||
cache := AssetsCacheInterceptorIgnoreManifest(
|
||||
config.ShortCache.MaxAge.Duration,
|
||||
config.ShortCache.SharedMaxAge.Duration,
|
||||
config.LongCache.MaxAge.Duration,
|
||||
config.LongCache.SharedMaxAge.Duration,
|
||||
)
|
||||
security := middleware.SecurityHeaders(csp(config.CSPDomain), nil)
|
||||
http.Handle("/", cache(security(http.FileServer(&spaHandler{statikFS}))))
|
||||
http.Handle(envRequestPath, cache(security(http.StripPrefix("/assets", http.FileServer(http.Dir(envDir))))))
|
||||
@@ -72,14 +87,16 @@ func csp(zitadelDomain string) *middleware.CSP {
|
||||
return &csp
|
||||
}
|
||||
|
||||
func AssetsCacheInterceptorIgnoreManifest(maxAge, sharedMaxAge time.Duration) func(http.Handler) http.Handler {
|
||||
func AssetsCacheInterceptorIgnoreManifest(shortMaxAge, shortSharedMaxAge, longMaxAge, longSharedMaxAge time.Duration) func(http.Handler) http.Handler {
|
||||
return func(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == manifestFile {
|
||||
middleware.NoCacheInterceptor(handler).ServeHTTP(w, r)
|
||||
for _, path := range paths {
|
||||
if r.URL.Path == path {
|
||||
middleware.AssetsCacheInterceptor(shortMaxAge, shortSharedMaxAge, handler).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
middleware.AssetsCacheInterceptor(maxAge, sharedMaxAge, handler).ServeHTTP(w, r)
|
||||
middleware.AssetsCacheInterceptor(longMaxAge, longSharedMaxAge, handler).ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user