fix: update Tauri environment detection for v2 compatibility#459
Conversation
WalkthroughThe update enhances Tauri environment detection in the frontend utility by extending the Changes
Possibly related PRs
Suggested labels
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
frontend/src/utils/tauriUtils.ts (1)
15-38: Good detection logic, but optimize the implementation.The multi-method detection approach correctly addresses Tauri v2 compatibility while maintaining backward compatibility. However, there are optimization opportunities:
- The try-catch block around
window.isTauriis unnecessary since property access won't throw an error- The
typeof window !== 'undefined'check is repeated multiple timesConsider this optimized version:
export const isTauriEnvironment = (): boolean => { + // Early return if not in browser environment + if (typeof window === 'undefined') { + return false; + } + - // Method 1: Use official Tauri API (recommended) - try { - // This is the official way to detect Tauri environment - // Available since Tauri 2.0.0-beta.9 - if (typeof window !== 'undefined' && window.isTauri) { - return true; - } - } catch (error) { - // Fallback to manual detection if official API fails - } + // Method 1: Use official Tauri API (recommended) + // Available since Tauri 2.0.0-beta.9 + if (window.isTauri) { + return true; + } - // Method 2: Check for __TAURI_INTERNALS__ (Tauri v2 manual detection) - if (typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window) { - return true; - } + // Method 2: Check for __TAURI_INTERNALS__ (Tauri v2 manual detection) + if ('__TAURI_INTERNALS__' in window) { + return true; + } - // Method 3: Fallback to __TAURI__ for backward compatibility (requires withGlobalTauri: true) - if (typeof window !== 'undefined' && '__TAURI__' in window) { - return true; - } + // Method 3: Fallback to __TAURI__ for backward compatibility (requires withGlobalTauri: true) + if ('__TAURI__' in window) { + return true; + } return false; };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
frontend/src/utils/tauriUtils.ts(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
- GitHub Check: Backend Tests
- GitHub Check: Tauri Build Check (ubuntu-22.04)
- GitHub Check: Tauri Build Check (macos-latest, --target aarch64-apple-darwin)
- GitHub Check: Tauri Tests
- GitHub Check: Tauri Build Check (windows-latest)
🔇 Additional comments (2)
frontend/src/utils/tauriUtils.ts (2)
5-12: LGTM! Well-structured TypeScript declarations.The global interface extension correctly defines the optional Tauri-specific properties with appropriate types. This provides good type safety for the detection logic.
40-49: Excellent implementation of the official API detection.The function correctly uses dynamic import for optional dependency loading and provides appropriate fallback handling. This gives users flexibility to choose between manual detection and official API detection.
|
@rahulharpal1603 i think now it works |
|
@Aditya30ag Thank you for the fix! |
|
Thanks to youu |
Summary
Fixed Tauri environment detection to work correctly with Tauri v2, resolving the issue where the desktop app was incorrectly showing the browser warning.
Problem
The existing
isTauriEnvironment()function used the deprecatedwindow.__TAURI__check, which is no longer available by default in Tauri v2 unlesswithGlobalTauri: trueis explicitly set.Solution
window.isTauricheck (official API since Tauri 2.0.0-beta.9)window.__TAURI_INTERNALS__fallback for Tauri v2 compatibilitywindow.__TAURI__as final fallback for backward compatibilityisTauriEnvironmentOfficial()using@tauri-apps/api/coreTesting
References
Summary by CodeRabbit