From 5e29235c03bd48ad040fbd1b5681d93de5aeabe0 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Mon, 18 Sep 2023 03:24:49 +0900 Subject: [PATCH] feat: use policy cache instead of creating a new policy for each request --- plugins/utils.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/plugins/utils.ts b/plugins/utils.ts index 746c61ef..a8207037 100644 --- a/plugins/utils.ts +++ b/plugins/utils.ts @@ -5,10 +5,23 @@ import { ipcMain, ipcRenderer } from 'electron'; import { ValueOf } from '../utils/type-utils'; -export const noopTrustedHtmlPolicy = () => window?.trustedTypes?.createPolicy('forceInner', { - createHTML: (s: string): string => s, -}) ?? { - createHTML: (s: string): string => s, +import type { TrustedTypePolicy } from 'trusted-types/lib'; + +let policyCache: Pick string }>, 'name' | 'createHTML'> | { + createHTML: (s: string) => string, +}; + +export const noopTrustedHtmlPolicy = () => { + if (policyCache) { + return policyCache; + } else { + policyCache = window?.trustedTypes?.createPolicy('forceInner', { + createHTML: (s: string): string => s, + }) ?? { + createHTML: (s: string): string => s, + }; + return policyCache; + } }; // Creates a DOM element from an HTML string