Skip to content

Commit

Permalink
Fix mounted event after initial hydration (#3480)
Browse files Browse the repository at this point in the history
* trigger queued mounted events immediately after hydrating

* add a playwright test to prevent regressions
  • Loading branch information
ealmloff authored Jan 8, 2025
1 parent d41ab3a commit d890cc7
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ members = [
"packages/playwright-tests/liveview",
"packages/playwright-tests/web",
"packages/playwright-tests/fullstack",
"packages/playwright-tests/fullstack-mounted",
"packages/playwright-tests/suspense-carousel",
"packages/playwright-tests/nested-suspense",
]
Expand Down
10 changes: 10 additions & 0 deletions packages/playwright-tests/fullstack-mounted.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @ts-check
const { test, expect } = require("@playwright/test");

test("hydration", async ({ page }) => {
await page.goto("http://localhost:7777");

// Expect the page to contain the pending text.
const main = page.locator("#main");
await expect(main).toContainText("The mounted event was triggered.");
});
17 changes: 17 additions & 0 deletions packages/playwright-tests/fullstack-mounted/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "dioxus-playwright-fullstack-mounted-test"
version = "0.1.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dioxus = { workspace = true, features = ["fullstack"] }
serde = "1.0.159"
tokio = { workspace = true, features = ["full"], optional = true }

[features]
default = []
server = ["dioxus/server", "dep:tokio"]
web = ["dioxus/web"]
23 changes: 23 additions & 0 deletions packages/playwright-tests/fullstack-mounted/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Regression test for https://github.com/DioxusLabs/dioxus/pull/3480

use dioxus::prelude::*;

fn main() {
dioxus::launch(App);
}

#[component]
fn App() -> Element {
let mut mounted = use_signal(|| false);

rsx! {
div {
onmounted: move |_| {
mounted.set(true);
},
if mounted() {
"The mounted event was triggered."
}
}
}
}
9 changes: 9 additions & 0 deletions packages/playwright-tests/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ module.exports = defineConfig({
reuseExistingServer: !process.env.CI,
stdout: "pipe",
},
{
cwd: path.join(process.cwd(), "fullstack-mounted"),
command:
'cargo run --package dioxus-cli --release -- serve --force-sequential --platform web --addr "127.0.0.1" --port 7777',
port: 7777,
timeout: 50 * 60 * 1000,
reuseExistingServer: !process.env.CI,
stdout: "pipe",
},
{
cwd: path.join(process.cwd(), "suspense-carousel"),
command:
Expand Down
6 changes: 6 additions & 0 deletions packages/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ pub async fn run(mut virtual_dom: VirtualDom, web_config: Config) -> ! {

let rx = websys_dom.rehydrate(&virtual_dom).unwrap();
hydration_receiver = Some(rx);

#[cfg(feature = "mounted")]
{
// Flush any mounted events that were queued up while hydrating
websys_dom.flush_queued_mounted_events();
}
}
#[cfg(not(feature = "hydrate"))]
{
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl WebsysDom {
}

#[cfg(feature = "mounted")]
fn flush_queued_mounted_events(&mut self) {
pub(crate) fn flush_queued_mounted_events(&mut self) {
for id in self.queued_mounted_events.drain(..) {
let node = self.interpreter.base().get_node(id.0 as u32);
if let Some(element) = node.dyn_ref::<web_sys::Element>() {
Expand Down

0 comments on commit d890cc7

Please sign in to comment.