fix(plugins): invalidate workbench html cache on plugin set changes

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-10-03 13:46:14 +08:00
1 parent 3a3eb7dd7c
commit 0b3d82fed5
2 files changed
+51 -2

No files matched your search

+31
View File
@@ -11,6 +11,7 @@ vi.mock("@/lib/backend/api", () => ({
readPluginUiAsset: mocks.readPluginUiAsset,
}));
import { COMPONENT_PLUGINS_UPDATED_EVENT } from "@/lib/updates/componentUpdateEvents";
import { clearPluginUiHtmlCache, getCachedPluginUiHtml, getOrLoadPluginUiHtml, setCachedPluginUiHtml } from "./pluginUiHtmlCache";
function uiEntryPayload(html: string): { dataBase64: string } {
@@ -51,6 +52,36 @@ describe("pluginUiHtmlCache", () => {
expect(getCachedPluginUiHtml("io.dbx.ssh@0.4.89")).toBeUndefined();
});
it("invalidates on dbx:plugins-changed so a same-version reinstall re-reads the build", async () => {
setCachedPluginUiHtml("io.dbx.ssh@0.4.88", { html: "stale", entryDirectory: "" });
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
expect(getCachedPluginUiHtml("io.dbx.ssh@0.4.88")).toBeUndefined();
// The next workbench boot pays a fresh bridge read instead of serving stale bytes.
mocks.readPluginUiEntry.mockResolvedValueOnce(uiEntryPayload("<html>fresh</html>"));
await expect(getOrLoadPluginUiHtml("io.dbx.ssh@0.4.88", "io.dbx.ssh")).resolves.toEqual({ html: "<html>fresh</html>", entryDirectory: "" });
});
it("invalidates on the component plugins updated event", () => {
setCachedPluginUiHtml("io.dbx.ssh@0.4.88", { html: "stale", entryDirectory: "" });
window.dispatchEvent(new Event(COMPONENT_PLUGINS_UPDATED_EVENT));
expect(getCachedPluginUiHtml("io.dbx.ssh@0.4.88")).toBeUndefined();
});
it("does not repopulate the cache when an invalidated load completes", async () => {
let resolveStale!: (value: { dataBase64: string }) => void;
mocks.readPluginUiEntry.mockReturnValueOnce(new Promise((resolve) => (resolveStale = resolve)));
const staleLoad = getOrLoadPluginUiHtml("io.dbx.ssh@0.4.88", "io.dbx.ssh");
window.dispatchEvent(new Event("dbx:plugins-changed"));
mocks.readPluginUiEntry.mockResolvedValueOnce(uiEntryPayload("<html>fresh</html>"));
const freshLoad = getOrLoadPluginUiHtml("io.dbx.ssh@0.4.88", "io.dbx.ssh");
resolveStale(uiEntryPayload("<html>stale</html>"));
await expect(staleLoad).resolves.toEqual({ html: "<html>stale</html>", entryDirectory: "" });
await expect(freshLoad).resolves.toEqual({ html: "<html>fresh</html>", entryDirectory: "" });
expect(getCachedPluginUiHtml("io.dbx.ssh@0.4.88")).toEqual({ html: "<html>fresh</html>", entryDirectory: "" });
});
it("getOrLoad resolves from the cache without touching the bridge", async () => {
setCachedPluginUiHtml("io.dbx.ssh@0.4.88", { html: "cached", entryDirectory: "" });
await expect(getOrLoadPluginUiHtml("io.dbx.ssh@0.4.88", "io.dbx.ssh")).resolves.toEqual({ html: "cached", entryDirectory: "" });
@@ -4,6 +4,7 @@
// is shared by every PluginWorkbenchHost instance, so only the first boot of a
// plugin version pays the read/decode/inline pipeline.
import * as api from "@/lib/backend/api";
import { COMPONENT_PLUGINS_UPDATED_EVENT } from "@/lib/updates/componentUpdateEvents";
export interface PluginUiHtml {
html: string;
@@ -17,6 +18,7 @@ export interface PluginUiHtml {
const cache = new Map<string, PluginUiHtml>();
const LIMIT = 4;
let cacheGeneration = 0;
export function getCachedPluginUiHtml(key: string): PluginUiHtml | undefined {
const hit = cache.get(key);
@@ -36,6 +38,7 @@ export function setCachedPluginUiHtml(key: string, value: PluginUiHtml): void {
/** Test and plugin-uninstall escape hatch. */
export function clearPluginUiHtmlCache(): void {
cacheGeneration++;
cache.clear();
inFlight.clear();
}
@@ -125,15 +128,30 @@ export function getOrLoadPluginUiHtml(key: string, pluginId: string): Promise<Pl
if (hit) return Promise.resolve(hit);
let load = inFlight.get(key);
if (!load) {
const generation = cacheGeneration;
load = loadPluginUiHtml(pluginId)
.then((value) => {
setCachedPluginUiHtml(key, value);
// A plugin change may arrive while its old UI document is still being
// read. Do not let that pre-invalidation request repopulate the cache.
if (cacheGeneration === generation) setCachedPluginUiHtml(key, value);
return value;
})
.finally(() => {
inFlight.delete(key);
// A post-invalidation load may already own this key.
if (cacheGeneration === generation) inFlight.delete(key);
});
inFlight.set(key, load);
}
return load;
}
// The plugin set can change from entry points other than the plugin center (the update center
// dispatches COMPONENT_PLUGINS_UPDATED_EVENT from App.vue while the center is closed; batch
// uninstall dispatches only dbx:plugins-changed), and a same-version reinstall reuses the
// id:version key. Invalidate on both events here, at the cache owner — the same contract the
// icon resolver follows — so workbench tabs opened after an install/update/uninstall read the
// new ui build instead of the stale inlined bytes.
if (typeof window !== "undefined") {
window.addEventListener(COMPONENT_PLUGINS_UPDATED_EVENT, clearPluginUiHtmlCache);
window.addEventListener("dbx:plugins-changed", clearPluginUiHtmlCache);
}