Plugins: Improve frontend loader cache (#87488)

* do it

* set empty child version to parent version

* feat(plugins): use pluginId for loader cache keys

* feat(plugins): apply caching to all js and css files systemjs loads

* remove old code and add comment

* test(plugins): update systemjs hooks tests in line with better caching

* test(plugins): wip - comment out failing backend loader tests

* fix tests and improve comment

* Update public/app/features/plugins/loader/cache.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

---------

Co-authored-by: Will Browne <will.browne@grafana.com>
Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
This commit is contained in:
Jack Westbrook
2024-06-07 10:03:41 +02:00
committed by GitHub
co-authored by Levente Balogh Will Browne
parent 897b81e566
commit 036c878843
9 changed files with 98 additions and 87 deletions
@@ -4,7 +4,6 @@ import (
"context"
"path"
"slices"
"strings"
"github.com/grafana/grafana/pkg/infra/slugify"
"github.com/grafana/grafana/pkg/plugins"
@@ -144,14 +143,11 @@ func configureAppChildPlugin(parent *plugins.Plugin, child *plugins.Plugin) {
return
}
child.IncludedInAppID = parent.ID
child.BaseURL = parent.BaseURL
// TODO move this logic within assetpath package
appSubPath := strings.ReplaceAll(strings.Replace(child.FS.Base(), parent.FS.Base(), "", 1), "\\", "/")
if parent.IsCorePlugin() {
child.Module = path.Join("core:plugin", parent.ID, appSubPath)
} else {
child.Module = path.Join("public/plugins", parent.ID, appSubPath, "module.js")
// If the child plugin does not have a version, it will inherit the version from the parent.
// This is to ensure that the frontend can appropriately cache the plugin assets.
if child.Info.Version == "" {
child.Info.Version = parent.Info.Version
}
}
@@ -96,7 +96,7 @@ func TestTemplateDecorateFunc(t *testing.T) {
}
func Test_configureAppChildPlugin(t *testing.T) {
t.Run("When setting paths based on core plugin on Windows", func(t *testing.T) {
t.Run("Child plugin will inherit parent version information when version is empty", func(t *testing.T) {
child := &plugins.Plugin{
FS: fakes.NewFakePluginFiles("c:\\grafana\\public\\app\\plugins\\app\\testdata-app\\datasources\\datasource"),
}
@@ -104,6 +104,7 @@ func Test_configureAppChildPlugin(t *testing.T) {
JSONData: plugins.JSONData{
Type: plugins.TypeApp,
ID: "testdata-app",
Info: plugins.Info{Version: "1.0.0"},
},
Class: plugins.ClassCore,
FS: fakes.NewFakePluginFiles("c:\\grafana\\public\\app\\plugins\\app\\testdata-app"),
@@ -112,19 +113,22 @@ func Test_configureAppChildPlugin(t *testing.T) {
configureAppChildPlugin(parent, child)
require.Equal(t, "core:plugin/testdata-app/datasources/datasource", child.Module)
require.Equal(t, "testdata-app", child.IncludedInAppID)
require.Equal(t, "public/app/plugins/app/testdata-app", child.BaseURL)
require.Equal(t, parent.Info.Version, child.Info.Version)
})
t.Run("When setting paths based on external plugin", func(t *testing.T) {
t.Run("Child plugin will not inherit parent version information when version is non-empty", func(t *testing.T) {
child := &plugins.Plugin{
FS: fakes.NewFakePluginFiles("/plugins/parent-app/child-panel"),
JSONData: plugins.JSONData{
Info: plugins.Info{Version: "2.0.2"},
},
}
parent := &plugins.Plugin{
JSONData: plugins.JSONData{
Type: plugins.TypeApp,
ID: "testdata-app",
Info: plugins.Info{Version: "2.0.0"},
},
Class: plugins.ClassExternal,
FS: fakes.NewFakePluginFiles("/plugins/parent-app"),
@@ -133,9 +137,8 @@ func Test_configureAppChildPlugin(t *testing.T) {
configureAppChildPlugin(parent, child)
require.Equal(t, "public/plugins/testdata-app/child-panel/module.js", child.Module)
require.Equal(t, "testdata-app", child.IncludedInAppID)
require.Equal(t, "plugins/parent-app", child.BaseURL)
require.NotEqual(t, parent.Info.Version, child.Info.Version)
})
}