From 0c20b6c881b4198f72191c0516d1ae6ff2b4b624 Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Thu, 11 Jun 2026 16:58:57 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20perf(runtime):=20cache=20s?= =?UTF-8?q?tatic=20page-load=20script=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also fix named file:// @resource refresh by checking the parsed resource path. --- src/app/service/service_worker/resource.ts | 38 +- .../service/service_worker/runtime.test.ts | 329 +++++++++++++- src/app/service/service_worker/runtime.ts | 424 +++++++++++------- 3 files changed, 605 insertions(+), 186 deletions(-) diff --git a/src/app/service/service_worker/resource.ts b/src/app/service/service_worker/resource.ts index f78639f12..6184c57bc 100644 --- a/src/app/service/service_worker/resource.ts +++ b/src/app/service/service_worker/resource.ts @@ -83,22 +83,9 @@ export class ResourceService { const ret: { [key: string]: Resource } = {}; await Promise.allSettled( script.metadata[type].map(async (uri) => { - /** 资源键名 */ - let resourceKey = uri; - /** 文件路径 */ - let path: string | null = uri; - if (type === "resource") { - // @resource xxx https://... - const split = uri.split(/\s+/); - if (split.length === 2) { - resourceKey = split[0]; - path = split[1].trim(); - } else { - path = null; - } - } + const { resourceKey, path } = parseResourceMetadataItem(uri, type); if (path) { - if (uri.startsWith("file:///")) { + if (path.startsWith("file:///")) { // 如果是file://协议,则每次请求更新一下文件 const res = await this.updateResource(script.uuid, path, type); ret[resourceKey] = res; @@ -365,3 +352,24 @@ export class ResourceService { }); } } + +export function parseResourceMetadataItem( + uri: string, + type: ResourceType +): { resourceKey: string; path: string | null } { + /** 资源键名 */ + let resourceKey = uri; + /** 文件路径 */ + let path: string | null = uri; + if (type === "resource") { + // @resource xxx https://... + const split = uri.split(/\s+/); + if (split.length === 2) { + resourceKey = split[0]; + path = split[1].trim(); + } else { + path = null; + } + } + return { resourceKey, path }; +} diff --git a/src/app/service/service_worker/runtime.test.ts b/src/app/service/service_worker/runtime.test.ts index 0e29cc5bb..1d9b7fd9f 100644 --- a/src/app/service/service_worker/runtime.test.ts +++ b/src/app/service/service_worker/runtime.test.ts @@ -4,18 +4,19 @@ import { vi, describe, it, expect, beforeEach, type MockedFunction } from "vites import { randomUUID } from "crypto"; import type { Script, ScriptRunResource } from "@App/app/repo/scripts"; import { SCRIPT_STATUS_DISABLE, SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL } from "@App/app/repo/scripts"; -import { getCombinedMeta } from "./utils"; +import { buildScriptRunResourceBasic, getCombinedMeta, scriptURLPatternResults } from "./utils"; import type { SystemConfig } from "@App/pkg/config/config"; import type { Group } from "@Packages/message/server"; import type { ServiceWorkerMessageSend, WindowMessageBody } from "@Packages/message/window_message"; import type { IMessageQueue } from "@Packages/message/message_queue"; import type { ValueService } from "./value"; import type { ScriptService } from "./script"; -import type { ResourceService } from "./resource"; +import { ResourceService } from "./resource"; import type { ScriptDAO } from "@App/app/repo/scripts"; import { LocalStorageDAO } from "@App/app/repo/localStorage"; import type { MessageConnect, TMessage } from "@Packages/message/types"; import { obtainBlackList } from "@App/pkg/utils/utils"; +import type { CompiledResource, Resource, ResourceType } from "@App/app/repo/resource"; initTestEnv(); @@ -389,3 +390,327 @@ describe.concurrent("RuntimeService - getPageScriptMatchingResultByUrl 脚本匹 }); }); }); + +describe("RuntimeService - getScriptsForTab 页面加载静态资料缓存", () => { + const createMockScript = (overrides: Partial