tauri-apps / tauri-docs

The source for all Tauri project documentation.
https://tauri.app
MIT License
808 stars 600 forks source link

FAQ: How to Determine the App Environment? #699

Open Laegel opened 2 years ago

Laegel commented 2 years ago

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.

lorenzolewis commented 2 years ago

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

tillcarlos commented 2 years ago

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?

FabianLars commented 2 years ago

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.

lorenzolewis commented 2 years ago

@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.

goenning commented 2 years ago

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/

ClementGre commented 1 year ago

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.

FabianLars commented 1 year ago

@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 🤔

ClementGre commented 1 year ago

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.

FabianLars commented 1 year ago

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

TechStudent10 commented 1 year ago

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

simonhyll commented 1 year ago

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 commented 7 months ago

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

martpie commented 7 months ago

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.

vasfvitor commented 6 months ago

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

andreadev-it commented 2 months ago

Just ended up on this topic while looking for this exact situation related to frontend. One thing that might be helpful for future reader: check if the frontend environment you're using doesn't already have a flag about this. In my case, I'm using Vite, and there is a flag accessible at import.meta.env.DEV that tells you if you're in the development environment (these are the docs about it).