Skip to content

Commit

Permalink
Revert "Check the code in the guide (#380)" (#391)
Browse files Browse the repository at this point in the history
This reverts commit b7f514e.
  • Loading branch information
jkelleyrtp authored Jan 16, 2025
1 parent b7f514e commit 334c3ad
Show file tree
Hide file tree
Showing 40 changed files with 860 additions and 1,342 deletions.
50 changes: 0 additions & 50 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dioxus = { version = "0.6.0", features = ["router"] }
dioxus-web = { version = "0.6.0", features = ["hydrate"], optional = true }
dioxus-ssr = { version = "0.6.0", optional = true }
dioxus-desktop = { version = "0.6.0", optional = true }
dioxus-cli-config = { version = "0.6.0", optional = true }
dioxus-liveview = { version = "0.6.0", features = ["axum"], optional = true }

dioxus-material-icons = { git = "https://github.com/jkelleyrtp/dioxus-material-icons", branch = "jk/git-rev" }
Expand Down Expand Up @@ -58,7 +57,6 @@ dioxus-sdk = { version = "0.6.0", optional = true }
tower-http = { version = "0.5.0", optional = true, features = ["timeout"] }
tracing = "0.1.40"
rand = { version = "0.8.5", optional = true }
rusqlite = { version = "0.32.1", optional = true }
askama_escape = { version = "0.10.3", optional = true }

[build-dependencies]
Expand Down Expand Up @@ -171,7 +169,5 @@ doc_test = [
"http",
"rand",
"server",
"dioxus/fullstack",
"dioxus-cli-config",
"dep:rusqlite"
"dioxus/fullstack"
]
Binary file removed assets/icon.png
Binary file not shown.
1 change: 0 additions & 1 deletion docs-src/0.4/en/router/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

29 changes: 23 additions & 6 deletions docs-src/0.6/src/guide/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ The bare-bones template already includes a base `main.css` in the `assets` folde
To include the CSS in our app, we can use the `asset!()` macro. This macro ensures the asset will be included in the final app bundle.

```rust
{{#include src/doc_examples/guide_assets.rs:css_asset}}
static CSS: Asset = asset!("/assets/main.css");
```

We also need to load the asset into our app using the `document::Stylesheet` component. This component is equivalent to the `<link>` HTML element but also ensures the CSS will be pre-loaded during server-side-rendering.

```rust
{{#include src/doc_examples/guide_assets.rs:css_stylesheet}}
fn App() -> Element {
rsx! {
document::Stylesheet { href: CSS }
// rest of the app
}
}
```

Unlike Rust's `include_str!()` macro, the `asset!()` macro does not actually include the *contents* of the asset in our final executable. Instead, it generates a unique path so that the asset can be loaded at runtime. This is ideal for web apps where assets are loaded in parallel through different HTTP requests.
Expand All @@ -58,27 +63,39 @@ In Dioxus, you can include images in two ways:
When including assets with a URL, simply fill the `src` attribute of `img {}`. Note that when the app is offline, URL-based images won't download.

```rust
{{#include src/doc_examples/guide_assets.rs:url_image}}
rsx! {
// ...
div {
img { src: "https://images.dog.ceo/breeds/pitbull/dog-3981540_1280.jpg" }
}
// ...
}
```

For static images, you can use the same `asset!()` macro that we used to include the app's CSS.

```rust
{{#include src/doc_examples/guide_assets.rs:asset_image}}
static ICON: Asset = asset!("/assets/icon.png");

rsx! {
img { src: ICON }
}
```

## Optimizations

By default, the `asset!()` macro will lightly optimize CSS, JavaScript, JSON, and images. The name of the asset will also be modified to include a content hash.

```rust
{{#include src/doc_examples/guide_assets.rs:asset_optimization}}
// would output main-j1238nask123.css
asset!("/assets/main.css").to_string()
```

You can optimize assets even further, with an optional `Options` struct. For example, `dx` can automatically convert `.png` images to a more optimized `.avif` format:

```rust
{{#include src/doc_examples/guide_assets.rs:image_asset_expansion}}
// outputs image-j1238jd2.avif
asset!("/assets/image.png", ImageAssetOptions::new().with_avif())
```
For many apps, asset optimization is the most effective way of improving load times. As developers, we frequently overlook the size of images and accidentally make our sites load slower.

Expand Down
Loading

0 comments on commit 334c3ad

Please sign in to comment.