Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(storage-plugin): use ngServerMode to check whether we are in SSR #2288

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $ npm install @ngxs/store@dev
- Fix(store): Complete action results on destroy [#2282](https://github.com/ngxs/store/pull/2282)
- Fix(store): Complete `dispatched$` in internal actions [#2285](https://github.com/ngxs/store/pull/2285)
- Build(store): Use `ngServerMode` to check whether we are in SSR [#2287](https://github.com/ngxs/store/pull/2287)
- Build(storage-plugin): Use `ngServerMode` to check whether we are in SSR [#2288](https://github.com/ngxs/store/pull/2288)
- Refactor(form-plugin): Replace `takeUntil` with `takeUntilDestroyed` [#2283](https://github.com/ngxs/store/pull/2283)

### 19.0.0 2024-12-3
Expand Down
9 changes: 5 additions & 4 deletions packages/storage-plugin/src/engines.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { InjectionToken, PLATFORM_ID, inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { InjectionToken } from '@angular/core';
import { StorageEngine } from '@ngxs/storage-plugin/internals';

declare const ngDevMode: boolean;
declare const ngServerMode: boolean;

export const LOCAL_STORAGE_ENGINE = /* @__PURE__ */ new InjectionToken<StorageEngine | null>(
typeof ngDevMode !== 'undefined' && ngDevMode ? 'LOCAL_STORAGE_ENGINE' : '',
{
providedIn: 'root',
factory: () => (isPlatformBrowser(inject(PLATFORM_ID)) ? localStorage : null)
factory: () => (typeof ngServerMode !== 'undefined' && ngServerMode ? null : localStorage)
}
);

export const SESSION_STORAGE_ENGINE = /* @__PURE__ */ new InjectionToken<StorageEngine | null>(
typeof ngDevMode !== 'undefined' && ngDevMode ? 'SESSION_STORAGE_ENGINE' : '',
{
providedIn: 'root',
factory: () => (isPlatformBrowser(inject(PLATFORM_ID)) ? sessionStorage : null)
factory: () =>
typeof ngServerMode !== 'undefined' && ngServerMode ? null : sessionStorage
}
);
10 changes: 4 additions & 6 deletions packages/storage-plugin/src/internals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isPlatformServer } from '@angular/common';
import {
ɵDEFAULT_STATE_KEY,
StorageOption,
Expand All @@ -7,6 +6,8 @@ import {
ɵNgxsTransformedStoragePluginOptions
} from '@ngxs/storage-plugin/internals';

declare const ngServerMode: boolean;

export function storageOptionsFactory(
options: NgxsStoragePluginOptions
): ɵNgxsTransformedStoragePluginOptions {
Expand All @@ -21,11 +22,8 @@ export function storageOptionsFactory(
};
}

export function engineFactory(
options: NgxsStoragePluginOptions,
platformId: string
): StorageEngine | null {
if (isPlatformServer(platformId)) {
export function engineFactory(options: NgxsStoragePluginOptions): StorageEngine | null {
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
return null;
}

Expand Down
5 changes: 2 additions & 3 deletions packages/storage-plugin/src/storage.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
NgModule,
ModuleWithProviders,
PLATFORM_ID,
EnvironmentProviders,
makeEnvironmentProviders
} from '@angular/core';
Expand Down Expand Up @@ -37,7 +36,7 @@ export class NgxsStoragePluginModule {
{
provide: STORAGE_ENGINE,
useFactory: engineFactory,
deps: [ɵNGXS_STORAGE_PLUGIN_OPTIONS, PLATFORM_ID]
deps: [ɵNGXS_STORAGE_PLUGIN_OPTIONS]
}
]
};
Expand All @@ -61,7 +60,7 @@ export function withNgxsStoragePlugin(
{
provide: STORAGE_ENGINE,
useFactory: engineFactory,
deps: [ɵNGXS_STORAGE_PLUGIN_OPTIONS, PLATFORM_ID]
deps: [ɵNGXS_STORAGE_PLUGIN_OPTIONS]
}
]);
}
7 changes: 3 additions & 4 deletions packages/storage-plugin/src/storage.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PLATFORM_ID, Injectable, inject } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { Injectable, inject } from '@angular/core';
import { ɵPlainObject } from '@ngxs/store/internals';
import {
NgxsPlugin,
Expand All @@ -21,16 +20,16 @@ import { getStorageKey } from './internals';
import { ɵNgxsStoragePluginKeysManager } from './keys-manager';

declare const ngDevMode: boolean;
declare const ngServerMode: boolean;

@Injectable()
export class NgxsStoragePlugin implements NgxsPlugin {
private _keysManager = inject(ɵNgxsStoragePluginKeysManager);
private _options = inject(ɵNGXS_STORAGE_PLUGIN_OPTIONS);
private _allStatesPersisted = inject(ɵALL_STATES_PERSISTED);
private _isServer = isPlatformServer(inject(PLATFORM_ID));

handle(state: any, event: any, next: NgxsNextPluginFn) {
if (this._isServer) {
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
return next(state, event);
}

Expand Down
Loading