Plugins: Ensure fetch loading strategy cases for nested plugins (#93209)

* fix

* fix func name
This commit is contained in:
Will Browne 2024-09-11 14:35:13 +01:00 committed by GitHub
parent f5926af99a
commit 0469b0f52e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 4 deletions

View File

@ -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
}

View File

@ -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{