dynamic_asset.rs 1.0 KB

12345678910111213141516171819202122232425262728293031
  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. const STYLE: Asset = asset!("/examples/assets/custom_assets.css");
  9. fn main() {
  10. dioxus::LaunchBuilder::desktop().launch(app);
  11. }
  12. fn app() -> Element {
  13. use_asset_handler("logos", |request, response| {
  14. // We get the original path - make sure you handle that!
  15. if request.uri().path() != "/logos/logo.png" {
  16. return;
  17. }
  18. response.respond(Response::new(include_bytes!("./assets/logo.png").to_vec()));
  19. });
  20. rsx! {
  21. document::Link { rel: "stylesheet", href: STYLE }
  22. h1 { "Dynamic Assets" }
  23. img { src: "/logos/logo.png" }
  24. }
  25. }