From 021f7a031af4f2b8f026e14c5ceb091b7caeeb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=A6=E9=82=A6=E9=82=A6=E9=82=A6?= <15622356989@163.com> Date: Thu, 26 Sep 2024 16:56:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3ssg=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/analytics/src/storage.ts | 11 ++++++++--- packages/analytics/src/utils.ts | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/analytics/src/storage.ts b/packages/analytics/src/storage.ts index b58abe2..5fbe27a 100644 --- a/packages/analytics/src/storage.ts +++ b/packages/analytics/src/storage.ts @@ -1,4 +1,4 @@ -import { isFunction } from './utils'; +import { isClient, isFunction } from './utils'; interface StorageSetOptions { expire?: number; @@ -31,7 +31,9 @@ export class Storage { this.store.setItem(key, JSON.stringify(data)); } remove(key: string) { - this.store.removeItem(key); + if (isClient) { + this.store.removeItem(key); + } } setExpire(key: string, expire: number) { const { value } = this.get(key); @@ -49,7 +51,10 @@ export class Storage { onValid?: (value: any, expire: number) => void; } = {} ) { - const dataStr = this.store.getItem(key); + let dataStr; + if (isClient) { + dataStr = this.store.getItem(key); + } if (!dataStr) { return { value: undefined, diff --git a/packages/analytics/src/utils.ts b/packages/analytics/src/utils.ts index a86fd82..17d9246 100644 --- a/packages/analytics/src/utils.ts +++ b/packages/analytics/src/utils.ts @@ -47,3 +47,8 @@ export function whenWindowLoad(callback: () => any): void { callback(); } } + +/** + * 是否是浏览器环境 + */ +export const isClient = typeof window !== 'undefined'; -- Gitee