How can I detect if I'm running the frontend in a browser? #6119
-
Hi, folks! Really enjoying Tauri so far! I've just a small issue. Is there a standard way to detect if the frontend of the app is currently running in a browser or in the Tauri webview? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
You can check if if (window.__TAURI__) {
// inside tauri
} |
Beta Was this translation helpful? Give feedback.
-
./src/components/sidebar/index.tsx:50:53 48 |
why i meet the compile error? |
Beta Was this translation helpful? Give feedback.
-
I created const isTauri = '__TAURI__' in window;
export default isTauri;
export const isMobile = navigator.maxTouchPoints > 0;
export const isDesktop = !isMobile;
export const isTauriMobile = isTauri && isMobile;
export const isTauriDesktop = isTauri && isDesktop; Example Usage: import isTauri, { isMobile, isDesktop, isTauriMobile, isTauriDesktop } from '../path-to/isTauri' <div style={isTauriDesktop ? {
backgroundColor: 'orange',
} : {
overflowX: 'hidden',
}}> <Route path="/" element={isTauri ? <Navigate to="/app" /> : <LandingPage />} /> {isTauriDesktop && <TitleBar />} I tested it on android, the web and as a windows desktop app. UpdateI added support for the web version, here is a more advanced use case that some might want: const isTauri = '__TAURI__' in window;
export default isTauri; // running on a desktop app or a mobile app - but not in the browser
export const isWeb = !isTauri; // running on the browser on either desktop or mobile - but not as a tauri app
export const isMobile = navigator.maxTouchPoints > 0; // running in mobile either in the browser or as a tauri app
export const isDesktop = !isMobile; // running in desktop either in the browser or as a tauri app
export const isTauriMobile = isTauri && isMobile; // running on mobile as a tauri app - but not on the browser
export const isTauriDesktop = isTauri && isDesktop; // running on desktop as a tauri app - but not on the browser
export const isWebMobile = isWeb && isMobile; // running on mobile in the browser - but not as a tauri app
export const isWebDesktop = isWeb && isDesktop; // running on desktop in the browser - but not as a tauri app |
Beta Was this translation helpful? Give feedback.
You can check if
window.__TAURI__
exists.