tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
84.31k stars 2.54k forks source link

[feat] Support pure server-side HTML generation #8460

Open drernie opened 10 months ago

drernie commented 10 months ago

WAS [feat] Support HTMX / JavaScript-less Tauri applications

Describe the problem

I realize Tauri was explicitly intended to enable JavaScript-based front-ends. However, I've been drinking the HTMX kool-aid, and have become convinced that I can get a 10x reduction in code size [for a certain class of apps] by avoiding JavaScript altogether (in favor of server-side/htmx HTML).

The main "blocker" I had to workaround is that (as far as I can tell) there is no way to replace or intercept the static src/index.html used by the main window. I don't even seem to get a load event I can use to trigger html (so I had to use a self-expiring timer).

Describe the solution you'd like

The main innovations that would make my life easier would be:

  1. Being able to tell the main window load its initial content from a "command" instead of a static html page
  2. Incorporate (or otherwise implement) the excellent tauri-plugin-htmx
  3. Provide an option to use a traditional Rust repo layout, with a top-level Cargo.toml and a single "src" folder for the Rust modules.

Alternatives considered

On the aggressive side, I would love it if create-tauri-app would provide a "HTMX" plugin -- alongside "VanillaJS" for Rust apps -- that would pre-configure all those options (perhaps even with a choice of server-side frameworks).

More realistically, I wonder if this is orthogonal enough from Tauri's purpose that I would be better off creating my own fork to do this. If that is what you recommend, I would appreciate guidance on the best way to structure my work so that I could potentially contribute specific more-generally-useful features back to the main branch.

Additional context

We are currently evaluating tauri+htmx for a high-profile internal project. If it succeeds, I hope we can release it as open source for others to learn form. If nothing else, perhaps this feature request can be a place to discuss best practices for how to build JavaScript-less Tauri applications. Suggestions welcome!

JonasKruckenberg commented 10 months ago

Aright, I think you're making multiple somewhat related requests/points so let me address these in-turn:

I realize Tauri was explicitly intended to enable JavaScript-based front-ends.

This is only half-true-ish, technically Tauri just offers you a regular old browsing context that you're free to do with whatever you wish. It's true that interfacing with any Tauri APIs through @tauri-apps/api requires JavaScript, but I would argue that anything that's purely non-interactive has no real point being an app (a static blog website should just be a website) but correct me if I'm wrong here.

have become convinced that I can get a 10x reduction in code size [for a certain class of apps] by avoiding JavaScript altogether (in favor of server-side/htmx HTML).

Yes, that is certainly true JavaScript tends to be the largest asset class in desktop applications (partly due to frontend frameworks and partly because of my previous point) and there are many setups right now that give you this benefit (my personal favourite stack here is Tauri+Astro+(vanilla JS or solid depending on complexity).

With that being said: HTMX is not a server-side framework, it's a (admittedly very small) JS bundle. So there is nothing stopping you right now from using the vanilla JS starter template, just add the HTMX script tag and you're good to go. No fancy interception workaround necessary.

What I suspect you're actually trying to do (and this is me speculating a bit so correct me if I'm wrong) is to render HTML in rust and just serve up new HTML in response to frontend input. The good news here is that this also already works: Instead of instructing the webview to render e.g. tauri://index.html as is the default you can register a custom uri scheme handler (v1 and v2 has a sync and an async variant) and instruct the webview to load from that instead e.g. my-app://index.html. Inside that callback you can now do any programatic HTML generation you want. You could even use a Rust server framework like Axum.

In conclusion I would say this doesn't sound like something that needs support from Tauri core, but like an excellent template one could open source and contribute to the awesome Tauri list

drernie commented 10 months ago

What I suspect you're actually trying to do (and this is me speculating a bit so correct me if I'm wrong) is to render HTML in rust and just serve up new HTML in response to frontend input.

Ah, that's an excellent point. Thanks for helping me think through this more clearly. I've retitled the request. :-)

Instead of instructing the webview to render e.g. tauri://index.html as is the default you can register a custom uri scheme handler (v1 and v2 has a sync and an async variant) and instruct the webview to load from that instead e.g. my-app://index.html.

Okay, I think I understand about custom URI handlers, but could not figure out how to "instruct the [default] webview to load from that instead". Is there a key for that in thetauri.conf.json window settings?

In conclusion I would say this doesn't sound like something that needs support from Tauri core, but like an excellent template one could open source and contribute to the awesome Tauri list

Thanks! Also -- sorry to be dense -- is there a guide somewhere on how to create a Tauri template? Or is it literally just a "flash frozen" Git Repo of a starter project?