-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||
} | ||||||||
|
||||||||
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)); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize safety checks and reduce redundancy The function has similar safety issues as 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
Suggested change
|
||||||||
} | ||||||||
|
||||||||
export function isMiniProgram () { | ||||||||
|
@@ -39,5 +39,5 @@ export function isAlipayMiniApp (): boolean { | |||||||
} | ||||||||
|
||||||||
export function isWechatMiniApp () { | ||||||||
return window.__wxjs_environment === 'miniprogram'; | ||||||||
return window?.__wxjs_environment === 'miniprogram'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
📝 Committable suggestion