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