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

Fix nextjs localstorage issue #1193

Merged
merged 4 commits into from
Jan 14, 2025
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
6 changes: 2 additions & 4 deletions packages/sdk/src/Platform/PlatfformManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,12 @@ describe('PlatformManager', () => {
});

it('should return true if isNotBrowser returns false', () => {
jest.spyOn(platformManager, 'isNotBrowser').mockReturnValue(false);

jest.spyOn(PlatformManager, 'isNotBrowser').mockReturnValue(false);
expect(platformManager.isBrowser()).toBe(true);
});

it('should return false if isNotBrowser returns true', () => {
jest.spyOn(platformManager, 'isNotBrowser').mockReturnValue(true);

jest.spyOn(PlatformManager, 'isNotBrowser').mockReturnValue(true);
expect(platformManager.isBrowser()).toBe(false);
});
});
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export async function setupStorageManager(instance: MetaMaskSDK) {
const { options } = instance;

if (options.storage?.enabled === true && !options.storage.storageManager) {
options.storage.storageManager = getStorageManager(
// instance.platformManager,
options.storage,
);
options.storage.storageManager = await getStorageManager(options.storage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function initializeStateAsync(instance: SDKProvider) {
let relayPersistence = false;

let useCache = false;
const storageManager = getStorageManager({ enabled: true });
const storageManager = await getStorageManager({ enabled: true });

// FIXME: currently set for backward compatibility so new sdk don't autoconnect with old wallet
// Only use cache if relayPersistence is enabled for current channel.
Expand Down
41 changes: 19 additions & 22 deletions packages/sdk/src/storage-manager/getStorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
StorageManager,
StorageManagerProps,
} from '@metamask/sdk-communication-layer';
import { PlatformManager } from '../Platform/PlatfformManager';

/* #if _NODEJS
import { StorageManagerNode as SMDyn } from './StorageManagerNode';
//#elif _WEB
import { StorageManagerWeb as SMDyn } from './StorageManagerWeb';
//#elif _REACTNATIVE
import { StorageManagerAS as SMDyn } from './StorageManagerAS';
//#else */
// This is ONLY used during development with devnext/devreactnative or via transpiling
import { StorageManagerAS as SMDyn } from './StorageManagerAS';
// eslint-disable-next-line spaced-comment
//#endif

export const getStorageManager = (
// platformManager: PlatformManager,
export const getStorageManager = async (
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);
): Promise<StorageManager> => {
if (PlatformManager.isBrowser()) {
const { StorageManagerWeb } = await import('./StorageManagerWeb');
return new StorageManagerWeb(options);

Check warning on line 13 in packages/sdk/src/storage-manager/getStorageManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/sdk/src/storage-manager/getStorageManager.ts#L12-L13

Added lines #L12 - L13 were not covered by tests
}

const noopStorageManager: StorageManager = {
persistChannelConfig: async () => undefined,

Check warning on line 17 in packages/sdk/src/storage-manager/getStorageManager.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L17 was not covered by tests
getPersistedChannelConfig: async () => undefined,
persistAccounts: async () => undefined,

Check warning on line 19 in packages/sdk/src/storage-manager/getStorageManager.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L19 was not covered by tests
getCachedAccounts: async () => [],
persistChainId: async () => undefined,

Check warning on line 21 in packages/sdk/src/storage-manager/getStorageManager.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L21 was not covered by tests
getCachedChainId: async () => undefined,
terminate: async () => undefined,

Check warning on line 23 in packages/sdk/src/storage-manager/getStorageManager.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L23 was not covered by tests
} as StorageManager;

return Promise.resolve(noopStorageManager);
};
Loading