From 0469b0f52ed0ed7ecd6b669a34cbbdec1dc0727f Mon Sep 17 00:00:00 2001 From: Will Browne Date: Wed, 11 Sep 2024 14:35:13 +0100 Subject: [PATCH] Plugins: Ensure `fetch` loading strategy cases for nested plugins (#93209) * fix * fix func name --- .../pluginassets/pluginassets.go | 15 ++++++-- .../pluginassets/pluginassets_test.go | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/pkg/services/pluginsintegration/pluginassets/pluginassets.go b/pkg/services/pluginsintegration/pluginassets/pluginassets.go index a92aabecb6f..aca1e536a12 100644 --- a/pkg/services/pluginsintegration/pluginassets/pluginassets.go +++ b/pkg/services/pluginsintegration/pluginassets/pluginassets.go @@ -46,16 +46,23 @@ func (s *Service) LoadingStrategy(_ context.Context, p pluginstore.Plugin) plugi } } - // If the plugin has a parent, check the parent's create_plugin_version setting + // If the plugin has a parent if p.Parent != nil { + // Check the parent's create_plugin_version setting if pCfg, ok := s.cfg.PluginSettings[p.Parent.ID]; ok { if s.compatibleCreatePluginVersion(pCfg) { return plugins.LoadingStrategyScript } } + + // Since the parent plugin is not explicitly configured as script loading compatible, + // If the plugin is either loaded from the CDN (via its parent) or contains Angular, we should use fetch + if s.cdnEnabled(p.Parent.ID, p.Class) || p.Angular.Detected { + return plugins.LoadingStrategyFetch + } } - if !s.cndEnabled(p) && !p.Angular.Detected { + if !s.cdnEnabled(p.ID, p.Class) && !p.Angular.Detected { return plugins.LoadingStrategyScript } @@ -76,6 +83,6 @@ func (s *Service) compatibleCreatePluginVersion(ps map[string]string) bool { return false } -func (s *Service) cndEnabled(p pluginstore.Plugin) bool { - return s.cdn.PluginSupported(p.ID) || p.Class == plugins.ClassCDN +func (s *Service) cdnEnabled(pluginID string, class plugins.Class) bool { + return s.cdn.PluginSupported(pluginID) || class == plugins.ClassCDN } diff --git a/pkg/services/pluginsintegration/pluginassets/pluginassets_test.go b/pkg/services/pluginsintegration/pluginassets/pluginassets_test.go index 3ca9c4bbccb..26f7fa181a5 100644 --- a/pkg/services/pluginsintegration/pluginassets/pluginassets_test.go +++ b/pkg/services/pluginsintegration/pluginassets/pluginassets_test.go @@ -76,6 +76,43 @@ func TestService_Calculate(t *testing.T) { }), expected: plugins.LoadingStrategyScript, }, + { + name: "Expected LoadingStrategyFetch when parent create-plugin version is not set, is configured as CDN enabled and plugin is not angular", + pluginSettings: setting.PluginSettings{ + "parent-datasource": { + "cdn": "true", + }, + }, + plugin: newPlugin(pluginID, false, func(p pluginstore.Plugin) pluginstore.Plugin { + p.Parent = &pluginstore.ParentPlugin{ID: "parent-datasource"} + return p + }), + expected: plugins.LoadingStrategyFetch, + }, + { + name: "Expected LoadingStrategyFetch when parent create-plugin version is not set, is configured as CDN enabled and plugin is angular", + pluginSettings: setting.PluginSettings{ + "parent-datasource": { + "cdn": "true", + }, + }, + plugin: newPlugin(pluginID, false, func(p pluginstore.Plugin) pluginstore.Plugin { + p.Angular.Detected = true + p.Parent = &pluginstore.ParentPlugin{ID: "parent-datasource"} + return p + }), + expected: plugins.LoadingStrategyFetch, + }, + { + name: "Expected LoadingStrategyFetch when parent create-plugin version is not set, is not configured as CDN enabled and plugin is angular", + pluginSettings: setting.PluginSettings{}, + plugin: newPlugin(pluginID, false, func(p pluginstore.Plugin) pluginstore.Plugin { + p.Angular.Detected = true + p.Parent = &pluginstore.ParentPlugin{ID: "parent-datasource"} + return p + }), + expected: plugins.LoadingStrategyFetch, + }, { name: "Expected LoadingStrategyFetch when create-plugin version is not compatible, plugin is not angular, is configured as CDN enabled and does not have the CDN class", pluginSettings: newPluginSettings(pluginID, map[string]string{