mirror of https://github.com/grafana/grafana
Chore: Change so we cache loading plugins by its version (#41367)
* making it possible to cache plugins based on the version. * feat(plugincache): introduce function to invalidate entries * removed todo's * added tests for the cache buster. * fixed tests. * fixed failing tests. Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>pull/41530/head
parent
baab021fec
commit
e5421dd53e
@ -0,0 +1,42 @@ |
||||
import { invalidatePluginInCache, locateWithCache, registerPluginInCache } from './pluginCacheBuster'; |
||||
|
||||
describe('PluginCacheBuster', () => { |
||||
const now = 12345; |
||||
|
||||
it('should append plugin version as cache flag if plugin is registered in buster', () => { |
||||
const slug = 'bubble-chart-1'; |
||||
const version = 'v1.0.0'; |
||||
const path = resolvePath(slug); |
||||
const address = `http://localhost:3000/public/${path}.js`; |
||||
|
||||
registerPluginInCache({ path, version }); |
||||
|
||||
const url = `${address}?_cache=${encodeURI(version)}`; |
||||
expect(locateWithCache({ address }, now)).toBe(url); |
||||
}); |
||||
|
||||
it('should append Date.now as cache flag if plugin is not registered in buster', () => { |
||||
const slug = 'bubble-chart-2'; |
||||
const address = `http://localhost:3000/public/${resolvePath(slug)}.js`; |
||||
|
||||
const url = `${address}?_cache=${encodeURI(String(now))}`; |
||||
expect(locateWithCache({ address }, now)).toBe(url); |
||||
}); |
||||
|
||||
it('should append Date.now as cache flag if plugin is invalidated in buster', () => { |
||||
const slug = 'bubble-chart-3'; |
||||
const version = 'v1.0.0'; |
||||
const path = resolvePath(slug); |
||||
const address = `http://localhost:3000/public/${path}.js`; |
||||
|
||||
registerPluginInCache({ path, version }); |
||||
invalidatePluginInCache(slug); |
||||
|
||||
const url = `${address}?_cache=${encodeURI(String(now))}`; |
||||
expect(locateWithCache({ address }, now)).toBe(url); |
||||
}); |
||||
}); |
||||
|
||||
function resolvePath(slug: string): string { |
||||
return `plugins/${slug}/module`; |
||||
} |
@ -0,0 +1,45 @@ |
||||
const cache: Record<string, string> = {}; |
||||
const initializedAt: number = Date.now(); |
||||
|
||||
type CacheablePlugin = { |
||||
path: string; |
||||
version: string; |
||||
}; |
||||
|
||||
export function registerPluginInCache({ path, version }: CacheablePlugin): void { |
||||
if (!cache[path]) { |
||||
cache[path] = encodeURI(version); |
||||
} |
||||
} |
||||
|
||||
export function invalidatePluginInCache(pluginId: string): void { |
||||
const path = `plugins/${pluginId}/module`; |
||||
if (cache[path]) { |
||||
delete cache[path]; |
||||
} |
||||
} |
||||
|
||||
export function locateWithCache(load: { address: string }, defaultBust = initializedAt): string { |
||||
const { address } = load; |
||||
const path = extractPath(address); |
||||
|
||||
if (!path) { |
||||
return `${address}?_cache=${defaultBust}`; |
||||
} |
||||
|
||||
const version = cache[path]; |
||||
const bust = version || defaultBust; |
||||
return `${address}?_cache=${bust}`; |
||||
} |
||||
|
||||
function extractPath(address: string): string | undefined { |
||||
const match = /\/public\/(plugins\/.+\/module)\.js/i.exec(address); |
||||
if (!match) { |
||||
return; |
||||
} |
||||
const [_, path] = match; |
||||
if (!path) { |
||||
return; |
||||
} |
||||
return path; |
||||
} |
Loading…
Reference in new issue