Files
grafana/public/app/features/plugins/loader/cache.ts
Jack Westbrook 036c878843 Plugins: Improve frontend loader cache (#87488)
* do it

* set empty child version to parent version

* feat(plugins): use pluginId for loader cache keys

* feat(plugins): apply caching to all js and css files systemjs loads

* remove old code and add comment

* test(plugins): update systemjs hooks tests in line with better caching

* test(plugins): wip - comment out failing backend loader tests

* fix tests and improve comment

* Update public/app/features/plugins/loader/cache.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

---------

Co-authored-by: Will Browne <will.browne@grafana.com>
Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
2024-06-07 10:03:41 +02:00

57 lines
1.3 KiB
TypeScript

import { clearPluginSettingsCache } from '../pluginSettings';
import { CACHE_INITIALISED_AT } from './constants';
const cache: Record<string, CacheablePlugin> = {};
type CacheablePlugin = {
pluginId: string;
version: string;
isAngular?: boolean;
};
export function registerPluginInCache({ pluginId, version, isAngular }: CacheablePlugin): void {
const key = pluginId;
if (key && !cache[key]) {
cache[key] = {
version: encodeURI(version),
isAngular,
pluginId,
};
}
}
export function invalidatePluginInCache(pluginId: string): void {
const path = pluginId;
if (cache[path]) {
delete cache[path];
}
clearPluginSettingsCache(pluginId);
}
export function resolveWithCache(url: string, defaultBust = CACHE_INITIALISED_AT): string {
const path = getCacheKey(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 = getCacheKey(path);
if (!key) {
return;
}
return cache[key];
}
function getCacheKey(address: string): string | undefined {
const key = Object.keys(cache).find((key) => address.includes(key));
if (!key) {
return;
}
return key;
}