mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* feat(plugins): introduce logic to selectively load fe plugins via script tags * feat(plugins): extend cache to store isAngular flag. use isAngular in shouldFetch * revert(plugins): remove unused prepareImport from SystemJSWithLoaderHooks type * fix(plugins): cache[path] maybe undefined if not registered or invalidated * Update public/app/features/plugins/plugin_loader.ts Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com> --------- Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { clearPluginSettingsCache } from '../pluginSettings';
|
|
|
|
const cache: Record<string, CacheablePlugin> = {};
|
|
const initializedAt: number = Date.now();
|
|
|
|
type CacheablePlugin = {
|
|
path: string;
|
|
version: string;
|
|
isAngular?: boolean;
|
|
};
|
|
|
|
export function registerPluginInCache({ path, version, isAngular }: CacheablePlugin): void {
|
|
const key = extractPath(path);
|
|
if (key && !cache[key]) {
|
|
cache[key] = {
|
|
version: encodeURI(version),
|
|
isAngular,
|
|
path,
|
|
};
|
|
}
|
|
}
|
|
|
|
export function invalidatePluginInCache(pluginId: string): void {
|
|
const path = `plugins/${pluginId}/module`;
|
|
if (cache[path]) {
|
|
delete cache[path];
|
|
}
|
|
clearPluginSettingsCache(pluginId);
|
|
}
|
|
|
|
export function resolveWithCache(url: string, defaultBust = initializedAt): string {
|
|
const path = extractPath(url);
|
|
if (!path) {
|
|
return `${url}?_cache=${defaultBust}`;
|
|
}
|
|
const version = cache[path]?.version;
|
|
const bust = version || defaultBust;
|
|
return `${url}?_cache=${bust}`;
|
|
}
|
|
|
|
export function getPluginFromCache(path: string): CacheablePlugin | undefined {
|
|
const key = extractPath(path);
|
|
if (!key) {
|
|
return;
|
|
}
|
|
return cache[key];
|
|
}
|
|
|
|
function extractPath(address: string): string | undefined {
|
|
const match = /\/?.+\/(plugins\/.+\/module)\.js/i.exec(address);
|
|
if (!match) {
|
|
return;
|
|
}
|
|
const [_, path] = match;
|
|
if (!path) {
|
|
return;
|
|
}
|
|
return path;
|
|
}
|