Compare commits

...

2 Commits

Author SHA1 Message Date
Vinícius Lourenço
997883c7a5 build(web): pin hashed asset names that the immutable cache relies on 2026-11-23 21:34:01 -03:00
Vinícius Lourenço
f778025bad perf(web): cache hashed frontend assets as immutable 2026-11-22 01:32:31 -03:00
4 changed files with 105 additions and 0 deletions

3
frontend/.gitignore vendored
View File

@@ -1,4 +1,7 @@
# Vite copies public/ unhashed, and the Go server caches /assets/ as immutable
/public/assets/
# Sentry Config File
.env.sentry-build-plugin
.qodo

View File

@@ -188,6 +188,15 @@ export default defineConfig(({ mode }): UserConfig => {
sourcemap: true,
outDir: 'build',
cssMinify: 'esbuild',
// The Go server caches /assets/ as immutable, so every file there must carry a content hash.
assetsDir: 'assets',
rolldownOptions: {
output: {
entryFileNames: 'assets/[name]-[hash].js',
chunkFileNames: 'assets/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash].[ext]',
},
},
},
server: {
open: true,

View File

@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/factory"
@@ -16,6 +17,11 @@ import (
"github.com/gorilla/mux"
)
const (
assetsPathPrefix = "/assets/"
immutableCacheControl = "public, max-age=31536000, immutable"
)
type provider struct {
config web.Config
indexContents []byte
@@ -101,6 +107,11 @@ func (provider *provider) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
// files under /assets/ are content-hashed, so their URL changes with their content
if strings.HasPrefix(req.URL.Path, assetsPathPrefix) {
rw.Header().Set("Cache-Control", immutableCacheControl)
}
// otherwise, use http.FileServer to serve the static file
provider.fileHandler.ServeHTTP(rw, req)
}

View File

@@ -6,6 +6,7 @@ import (
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
@@ -251,3 +252,84 @@ func TestServeStaticFilesUnchanged(t *testing.T) {
assert.Equal(t, string(expected), httpGet(t, base+"/assets/style.css"))
}
func TestServeCacheControl(t *testing.T) {
t.Parallel()
const (
noCache = "max-age=0,no-cache,private,must-revalidate"
immutable = "public, max-age=31536000, immutable"
)
directory := t.TempDir()
files := map[string]string{
"index.html": "<html>index</html>",
"assets/x.js": "console.log('x')",
"assets/nested/y.css": "body{}",
"assets-legacy/z.js": "console.log('z')",
"favicon.ico": "icon",
"css/uPlot.min.css": ".uplot{}",
"locales/en/main.json": "{}",
}
for name, content := range files {
require.NoError(t, os.MkdirAll(filepath.Join(directory, filepath.Dir(name)), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(directory, name), []byte(content), 0o644))
}
testCases := []struct {
name string
basePath string
path string
expectedBody string
expectedCC string
}{
{name: "ExistingAsset_Immutable", path: "/assets/x.js", expectedBody: files["assets/x.js"], expectedCC: immutable},
{name: "ExistingNestedAsset_Immutable", path: "/assets/nested/y.css", expectedBody: files["assets/nested/y.css"], expectedCC: immutable},
{name: "MissingAsset_ServesIndex_NoCache", path: "/assets/missing-AAAA.js", expectedBody: files["index.html"], expectedCC: noCache},
{name: "AssetsDirectory_ServesIndex_NoCache", path: "/assets/nested/", expectedBody: files["index.html"], expectedCC: noCache},
{name: "Root_ServesIndex_NoCache", path: "/", expectedBody: files["index.html"], expectedCC: noCache},
{name: "UnknownRoute_ServesIndex_NoCache", path: "/does-not-exist", expectedBody: files["index.html"], expectedCC: noCache},
{name: "NonAssetFile_NoCache", path: "/favicon.ico", expectedBody: files["favicon.ico"], expectedCC: noCache},
{name: "NonAssetNestedFile_NoCache", path: "/css/uPlot.min.css", expectedBody: files["css/uPlot.min.css"], expectedCC: noCache},
{name: "LocaleFile_NoCache", path: "/locales/en/main.json", expectedBody: files["locales/en/main.json"], expectedCC: noCache},
{name: "AssetsPrefixLookalike_NoCache", path: "/assets-legacy/z.js", expectedBody: files["assets-legacy/z.js"], expectedCC: noCache},
{name: "BasePath_ExistingAsset_Immutable", basePath: "/signoz", path: "/assets/x.js", expectedBody: files["assets/x.js"], expectedCC: immutable},
{name: "BasePath_MissingAsset_ServesIndex_NoCache", basePath: "/signoz", path: "/assets/missing-AAAA.js", expectedBody: files["index.html"], expectedCC: noCache},
{name: "BasePath_NonAssetFile_NoCache", basePath: "/signoz", path: "/favicon.ico", expectedBody: files["favicon.ico"], expectedCC: noCache},
{name: "BasePath_Root_ServesIndex_NoCache", basePath: "/signoz", path: "/", expectedBody: files["index.html"], expectedCC: noCache},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
routerWeb, err := New(
context.Background(),
factorytest.NewSettings(),
web.Config{Index: "index.html", Directory: directory},
global.Config{ExternalURL: &url.URL{Path: testCase.basePath}},
)
require.NoError(t, err)
router := mux.NewRouter().UseEncodedPath()
require.NoError(t, routerWeb.AddToRouter(router))
var handler http.Handler = router
if testCase.basePath != "" {
handler = http.StripPrefix(testCase.basePath, router)
}
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
res, err := http.Get(server.URL + testCase.basePath + testCase.path)
require.NoError(t, err)
defer func() { _ = res.Body.Close() }()
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, testCase.expectedCC, res.Header.Get("Cache-Control"))
assert.Equal(t, testCase.expectedBody, string(body))
})
}
}