Skip to content

Commit ea14232

Browse files
authored
Fix nextjs localstorage issue (#1193)
* fix nextjs localstorage issue * pr comments * fix test
1 parent 801c9ef commit ea14232

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

packages/sdk/src/Platform/PlatfformManager.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,12 @@ describe('PlatformManager', () => {
407407
});
408408

409409
it('should return true if isNotBrowser returns false', () => {
410-
jest.spyOn(platformManager, 'isNotBrowser').mockReturnValue(false);
411-
410+
jest.spyOn(PlatformManager, 'isNotBrowser').mockReturnValue(false);
412411
expect(platformManager.isBrowser()).toBe(true);
413412
});
414413

415414
it('should return false if isNotBrowser returns true', () => {
416-
jest.spyOn(platformManager, 'isNotBrowser').mockReturnValue(true);
417-
415+
jest.spyOn(PlatformManager, 'isNotBrowser').mockReturnValue(true);
418416
expect(platformManager.isBrowser()).toBe(false);
419417
});
420418
});

packages/sdk/src/Platform/PlatfformManager.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class PlatformManager {
8989
return this.state.platformType === PlatformType.MobileWeb;
9090
}
9191

92-
isNotBrowser() {
92+
static isNotBrowser() {
9393
return (
9494
typeof window === 'undefined' ||
9595
!window?.navigator ||
@@ -99,14 +99,22 @@ export class PlatformManager {
9999
);
100100
}
101101

102-
isNodeJS() {
103-
return this.isNotBrowser() && !this.isReactNative();
102+
isNotBrowser() {
103+
return PlatformManager.isNotBrowser();
104104
}
105105

106-
isBrowser() {
106+
static isBrowser() {
107107
return !this.isNotBrowser();
108108
}
109109

110+
isBrowser() {
111+
return PlatformManager.isBrowser();
112+
}
113+
114+
isNodeJS() {
115+
return this.isNotBrowser() && !this.isReactNative();
116+
}
117+
110118
isUseDeepLink() {
111119
return this.state.useDeeplink;
112120
}

packages/sdk/src/services/MetaMaskSDK/InitializerManager/setupStorage.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ export async function setupStorageManager(instance: MetaMaskSDK) {
1515
const { options } = instance;
1616

1717
if (options.storage?.enabled === true && !options.storage.storageManager) {
18-
options.storage.storageManager = getStorageManager(
19-
// instance.platformManager,
20-
options.storage,
21-
);
18+
options.storage.storageManager = await getStorageManager(options.storage);
2219
}
2320
}

packages/sdk/src/services/SDKProvider/InitializationManager/initializeStateAsync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function initializeStateAsync(instance: SDKProvider) {
5050
let relayPersistence = false;
5151

5252
let useCache = false;
53-
const storageManager = getStorageManager({ enabled: true });
53+
const storageManager = await getStorageManager({ enabled: true });
5454

5555
// FIXME: currently set for backward compatibility so new sdk don't autoconnect with old wallet
5656
// Only use cache if relayPersistence is enabled for current channel.

packages/sdk/src/storage-manager/getStorageManager.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,25 @@ import {
33
StorageManager,
44
StorageManagerProps,
55
} from '@metamask/sdk-communication-layer';
6+
import { PlatformManager } from '../Platform/PlatfformManager';
67

7-
/* #if _NODEJS
8-
import { StorageManagerNode as SMDyn } from './StorageManagerNode';
9-
//#elif _WEB
10-
import { StorageManagerWeb as SMDyn } from './StorageManagerWeb';
11-
//#elif _REACTNATIVE
12-
import { StorageManagerAS as SMDyn } from './StorageManagerAS';
13-
//#else */
14-
// This is ONLY used during development with devnext/devreactnative or via transpiling
15-
import { StorageManagerAS as SMDyn } from './StorageManagerAS';
16-
// eslint-disable-next-line spaced-comment
17-
//#endif
18-
19-
export const getStorageManager = (
20-
// platformManager: PlatformManager,
8+
export const getStorageManager = async (
219
options: StorageManagerProps,
22-
): StorageManager => {
23-
// TODO uncomment and test to use similar dynamic imports for each platforms and drop support for JSCC
24-
// Currently might have an issue with NextJS and server side rendering
25-
// if (platformManager.isNotBrowser()) {
26-
// const { StorageManagerNode } = await import('./StorageManagerNode');
27-
// return new StorageManagerNode(options);
28-
// }
29-
return new SMDyn(options);
10+
): Promise<StorageManager> => {
11+
if (PlatformManager.isBrowser()) {
12+
const { StorageManagerWeb } = await import('./StorageManagerWeb');
13+
return new StorageManagerWeb(options);
14+
}
15+
16+
const noopStorageManager: StorageManager = {
17+
persistChannelConfig: async () => undefined,
18+
getPersistedChannelConfig: async () => undefined,
19+
persistAccounts: async () => undefined,
20+
getCachedAccounts: async () => [],
21+
persistChainId: async () => undefined,
22+
getCachedChainId: async () => undefined,
23+
terminate: async () => undefined,
24+
} as StorageManager;
25+
26+
return Promise.resolve(noopStorageManager);
3027
};

0 commit comments

Comments
 (0)