Plugins: Fix Cache-Control header response for public/plugins/* assets API (#64051)

* fix caching

* fixeroo

* simplify

* undo changes

* fix test
This commit is contained in:
Will Browne 2023-03-06 17:42:18 +00:00 committed by GitHub
parent b9ebd5757a
commit 88666f9bbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 1 deletions

View File

@ -37,7 +37,8 @@ func AddDefaultResponseHeaders(cfg *setting.Cfg) web.Handler {
_, _, resourceURLMatch := t.Match(c.Req.URL.Path)
resourceCachable := resourceURLMatch && allowCacheControl(c.Resp)
if !strings.HasPrefix(c.Req.URL.Path, "/api/datasources/proxy/") && !resourceCachable {
if !strings.HasPrefix(c.Req.URL.Path, "/public/plugins/") &&
!strings.HasPrefix(c.Req.URL.Path, "/api/datasources/proxy/") && !resourceCachable {
addNoCacheHeaders(c.Resp)
}

View File

@ -20,6 +20,7 @@ import (
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testinfra"
)
@ -117,6 +118,57 @@ func TestIntegrationPlugins(t *testing.T) {
})
}
func TestIntegrationPluginAssets(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
type testCase struct {
desc string
url string
env string
expStatus int
expCacheControl string
}
t.Run("Assets", func(t *testing.T) {
testCases := []testCase{
{
desc: "should return no-cache settings for Dev env",
env: setting.Dev,
url: "http://%s/public/plugins/testdata/img/testdata.svg",
expStatus: http.StatusOK,
expCacheControl: "max-age=0, must-revalidate, no-cache",
},
{
desc: "should return cache settings for Prod env",
env: setting.Prod,
url: "http://%s/public/plugins/testdata/img/testdata.svg",
expStatus: http.StatusOK,
expCacheControl: "public, max-age=3600",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
dir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
AppModeProduction: tc.env == setting.Prod,
})
grafanaListedAddr, _ := testinfra.StartGrafana(t, dir, cfgPath)
url := fmt.Sprintf(tc.url, grafanaListedAddr)
// nolint:gosec
resp, err := http.Get(url)
t.Cleanup(func() {
require.NoError(t, resp.Body.Close())
})
require.NoError(t, err)
require.Equal(t, tc.expStatus, resp.StatusCode)
require.Equal(t, tc.expCacheControl, resp.Header.Get("Cache-Control"))
})
}
})
}
func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserCommand) {
t.Helper()