From 0b3d82fed50c0078c6ca5c1b09c6111686892513 Mon Sep 17 00:00:00 2001 From: Abeautifulsnow <1491444340@qq.com> Date: Sat, 3 Oct 2026 13:46:14 +0800 Subject: [PATCH] fix(plugins): invalidate workbench html cache on plugin set changes --- .../src/lib/plugins/pluginUiHtmlCache.spec.ts | 31 +++++++++++++++++++ .../src/lib/plugins/pluginUiHtmlCache.ts | 22 +++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/lib/plugins/pluginUiHtmlCache.spec.ts b/apps/desktop/src/lib/plugins/pluginUiHtmlCache.spec.ts index 0f5cf26b66..c52d10ddab 100644 --- a/apps/desktop/src/lib/plugins/pluginUiHtmlCache.spec.ts +++ b/apps/desktop/src/lib/plugins/pluginUiHtmlCache.spec.ts @@ -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("fresh")); + await expect(getOrLoadPluginUiHtml("io.dbx.ssh@0.4.88", "io.dbx.ssh")).resolves.toEqual({ html: "fresh", 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("fresh")); + const freshLoad = getOrLoadPluginUiHtml("io.dbx.ssh@0.4.88", "io.dbx.ssh"); + resolveStale(uiEntryPayload("stale")); + + await expect(staleLoad).resolves.toEqual({ html: "stale", entryDirectory: "" }); + await expect(freshLoad).resolves.toEqual({ html: "fresh", entryDirectory: "" }); + expect(getCachedPluginUiHtml("io.dbx.ssh@0.4.88")).toEqual({ html: "fresh", 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: "" }); diff --git a/apps/desktop/src/lib/plugins/pluginUiHtmlCache.ts b/apps/desktop/src/lib/plugins/pluginUiHtmlCache.ts index 9f26612d33..a94cdbbc6d 100644 --- a/apps/desktop/src/lib/plugins/pluginUiHtmlCache.ts +++ b/apps/desktop/src/lib/plugins/pluginUiHtmlCache.ts @@ -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(); 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 { - 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); +}