Skip to content

Commit

Permalink
fix nextjs localstorage issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chakra-guy committed Jan 13, 2025
1 parent f4dacaa commit 090df3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
16 changes: 12 additions & 4 deletions packages/sdk/src/Platform/PlatfformManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class PlatformManager {
return this.state.platformType === PlatformType.MobileWeb;
}

isNotBrowser() {
static isNotBrowser() {
return (
typeof window === 'undefined' ||
!window?.navigator ||
Expand All @@ -99,14 +99,22 @@ export class PlatformManager {
);
}

isNodeJS() {
return this.isNotBrowser() && !this.isReactNative();
isNotBrowser() {
return PlatformManager.isNotBrowser();
}

isBrowser() {
static isBrowser() {
return !this.isNotBrowser();
}

isBrowser() {
return PlatformManager.isBrowser();
}

isNodeJS() {
return this.isNotBrowser() && !this.isReactNative();
}

isUseDeepLink() {
return this.state.useDeeplink;
}
Expand Down
24 changes: 16 additions & 8 deletions packages/sdk/src/storage-manager/getStorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
StorageManager,
StorageManagerProps,
} from '@metamask/sdk-communication-layer';
import { PlatformManager } from 'src/Platform/PlatfformManager';

/* #if _NODEJS
import { StorageManagerNode as SMDyn } from './StorageManagerNode';
Expand All @@ -17,14 +18,21 @@ import { StorageManagerAS as SMDyn } from './StorageManagerAS';
//#endif

export const getStorageManager = (
// platformManager: PlatformManager,
options: StorageManagerProps,
): StorageManager => {
// TODO uncomment and test to use similar dynamic imports for each platforms and drop support for JSCC
// Currently might have an issue with NextJS and server side rendering
// if (platformManager.isNotBrowser()) {
// const { StorageManagerNode } = await import('./StorageManagerNode');
// return new StorageManagerNode(options);
// }
return new SMDyn(options);
if (PlatformManager.isBrowser()) {
return new SMDyn(options);
}

const noopStorageManager: StorageManager = {
persistChannelConfig: async () => undefined,
getPersistedChannelConfig: async () => undefined,
persistAccounts: async () => undefined,
getCachedAccounts: async () => [],
persistChainId: async () => undefined,
getCachedChainId: async () => undefined,
terminate: async () => undefined,
} as StorageManager;

return noopStorageManager;
};

0 comments on commit 090df3b

Please sign in to comment.