From ba40a4a624e1919109e9f459e25af99a4e849e4a Mon Sep 17 00:00:00 2001 From: sumeyye Date: Mon, 29 Jun 2026 17:15:24 +0300 Subject: [PATCH] fix: storage check for the local storage service --- .../src/lib/services/local-storage.service.ts | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/local-storage.service.ts b/npm/ng-packs/packages/core/src/lib/services/local-storage.service.ts index ebe47955f2..cf0687407d 100644 --- a/npm/ng-packs/packages/core/src/lib/services/local-storage.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/local-storage.service.ts @@ -10,35 +10,36 @@ export class AbpLocalStorageService implements Storage { constructor() { } [name: string]: any; + + private get storage(): Storage | null { + if (!isPlatformBrowser(this.platformId) || typeof window === 'undefined') { + return null; + } + + try { + return window.localStorage; + } catch { + return null; + } + } + get length(): number { - return isPlatformBrowser(this.platformId) ? localStorage.length : 0; + return this.storage?.length || 0; } clear(): void { - if (isPlatformBrowser(this.platformId)) { - localStorage.clear(); - } + this.storage?.clear(); } getItem(key: string): string | null { - if (!isPlatformBrowser(this.platformId)) { - return null; - } - return localStorage.getItem(key); + return this.storage?.getItem(key) || null; } key(index: number): string | null { - if (!isPlatformBrowser(this.platformId)) { - return null; - } - return localStorage.key(index); + return this.storage?.key(index) || null; } removeItem(key: string): void { - if (isPlatformBrowser(this.platformId)) { - localStorage.removeItem(key); - } + this.storage?.removeItem(key); } setItem(key: string, value: string): void { - if (isPlatformBrowser(this.platformId)) { - localStorage.setItem(key, value); - } + this.storage?.setItem(key, value); } }