dynamic_asset.rs 968 B

1234567891011121314151617181920212223242526272829
  1. //! This example shows how to load in custom assets with the use_asset_handler hook.
  2. //!
  3. //! This hook is currently only available on desktop and allows you to intercept any request made by the webview
  4. //! and respond with your own data. You could use this to load in custom videos, streams, stylesheets, images,
  5. //! or any asset that isn't known at compile time.
  6. use dioxus::desktop::{use_asset_handler, wry::http::Response};
  7. use dioxus::prelude::*;
  8. fn main() {
  9. launch_desktop(app);
  10. }
  11. fn app() -> Element {
  12. use_asset_handler("logos", |request, response| {
  13. // We get the original path - make sure you handle that!
  14. if request.uri().path() != "/logos/logo.png" {
  15. return;
  16. }
  17. response.respond(Response::new(include_bytes!("./assets/logo.png").to_vec()));
  18. });
  19. rsx! {
  20. style { {include_str!("./assets/custom_assets.css")} }
  21. h1 { "Dynamic Assets" }
  22. img { src: "/logos/logo.png" }
  23. }
  24. }