Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: implement CDN for static assets (#415)

Guilherme Oenningb9fc685351fa
diff --git a/.example.env b/.example.env
index 2ea4cba7a..f4d6a215b 100644
--- a/.example.env
+++ b/.example.env
@@ -3,6 +3,8 @@ LOG_LEVEL=DEBUG
DATABASE_URL=postgres://fider:fider_pw@localhost:5555/fider?sslmode=disable
JWT_SECRET=hsjl]W;&ZcHxT&FK;s%bgIQF:#ch=~#Al4:5]N;7V<qPZ3e9lT4'%;go;LIkc%k
+CDN_HOST=dev.assets-fider.io:3000
+
OAUTH_FACEBOOK_APPID=
OAUTH_FACEBOOK_SECRET=
diff --git a/.test.env b/.test.env
index e7b7a48e6..d0c822fd6 100644
--- a/.test.env
+++ b/.test.env
@@ -5,6 +5,8 @@ DATABASE_URL=postgres://fider_test:fider_test_pw@localhost:5566/fider_test?sslmo
AUTH_ENDPOINT=http://login.test.fider.io:3000
JWT_SECRET=SOME_RANDOM_TOKEN_JUST_FOR_TESTING
+CDN_HOST=
+
EMAIL_SMTP_HOST=smtp.random.org
diff --git a/app/middlewares/security.go b/app/middlewares/security.go
index f26babd1d..5a05cc063 100644
--- a/app/middlewares/security.go
+++ b/app/middlewares/security.go
@@ -3,6 +3,7 @@ package middlewares
import (
"fmt"
+ "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/web"
)
@@ -10,10 +11,16 @@ import (
func Secure() web.MiddlewareFunc {
return func(next web.HandlerFunc) web.HandlerFunc {
return func(c web.Context) error {
- c.Response.Header().Add("Content-Security-Policy", fmt.Sprintf(web.CspPolicyTemplate, c.ContextID()))
- c.Response.Header().Add("X-XSS-Protection", "1; mode=block")
- c.Response.Header().Add("X-Content-Type-Options", "nosniff")
- c.Response.Header().Add("Referrer-Policy", "no-referrer-when-downgrade")
+ cdnHost := env.GetEnvOrDefault("CDN_HOST", "")
+ if cdnHost != "" {
+ cdnHost = "*." + cdnHost
+ }
+ csp := fmt.Sprintf(web.CspPolicyTemplate, c.ContextID(), cdnHost)
+
+ c.Response.Header().Set("Content-Security-Policy", csp)
+ c.Response.Header().Set("X-XSS-Protection", "1; mode=block")
+ c.Response.Header().Set("X-Content-Type-Options", "nosniff")
+ c.Response.Header().Set("Referrer-Policy", "no-referrer-when-downgrade")
return next(c)
}
}
diff --git a/app/middlewares/security_test.go b/app/middlewares/security_test.go
index 32b021f6e..a58084c36 100644
--- a/app/middlewares/security_test.go
+++ b/app/middlewares/security_test.go
@@ -1,8 +1,8 @@
package middlewares_test
import (
- "fmt"
"net/http"
+ "os"
"testing"
"github.com/getfider/fider/app/middlewares"
@@ -11,20 +11,45 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
-func TestSecure(t *testing.T) {
+func TestSecureWithoutCDN(t *testing.T) {
RegisterT(t)
server, _ := mock.NewServer()
server.Use(middlewares.Secure())
- var id string
+ var ctxID string
status, response := server.Execute(func(c web.Context) error {
- id = c.ContextID()
+ ctxID = c.ContextID()
return c.NoContent(http.StatusOK)
})
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'nonce-" + ctxID + "' https://fonts.googleapis.com ; script-src 'self' 'nonce-" + ctxID + "' https://cdn.polyfill.io https://www.google-analytics.com ; img-src 'self' https: data: ; font-src 'self' https://fonts.gstatic.com data: ; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com"
+
+ Expect(status).Equals(http.StatusOK)
+ Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
+ Expect(response.Header().Get("X-XSS-Protection")).Equals("1; mode=block")
+ Expect(response.Header().Get("X-Content-Type-Options")).Equals("nosniff")
+ Expect(response.Header().Get("Referrer-Policy")).Equals("no-referrer-when-downgrade")
+}
+
+func TestSecureWithCDN(t *testing.T) {
+ RegisterT(t)
+
+ os.Setenv("CDN_HOST", "test.fider.io")
+
+ server, _ := mock.NewServer()
+ server.Use(middlewares.Secure())
+
+ var ctxID string
+ status, response := server.Execute(func(c web.Context) error {
+ ctxID = c.ContextID()
+ return c.NoContent(http.StatusOK)
+ })
+
+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'nonce-" + ctxID + "' https://fonts.googleapis.com *.test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://cdn.polyfill.io https://www.google-analytics.com *.test.fider.io; img-src 'self' https: data: *.test.fider.io; font-src 'self' https://fonts.gstatic.com data: *.test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com"
+
Expect(status).Equals(http.StatusOK)
- Expect(response.Header().Get("Content-Security-Policy")).Equals(fmt.Sprintf(web.CspPolicyTemplate, id))
+ Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)
Expect(response.Header().Get("X-XSS-Protection")).Equals("1; mode=block")
Expect(response.Header().Get("X-Content-Type-Options")).Equals("nosniff")
Expect(response.Header().Get("Referrer-Policy")).Equals("no-referrer-when-downgrade")
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.go
index 5bf38096b..3b2765418 100644
--- a/app/pkg/env/env.go
+++ b/app/pkg/env/env.go
@@ -140,9 +140,22 @@ func IsDevelopment() bool {
// Subdomain returns the Fider subdomain (if available) from given host
func Subdomain(host string) string {
+ if IsSingleHostMode() {
+ return ""
+ }
+
domain := MultiTenantDomain()
if domain != "" && strings.Contains(host, domain) {
return strings.Replace(host, domain, "", -1)
}
+
+ if IsDefined("CDN_HOST") {
+ domain = MustGet("CDN_HOST")
+ parts := strings.Split(domain, ":")
+ if parts[0] != "" && strings.Contains(host, "."+parts[0]) {
+ return strings.Replace(host, "."+parts[0], "", -1)
+ }
+ }
+
return ""
}
diff --git a/app/pkg/env/env_test.go b/app/pkg/env/env_test.go
index 4508bc46a..aa8db8d69 100644
--- a/app/pkg/env/env_test.go
+++ b/app/pkg/env/env_test.go
@@ -127,13 +127,21 @@ func TestGetPublicIP(t *testing.T) {
func TestSubdomain(t *testing.T) {
RegisterT(t)
+ Expect(env.Subdomain("demo.test.assets-fider.io")).Equals("")
+
+ os.Setenv("CDN_HOST", "test.assets-fider.io:3000")
+
Expect(env.Subdomain("demo.test.fider.io")).Equals("demo")
+ Expect(env.Subdomain("demo.test.assets-fider.io")).Equals("demo")
Expect(env.Subdomain("test.fider.io")).Equals("")
+ Expect(env.Subdomain("test.assets-fider.io")).Equals("")
Expect(env.Subdomain("helloworld.com")).Equals("")
os.Setenv("HOST_MODE", "single")
Expect(env.Subdomain("demo.test.fider.io")).Equals("")
+ Expect(env.Subdomain("demo.test.assets-fider.io")).Equals("")
Expect(env.Subdomain("test.fider.io")).Equals("")
+ Expect(env.Subdomain("test.assets-fider.io")).Equals("")
Expect(env.Subdomain("helloworld.com")).Equals("")
}
diff --git a/app/pkg/web/context.go b/app/pkg/web/context.go
index df1689d74..eb7319319 100644
--- a/app/pkg/web/context.go
+++ b/app/pkg/web/context.go
@@ -145,7 +145,9 @@ func (ctx *Context) SetTenant(tenant *models.Tenant) {
} else {
ctx.Logger().Debugf("Current tenant: nil")
}
- ctx.Services().SetCurrentTenant(tenant)
+ if ctx.Services() != nil {
+ ctx.Services().SetCurrentTenant(tenant)
+ }
ctx.Set(tenantContextKey, tenant)
}
@@ -296,13 +298,19 @@ func (ctx *Context) SetUser(user *models.User) {
} else {
ctx.Logger().Debugf("Logged as: nil")
}
+ if ctx.Services() != nil {
+ ctx.Services().SetCurrentUser(user)
+ }
ctx.Set(userContextKey, user)
- ctx.Services().SetCurrentUser(user)
}
//Services returns current app.Services from context
func (ctx *Context) Services() *app.Services {
- return ctx.Get(servicesContextKey).(*app.Services)
+ svc, ok := ctx.Get(servicesContextKey).(*app.Services)
+ if ok {
+ return svc
+ }
+ return nil
}
//AddAuthCookie generates and adds a cookie
@@ -482,3 +490,27 @@ func (ctx *Context) Redirect(url string) error {
ctx.Response.WriteHeader(http.StatusTemporaryRedirect)
return nil
}
+
+// GlobalAssetsURL return the full URL to a globally shared static asset
+func (ctx *Context) GlobalAssetsURL(path string, a ...interface{}) string {
+ path = fmt.Sprintf(path, a...)
+ if env.IsDefined("CDN_HOST") {
+ if env.IsSingleHostMode() {
+ return ctx.Request.URL.Scheme + "://" + env.MustGet("CDN_HOST") + path
+ }
+ return ctx.Request.URL.Scheme + "://cdn." + env.MustGet("CDN_HOST") + path
+ }
+ return ctx.BaseURL() + path
+}
+
+// TenantAssetsURL return the full URL to a tenant-specific static asset
+func (ctx *Context) TenantAssetsURL(path string, a ...interface{}) string {
+ path = fmt.Sprintf(path, a...)
+ if env.IsDefined("CDN_HOST") {
+ if env.IsSingleHostMode() {
+ return ctx.Request.URL.Scheme + "://" + env.MustGet("CDN_HOST") + path
+ }
+ return ctx.Request.URL.Scheme + "://" + ctx.Tenant().Subdomain + "." + env.MustGet("CDN_HOST") + path
+ }
+ return ctx.BaseURL() + path
+}
diff --git a/app/pkg/web/context_test.go b/app/pkg/web/context_test.go
index 4c3bd24ba..e10669212 100644
--- a/app/pkg/web/context_test.go
+++ b/app/pkg/web/context_test.go
@@ -1,7 +1,9 @@
package web_test
import (
+ "crypto/tls"
"net/http/httptest"
+ "net/url"
"os"
"strings"
"testing"
@@ -11,12 +13,24 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
-func newGetContext(params web.StringMap) *web.Context {
+func newGetContext(rawurl string, headers map[string]string) *web.Context {
+ u, _ := url.Parse(rawurl)
e := web.New(nil)
res := httptest.NewRecorder()
- req := httptest.NewRequest("GET", "/some/resource", nil)
- req.Host = "demo.test.fider.io:3000"
- ctx := e.NewContext(res, req, params)
+ req := httptest.NewRequest("GET", u.RequestURI(), nil)
+ req.Host = u.Host
+
+ if u.Scheme == "https" {
+ req.TLS = &tls.ConnectionState{}
+ }
+
+ if headers != nil {
+ for k, v := range headers {
+ req.Header.Set(k, v)
+ }
+ }
+
+ ctx := e.NewContext(res, req, nil)
return &ctx
}
@@ -33,7 +47,7 @@ func newBodyContext(method string, params web.StringMap, body, contentType strin
func TestContextID(t *testing.T) {
RegisterT(t)
- ctx := newGetContext(nil)
+ ctx := newGetContext("http://demo.test.fider.io:3000", nil)
Expect(ctx.ContextID()).IsNotEmpty()
Expect(ctx.ContextID()).HasLen(32)
@@ -42,15 +56,41 @@ func TestContextID(t *testing.T) {
func TestBaseURL(t *testing.T) {
RegisterT(t)
- ctx := newGetContext(nil)
+ ctx := newGetContext("http://demo.test.fider.io:3000", nil)
Expect(ctx.BaseURL()).Equals("http://demo.test.fider.io:3000")
}
+func TestBaseURL_HTTPS(t *testing.T) {
+ RegisterT(t)
+
+ ctx := newGetContext("https://demo.test.fider.io:3000", nil)
+
+ Expect(ctx.BaseURL()).Equals("https://demo.test.fider.io:3000")
+}
+
+func TestBaseURL_HTTPS_Proxy(t *testing.T) {
+ RegisterT(t)
+
+ ctx := newGetContext("http://demo.test.fider.io:3000", map[string]string{
+ "X-Forwarded-Proto": "https",
+ })
+
+ Expect(ctx.BaseURL()).Equals("https://demo.test.fider.io:3000")
+}
+
+func TestCurrentURL(t *testing.T) {
+ RegisterT(t)
+
+ ctx := newGetContext("http://demo.test.fider.io:3000/resource?id=23", nil)
+
+ Expect(ctx.Request.URL.String()).Equals("http://demo.test.fider.io:3000/resource?id=23")
+}
+
func TestTenantURL(t *testing.T) {
RegisterT(t)
- ctx := newGetContext(nil)
+ ctx := newGetContext("http://login.test.fider.io:3000", nil)
tenant := &models.Tenant{
ID: 1,
Subdomain: "theavengers",
@@ -61,7 +101,7 @@ func TestTenantURL(t *testing.T) {
func TestTenantURL_WithCNAME(t *testing.T) {
RegisterT(t)
- ctx := newGetContext(nil)
+ ctx := newGetContext("http://demo.test.fider.io:3000", nil)
tenant := &models.Tenant{
ID: 1,
Subdomain: "theavengers",
@@ -70,14 +110,78 @@ func TestTenantURL_WithCNAME(t *testing.T) {
Expect(ctx.TenantBaseURL(tenant)).Equals("http://ideas.theavengers.com:3000")
}
-func TestTenantURL_SingleTenantMode(t *testing.T) {
+func TestTenantURL_SingleHostMode(t *testing.T) {
RegisterT(t)
os.Setenv("HOST_MODE", "single")
- ctx := newGetContext(nil)
+ ctx := newGetContext("http://demo.test.fider.io:3000", nil)
tenant := &models.Tenant{
ID: 1,
Subdomain: "theavengers",
}
Expect(ctx.TenantBaseURL(tenant)).Equals("http://demo.test.fider.io:3000")
}
+
+func TestGlobalAssetsURL_SingleHostMode(t *testing.T) {
+ RegisterT(t)
+
+ os.Setenv("HOST_MODE", "single")
+ ctx := newGetContext("http://demo.test.fider.io:3000", nil)
+ Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://demo.test.fider.io:3000/assets/main.js")
+ Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://demo.test.fider.io:3000/assets/main.css")
+
+ os.Setenv("CDN_HOST", "assets-fider.io")
+ Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://assets-fider.io/assets/main.js")
+ Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://assets-fider.io/assets/main.css")
+}
+
+func TestGlobalAssetsURL_MultiHostMode(t *testing.T) {
+ RegisterT(t)
+
+ os.Setenv("HOST_MODE", "multi")
+ ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
+ ctx.SetTenant(&models.Tenant{
+ ID: 1,
+ Subdomain: "theavengers",
+ CNAME: "ideas.theavengers.com",
+ })
+
+ Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://theavengers.test.fider.io:3000/assets/main.js")
+ Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://theavengers.test.fider.io:3000/assets/main.css")
+
+ os.Setenv("CDN_HOST", "assets-fider.io")
+ Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://cdn.assets-fider.io/assets/main.js")
+ Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://cdn.assets-fider.io/assets/main.css")
+}
+
+func TestTenantAssetsURL_SingleHostMode(t *testing.T) {
+ RegisterT(t)
+
+ os.Setenv("HOST_MODE", "single")
+ ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
+ Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://theavengers.test.fider.io:3000/assets/main.js")
+ Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://theavengers.test.fider.io:3000/assets/main.css")
+
+ os.Setenv("CDN_HOST", "assets-fider.io")
+ Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://assets-fider.io/assets/main.js")
+ Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://assets-fider.io/assets/main.css")
+}
+
+func TestTenantAssetsURL_MultiHostMode(t *testing.T) {
+ RegisterT(t)
+
+ os.Setenv("HOST_MODE", "multi")
+ ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
+ ctx.SetTenant(&models.Tenant{
+ ID: 1,
+ Subdomain: "theavengers",
+ CNAME: "ideas.theavengers.com",
+ })
+
+ Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://theavengers.test.fider.io:3000/assets/main.js")
+ Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://theavengers.test.fider.io:3000/assets/main.css")
+
+ os.Setenv("CDN_HOST", "assets-fider.io")
+ Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://theavengers.assets-fider.io/assets/main.js")
+ Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://theavengers.assets-fider.io/assets/main.css")
+}
diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.go
index 1e0b9b0dd..47d70731d 100644
--- a/app/pkg/web/engine.go
+++ b/app/pkg/web/engine.go
@@ -24,10 +24,10 @@ import (
var (
cspBase = "base-uri 'self'"
cspDefault = "default-src 'self'"
- cspStyle = "style-src 'self' 'nonce-%[1]s' https://fonts.googleapis.com"
- cspScript = "script-src 'self' 'nonce-%[1]s' https://cdn.polyfill.io https://www.google-analytics.com"
- cspFont = "font-src 'self' https://fonts.gstatic.com data:"
- cspImage = "img-src 'self' https: data:"
+ cspStyle = "style-src 'self' 'nonce-%[1]s' https://fonts.googleapis.com %[2]s"
+ cspScript = "script-src 'self' 'nonce-%[1]s' https://cdn.polyfill.io https://www.google-analytics.com %[2]s"
+ cspFont = "font-src 'self' https://fonts.gstatic.com data: %[2]s"
+ cspImage = "img-src 'self' https: data: %[2]s"
cspObject = "object-src 'none'"
cspMedia = "media-src 'none'"
cspConnect = "connect-src 'self' https://www.google-analytics.com"
diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.go
index a04c7f8de..9f6922cdd 100644
--- a/app/pkg/web/renderer.go
+++ b/app/pkg/web/renderer.go
@@ -28,35 +28,20 @@ var templateFunctions = template.FuncMap{
//Renderer is the default HTML Render
type Renderer struct {
- templates map[string]*template.Template
- logger log.Logger
- settings *models.SystemSettings
- jsBundle string
- vendorBundle string
- cssBundle string
+ templates map[string]*template.Template
+ logger log.Logger
+ settings *models.SystemSettings
+ assets map[string]string
}
// NewRenderer creates a new Renderer
func NewRenderer(settings *models.SystemSettings, logger log.Logger) *Renderer {
- r := &Renderer{
+ return &Renderer{
templates: make(map[string]*template.Template),
logger: logger,
settings: settings,
+ assets: make(map[string]string, 0),
}
-
- r.add("index.html")
- r.add("not-invited.html")
- r.add("legal.html")
- r.add("403.html")
- r.add("404.html")
- r.add("410.html")
- r.add("500.html")
-
- r.jsBundle = r.getBundle("/dist/js", "main")
- r.vendorBundle = r.getBundle("/dist/js", "vendor")
- r.cssBundle = r.getBundle("/dist/css", "main")
-
- return r
}
//Render a template based on parameters
@@ -72,12 +57,12 @@ func (r *Renderer) add(name string) *template.Template {
return tpl
}
-func (r *Renderer) getBundle(folder, prefix string) string {
+func (r *Renderer) getBundle(folder, prefix, suffix string) string {
files, _ := ioutil.ReadDir(env.Path(folder))
if len(files) > 0 {
for _, file := range files {
fileName := file.Name()
- if strings.HasPrefix(fileName, prefix) {
+ if strings.HasPrefix(fileName, prefix) && strings.HasSuffix(fileName, suffix) {
return fileName
}
}
@@ -93,16 +78,16 @@ func (r *Renderer) getBundle(folder, prefix string) string {
//Render a template based on parameters
func (r *Renderer) Render(w io.Writer, name string, props Props, ctx *Context) {
- tmpl, ok := r.templates[name]
- if !ok {
- panic(fmt.Errorf("The template '%s' does not exist", name))
+ if len(r.assets) == 0 || env.IsDevelopment() {
+ r.assets["main.js"] = r.getBundle("/dist/js", "main", "js")
+ r.assets["vendor.js"] = r.getBundle("/dist/js", "vendor", "js")
+ r.assets["main.css"] = r.getBundle("/dist/css", "main", "css")
+ r.assets["icons.woff2"] = r.getBundle("/dist/fonts", "icons", "woff2")
}
- if env.IsDevelopment() {
+ tmpl, ok := r.templates[name]
+ if !ok || env.IsDevelopment() {
tmpl = r.add(name)
- r.jsBundle = r.getBundle("/dist/js", "main")
- r.vendorBundle = r.getBundle("/dist/js", "vendor")
- r.cssBundle = r.getBundle("/dist/css", "main")
}
m := props.Data
@@ -127,20 +112,26 @@ func (r *Renderer) Render(w io.Writer, name string, props Props, ctx *Context) {
m["__Description"] = fmt.Sprintf("%.150s", description)
}
- m["__VendorBundle"] = "/assets/js/" + r.vendorBundle
- m["__JavaScriptBundle"] = "/assets/js/" + r.jsBundle
- m["__StyleBundle"] = "/assets/css/" + r.cssBundle
+ m["__VendorBundle"] = ctx.GlobalAssetsURL("/assets/js/%s", r.assets["vendor.js"])
+ m["__JavaScriptBundle"] = ctx.GlobalAssetsURL("/assets/js/%s", r.assets["main.js"])
+ m["__StyleBundle"] = ctx.GlobalAssetsURL("/assets/css/%s", r.assets["main.css"])
+ m["__FontBundle"] = ctx.GlobalAssetsURL("/assets/fonts/%s", r.assets["icons.woff2"])
+
m["__ContextID"] = ctx.ContextID()
if ctx.Tenant() != nil && ctx.Tenant().LogoID > 0 {
- m["__logo"] = fmt.Sprintf("%s/logo/200/%d", ctx.BaseURL(), ctx.Tenant().LogoID)
- m["__favicon"] = fmt.Sprintf("%s/logo/50/%d", ctx.BaseURL(), ctx.Tenant().LogoID)
+ m["__logo"] = ctx.TenantAssetsURL("/logo/200/%d", ctx.Tenant().LogoID)
+ m["__favicon"] = ctx.TenantAssetsURL("/logo/50/%d", ctx.Tenant().LogoID)
} else {
m["__logo"] = "https://getfider.com/images/logo-100x100.png"
- m["__favicon"] = "/favicon.ico"
+ m["__favicon"] = ctx.GlobalAssetsURL("/favicon.ico")
}
m["system"] = r.settings
m["baseURL"] = ctx.BaseURL()
+ m["assetsBaseURL"] = ctx.GlobalAssetsURL("")
+ if ctx.Tenant() != nil {
+ m["tenantAssetsBaseURL"] = ctx.TenantAssetsURL("")
+ }
m["currentURL"] = ctx.Request.URL.String()
m["tenant"] = ctx.Tenant()
m["auth"] = Map{
diff --git a/public/components/common/Footer.tsx b/public/components/common/Footer.tsx
index 73f5efa83..a41fcac1c 100644
--- a/public/components/common/Footer.tsx
+++ b/public/components/common/Footer.tsx
@@ -1,6 +1,8 @@
import "./Footer.scss";
import * as React from "react";
+import { page } from "@fider/services";
+
const logo = require("@fider/assets/images/logo-small.png");
export const Footer = () => {
@@ -8,7 +10,7 @@ export const Footer = () => {
<div id="c-footer">
<div className="container">
<a target="_blank" href="https://getfider.com/">
- <img src={logo} alt="Fider" />
+ <img src={`${page.getAssetsBaseUrl()}${logo}`} alt="Fider" />
<span>Powered by Fider</span>
</a>
</div>
diff --git a/public/components/common/Gravatar.tsx b/public/components/common/Gravatar.tsx
index 59f6f5611..7bc8d1ee3 100644
--- a/public/components/common/Gravatar.tsx
+++ b/public/components/common/Gravatar.tsx
@@ -13,7 +13,7 @@ export const Gravatar = (props: GravatarProps) => {
const size = props.size || "normal";
const id = props.user ? props.user.id : 0;
const name = props.user ? props.user.name : "";
- const url = `${page.getBaseUrl()}/avatars/50/${id}/${encodeURIComponent(name || "?")}`;
+ const url = `${page.getTenantAssetsBaseUrl()}/avatars/50/${id}/${encodeURIComponent(name || "?")}`;
const className = classSet({
"c-avatar": true,
diff --git a/public/components/common/Logo.tsx b/public/components/common/Logo.tsx
index de5146e7d..692dcadcc 100644
--- a/public/components/common/Logo.tsx
+++ b/public/components/common/Logo.tsx
@@ -1,5 +1,6 @@
import * as React from "react";
import { Tenant } from "@fider/models";
+import { page } from "@fider/services";
interface LogoProps {
url?: string;
@@ -14,7 +15,7 @@ export const Logo = (props: LogoProps) => {
const size = props.size || 200;
if (props.tenant && props.tenant.logoId > 0) {
- return <img src={`/logo/${size}/${props.tenant.logoId}`} alt={props.tenant.name} />;
+ return <img src={`${page.getTenantAssetsBaseUrl()}/logo/${size}/${props.tenant.logoId}`} alt={props.tenant.name} />;
}
return null;
diff --git a/public/services/page.ts b/public/services/page.ts
index dbf4b771e..53acbb7ea 100644
--- a/public/services/page.ts
+++ b/public/services/page.ts
@@ -12,6 +12,14 @@ export const getBaseUrl = (): string => {
return (window as any).props.baseURL;
};
+export const getAssetsBaseUrl = (): string => {
+ return (window as any).props.assetsBaseURL;
+};
+
+export const getTenantAssetsBaseUrl = (): string => {
+ return (window as any).props.tenantAssetsBaseURL;
+};
+
export const isSingleHostMode = (): boolean => {
return (window as any).props.system.mode === "single";
};
diff --git a/views/base.html b/views/base.html
index 3381014c4..5cc18061a 100644
--- a/views/base.html
+++ b/views/base.html
@@ -4,6 +4,7 @@
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link rel="icon" type="image/x-icon" href="{{ .__favicon }}">
+ <link rel="preload" href="{{ .__FontBundle }}" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="stylesheet" href="{{ .__StyleBundle }}" />
<title>{{ .__Title }}</title>
<meta name="description" content="{{ .__Description }}" />
@@ -14,7 +15,7 @@
<meta property="og:image" content="{{ .__logo }}">
{{ if .tenant }}
{{ if .tenant.CustomCSS }}
- <link rel="stylesheet" href="/custom/{{ .tenant.CustomCSS | md5 }}.css" />
+ <link rel="stylesheet" href="{{ .tenantAssetsBaseURL }}/custom/{{ .tenant.CustomCSS | md5 }}.css" />
{{ end }}
{{ end }}
</head>