Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions moli-core/tests/fetch_behaviors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,27 @@ async fn fetch_with_networkidle_waits_for_delayed_fetch_dom_mutation() -> Result
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_domstable_waits_for_async_scripts_iframes_and_workers() -> Result<()> {
let server = FixtureServer::spawn().await?;
let browser = Browser::new(AppConfig::default())?;

for kind in ["async", "frame", "worker"] {
let mut page = browser
.fetch_with_wait_until(
&server.url(&format!("/wait-until-domstable-pending-load?kind={kind}")),
RenderedDomWaitUntil::DomStable,
Duration::from_secs(7),
)
.await?;
let html = page.serialize_html_async().await.unwrap();
assert!(html.contains(&format!(">{kind}-ran<")), "{kind}: {html}");
}

server.shutdown().await;
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_domstable_waits_for_delayed_fetch_dom_mutation() -> Result<()> {
let server = FixtureServer::spawn().await?;
Expand Down
11 changes: 9 additions & 2 deletions moli-renderer-v8/src/runtime/page_dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,13 @@ impl PageVm {
|| self.vm().has_pending_webcrypto_tasks()
|| self.vm().has_pending_opfs_tasks();
state.saw_post_domcontentloaded_runtime_work |= pending_runtime_work;
// Loads that may still mutate the DOM hold the stability timer, but
// unlike runtime script work they do not select the longer quiet window.
let pending_loads = self
.vm_mut()
.has_post_domcontentloaded_load_delaying_runtime_work()
|| self.vm().has_pending_child_document_lifecycle()
|| self.vm().has_loading_dedicated_workers();
let has_long_pending_timeout = self
.vm()
.ms_to_next_timeout()
Expand All @@ -1503,12 +1510,12 @@ impl PageVm {
"{snapshot}|html:{serialized_document}|network:{}|pending:{}|runtime:{}",
self.vm().subresource_activity_epoch(),
pending_subresource_requests,
pending_runtime_work
pending_runtime_work || pending_loads
);

let now = std::time::Instant::now();
let mut stable_remaining = DOM_STABLE_POLL_INTERVAL;
if pending_subresource_requests == 0 && !pending_runtime_work {
if pending_subresource_requests == 0 && !pending_runtime_work && !pending_loads {
if state.last_snapshot.as_deref() != Some(snapshot.as_str()) {
state.saw_long_pending_timeout_for_observation = has_long_pending_timeout;
} else {
Expand Down
2 changes: 1 addition & 1 deletion moli-renderer-v8/src/script_vm/post_parse_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ impl ScriptVm {
&& self.has_pending_runtime_script_work())
}

fn has_post_domcontentloaded_load_delaying_runtime_work(&mut self) -> bool {
pub(crate) fn has_post_domcontentloaded_load_delaying_runtime_work(&mut self) -> bool {
if !self.document_runtime.dom_content_loaded_dispatched() {
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions moli-renderer-v8/src/script_vm/runtime_observability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,13 @@ impl ScriptVm {
.dedicated_worker_running_worker_isolate_count_for_diagnostics()
}

pub(crate) fn has_loading_dedicated_workers(&self) -> bool {
self._context_host
.borrow()
.dedicated_worker_loading_count_for_diagnostics()
> 0
}

pub(crate) fn has_pending_webcrypto_tasks(&self) -> bool {
self._context_host.borrow().has_pending_webcrypto_tasks()
}
Expand Down
42 changes: 42 additions & 0 deletions moli-test-support/src/routes_wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ pub(super) fn add_wait_routes(router: Router) -> Router {
get(wait_until_cookie_fetch_page),
)
.route("/wait-until-delayed-xhr", get(wait_until_delayed_xhr_page))
.route(
"/wait-until-domstable-pending-load",
get(wait_until_domstable_pending_load_page),
)
.route(
"/wait-until-domstable-slow-load",
get(wait_until_domstable_slow_load),
)
.route(
"/wait-until-xhr-location-replace",
get(wait_until_xhr_location_replace_page),
Expand Down Expand Up @@ -695,6 +703,40 @@ async fn wait_until_cookie_fetch_page() -> Response {
.into_response()
}

// Each `kind` starts one load that a `domstable` wait must not outlive: a
// parser-inserted async script, a starting iframe or a dedicated Worker.
async fn wait_until_domstable_pending_load_page(
Query(params): Query<std::collections::HashMap<String, String>>,
) -> Html<&'static str> {
Html(match params.get("kind").map(String::as_str) {
Some("async") => {
"<!doctype html><p id=s>initial</p><script async src=\"/wait-until-domstable-slow-load?kind=async\"></script>"
}
Some("frame") => {
"<!doctype html><p id=s>initial</p><iframe src=\"/wait-until-domstable-slow-load?kind=frame\"></iframe>"
}
_ => {
"<!doctype html><p id=s>initial</p><script>new Worker('/wait-until-domstable-slow-load?kind=worker').onmessage=e=>{s.textContent=e.data}</script>"
}
})
}

async fn wait_until_domstable_slow_load(
Query(params): Query<std::collections::HashMap<String, String>>,
) -> Response {
// Longer than the domstable quiet window, so only an explicit pending-load
// check keeps the wait open.
sleep(Duration::from_millis(1800)).await;
match params.get("kind").map(String::as_str) {
Some("async") => javascript_response("document.getElementById('s').textContent='async-ran'"),
Some("frame") => Html(
"<!doctype html><script>parent.document.getElementById('s').textContent='frame-ran'</script>",
)
.into_response(),
_ => javascript_response("postMessage('worker-ran')"),
}
}

async fn wait_until_delayed_xhr_page() -> Html<&'static str> {
Html(WAIT_UNTIL_DELAYED_XHR_HTML)
}
Expand Down
Loading