-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(storage-plugin): access local and session storages globals only i…
…n browser (#2034) This commit modifies the factory functions for the `LOCAL_STORAGE_ENGINE` and `SESSION_STORAGE_ENGINE` tokens. Previously, these functions resolved to `localStorage` and `sessionStorage`, which caused an error when executing the code in a server-side environment where localStorage is not defined. To address this, we have implemented a check to determine if the code is running in a browser environment before retrieving the `localStorage`.
- Loading branch information
Showing
3 changed files
with
74 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { InjectionToken, PLATFORM_ID, inject } from '@angular/core'; | ||
import { isPlatformBrowser } from '@angular/common'; | ||
|
||
import { StorageEngine } from './symbols'; | ||
|
||
export const LOCAL_STORAGE_ENGINE = new InjectionToken<StorageEngine>('LOCAL_STORAGE_ENGINE', { | ||
providedIn: 'root', | ||
factory: () => localStorage | ||
}); | ||
declare const ngDevMode: boolean; | ||
|
||
export const SESSION_STORAGE_ENGINE = new InjectionToken<StorageEngine>( | ||
'SESSION_STORAGE_ENGINE', | ||
const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode; | ||
|
||
export const LOCAL_STORAGE_ENGINE = new InjectionToken<StorageEngine | null>( | ||
NG_DEV_MODE ? 'LOCAL_STORAGE_ENGINE' : '', | ||
{ | ||
providedIn: 'root', | ||
factory: () => (isPlatformBrowser(inject(PLATFORM_ID)) ? localStorage : null) | ||
} | ||
); | ||
|
||
export const SESSION_STORAGE_ENGINE = new InjectionToken<StorageEngine | null>( | ||
NG_DEV_MODE ? 'SESSION_STORAGE_ENGINE' : '', | ||
{ | ||
providedIn: 'root', | ||
factory: () => sessionStorage | ||
factory: () => (isPlatformBrowser(inject(PLATFORM_ID)) ? sessionStorage : null) | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters