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: 增加 window 使用前的判断 #767

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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: 3 additions & 3 deletions packages/effects-core/src/utils/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export function isIOSByUA () {
}

export function isAndroid (): boolean {
return /\b[Aa]ndroid\b/.test(navigator.userAgent);
return window && /\b[Aa]ndroid\b/.test(navigator.userAgent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Enhance window and navigator safety checks

The current window check is insufficient and could still throw a ReferenceError. Additionally, navigator should be checked before access.

Apply this diff to improve safety:

-  return window && /\b[Aa]ndroid\b/.test(navigator.userAgent);
+  return typeof window !== 'undefined' && typeof navigator !== 'undefined' && /\b[Aa]ndroid\b/.test(navigator.userAgent);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return window && /\b[Aa]ndroid\b/.test(navigator.userAgent);
return typeof window !== 'undefined' && typeof navigator !== 'undefined' && /\b[Aa]ndroid\b/.test(navigator.userAgent);

}

export function isSimulatorCellPhone (): boolean {
return isAndroid() || /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent);
return isAndroid() || (window && /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Optimize safety checks and reduce redundancy

The function has similar safety issues as isAndroid() and includes redundant checks through the isAndroid() call.

Apply this diff to improve safety and efficiency:

-  return isAndroid() || (window && /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent));
+  return typeof window !== 'undefined' && typeof navigator !== 'undefined' && 
+    (/\b[Aa]ndroid\b/.test(navigator.userAgent) || /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return isAndroid() || (window && /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent));
return typeof window !== 'undefined' && typeof navigator !== 'undefined' &&
(/\b[Aa]ndroid\b/.test(navigator.userAgent) || /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent));

}

export function isMiniProgram () {
Expand All @@ -39,5 +39,5 @@ export function isAlipayMiniApp (): boolean {
}

export function isWechatMiniApp () {
return window.__wxjs_environment === 'miniprogram';
return window?.__wxjs_environment === 'miniprogram';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use consistent window safety check pattern

While optional chaining helps, it doesn't prevent ReferenceError when window is undefined. Let's maintain consistency with other functions.

Apply this diff:

-  return window?.__wxjs_environment === 'miniprogram';
+  return typeof window !== 'undefined' && window.__wxjs_environment === 'miniprogram';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return window?.__wxjs_environment === 'miniprogram';
return typeof window !== 'undefined' && window.__wxjs_environment === 'miniprogram';

}
2 changes: 1 addition & 1 deletion plugin-packages/alipay-downgrade/src/native-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { isAlipayMiniApp, isAndroid, logger } from '@galacean/effects';

const prefix = '[Galacean Effects]';
const ap = isAlipayMiniApp() ? my : window.AlipayJSBridge;
const ap = isAlipayMiniApp() ? my : window?.AlipayJSBridge;

logger.register(nativeLogger);

Expand Down
2 changes: 1 addition & 1 deletion plugin-packages/alipay-downgrade/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function getDowngradeResult (bizId: string, options: DowngradeOptio
});
}

const ap = isAlipayMiniApp() ? my : window.AlipayJSBridge;
const ap = isAlipayMiniApp() ? my : window?.AlipayJSBridge;

// 当需要通过 ap 获取降级信息时,才进行降级环境的检查
if (!ap && (!options.systemInfo || !options.downgradeResult)) {
Expand Down