grafana/public/app/features/plugins/loader/cache.ts
Jack Westbrook fd1bf66d86
Plugins: Selectively load plugins using script tags (#85750)
* 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>
2024-04-24 12:27:28 +02:00

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