Sandbox: Use resolveCache to resolve plugin's code file as systemjs does (#75509)

* Sandbox: Use resolveCache to resolve plugin's code file as systemjs does

* Add clarifying comments
This commit is contained in:
Esteban Beltran 2023-09-27 13:56:35 +02:00 committed by GitHub
parent 720543f483
commit 2bbd5521fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
import { PluginMeta } from '@grafana/data';
import { transformPluginSourceForCDN } from '../cdn/utils';
import { resolveWithCache } from '../loader/cache';
import { isHostedOnCDN } from '../loader/utils';
import { SandboxEnvironment } from './types';
@ -40,7 +41,7 @@ export async function loadScriptIntoSandbox(url: string, meta: PluginMeta, sandb
export async function getPluginCode(meta: PluginMeta): Promise<string> {
if (isHostedOnCDN(meta.module)) {
// should load plugin from a CDN
// Load plugin from CDN, no need for "resolveWithCache" as CDN URLs already include the version
const url = meta.module;
const response = await fetch(url);
let pluginCode = await response.text();
@ -51,8 +52,10 @@ export async function getPluginCode(meta: PluginMeta): Promise<string> {
});
return pluginCode;
} else {
//local plugin loading
const response = await fetch(meta.module);
// local plugin. resolveWithCache will append a query parameter with its version
// to ensure correct cached version is served
const pluginCodeUrl = resolveWithCache(meta.module);
const response = await fetch(pluginCodeUrl);
let pluginCode = await response.text();
pluginCode = patchPluginSourceMap(meta, pluginCode);
pluginCode = patchPluginAPIs(pluginCode);