tauri-docs icon indicating copy to clipboard operation
tauri-docs copied to clipboard

FAQ: How to Determine the App Environment?

Open Laegel opened this issue 3 years ago • 15 comments

The question about knowing the app environment is often asked, for both backend and frontend sides. :point_right: This may indicate we're missing a simple feature for a better DX and/or a new line in our FAQ.

Laegel avatar Jun 22 '22 09:06 Laegel

What specifics? OS, OS version, if in dev or release, etc? Do we have an API that exposed this all to reference?

lorenzolewis avatar Jun 23 '22 11:06 lorenzolewis

Same problem here. I'd just like to know if I'm currently in the yarn tauri dev or if it's built already.

Googling it for 15min now and still nothing. Maybe this is not a common thing to do?

tillcarlos avatar Aug 15 '22 03:08 tillcarlos

Copying from discord: In rust you can check it with #[cfg(dev)], example:

#[cfg(dev)]
do_something_in_dev_only();

And in javascript you can just check if the current url matches your devPath.

FabianLars avatar Aug 15 '22 09:08 FabianLars

@FabianLars @lucasfernog I think this is something that would be a better as an API like Laegel mentioned. Something like app.getEnvironment that returns an enum or something and is bridged over to the JS side.

lorenzolewis avatar Aug 15 '22 11:08 lorenzolewis

The problem I have with app.get{Something} is that it's async/promise, not a big deal really, but I'd rather work with static variables when possible.

What I do currently is use the TAURI_{Something} variables that are exposed during the build process to inject static strings to the bundles (using vite). I struggled to find this on the docs too, but it's mentioned on the vite specific guide: https://tauri.app/v1/guides/getting-started/setup/vite/

goenning avatar Oct 01 '22 11:10 goenning

Ok for the #[cfg(dev)], but what about the #[cfg(target_os = "macos")] ?

There is no way to check on operating system type from ~~backend~~ frontend.

ClementGre avatar Dec 18 '22 19:12 ClementGre

@ClementGre

There is no way to check on operating system type from backend.

I don't think i understand your question. You did show how to check the os in the backend = the target_os cfg is correct 🤔

FabianLars avatar Dec 19 '22 08:12 FabianLars

Sorry for that, I meant frontend.

The target os in frontend is always wasm (WebAssembly), then there is no way to check on the underlying os from frontend in rust (neither in js).

It would be great to have an API in frontend to get this kind of informations without having to use tauri commands and yew suspenses waiting for an async task when starting the app.

ClementGre avatar Dec 19 '22 10:12 ClementGre

You could also check the os like this: https://github.com/tauri-apps/tauri/blob/75a0c79dea39e3e8372097152a0fa82cf6493686/tooling/api/src/helpers/os-check.ts which does not need async. Would be cool if yew would support build-time environment flag similar to how vite handles VITE_ (or does it?) - this way you could also use the vars injected into beforeBuildCommand: https://tauri.app/v1/api/config#buildconfig.beforebuildcommand

FabianLars avatar Dec 19 '22 11:12 FabianLars

Would be nice if there was a simple boolean/utility function that returns a boolean if the app is in development mode.

TechStudent10 avatar Feb 06 '23 15:02 TechStudent10

So this issue to me seems like the need is generally Rust and web dev knowledge, because in WASM you can only get information at runtime regarding the browser you're running in so there's no cfg to get it, and in Rust you already have all the flags needed to determine everything you might want.

That said, we probably want to cover this sort of information somewhere, I'll track it for 2.0 and try to think of a good place to put it.

simonhyll avatar Sep 15 '23 21:09 simonhyll

Adding context from #1870 : We need to document how to check if you're running in Tauri, e.g. by checking if window.__TAURI_INTERNAL__ exists

simonhyll avatar Feb 26 '24 14:02 simonhyll

For anyone looking for a dirty unofficial solution:

function isDev() {
  return window.location.host.startsWith('localhost:');
}

Assuming you're not launching a local server on your prod build of course.

martpie avatar Mar 02 '24 23:03 martpie

as of now we have tauri::dev() added in https://github.com/tauri-apps/tauri/pull/9113 not specifically to solve the current issue

vasfvitor avatar Mar 25 '24 19:03 vasfvitor