Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(preload): start links queued by completion callbacks

ldm0e559cff1571b
diff --git a/moli-renderer-v8/src/runtime/page_vm/page_dom_manipulation_task_completion.rs b/moli-renderer-v8/src/runtime/page_vm/page_dom_manipulation_task_completion.rs
index 07be9e27e..32938e4f4 100644
--- a/moli-renderer-v8/src/runtime/page_vm/page_dom_manipulation_task_completion.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/page_dom_manipulation_task_completion.rs
@@ -38,7 +38,9 @@ impl PageVm {
action.into_page_task_completion()
}
PageDomManipulationTurnAction::ConnectedStyleEvent(action) => {
- action.into_page_task_completion()
+ return self
+ .finish_selected_page_connected_style_event_task(action, loader)
+ .await;
}
PageDomManipulationTurnAction::TextTrackDefaultMode(action) => {
action.into_page_task_completion()
diff --git a/moli-renderer-v8/src/runtime/page_vm/page_networking_task_completion.rs b/moli-renderer-v8/src/runtime/page_vm/page_networking_task_completion.rs
index 44b50f9f3..4de5e148d 100644
--- a/moli-renderer-v8/src/runtime/page_vm/page_networking_task_completion.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/page_networking_task_completion.rs
@@ -22,11 +22,8 @@ impl PageVm {
self.finish_selected_page_resource_completion_task(action)?;
}
PageNetworkingTurnAction::StyleElementEvent(action) => {
- self.finish_selected_page_task_completion(
- action.into_page_task_completion(),
- loader,
- )
- .await?;
+ self.finish_selected_page_connected_style_event_task(action, loader)
+ .await?;
}
PageNetworkingTurnAction::TextTrackLoad(action) => {
self.finish_selected_page_task_completion(
diff --git a/moli-renderer-v8/src/runtime/page_vm/page_stylesheet_task.rs b/moli-renderer-v8/src/runtime/page_vm/page_stylesheet_task.rs
index 6ba756e78..0034a091d 100644
--- a/moli-renderer-v8/src/runtime/page_vm/page_stylesheet_task.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/page_stylesheet_task.rs
@@ -45,6 +45,28 @@ impl IntoPageTaskCompletion for PageStylesheetNetworkingTurnAction {
}
impl PageVm {
+ pub(super) async fn finish_selected_page_connected_style_event_task(
+ &mut self,
+ action: PageConnectedStyleEventTurnAction,
+ loader: &crate::network::ResourceRequestClient,
+ ) -> anyhow::Result<()> {
+ let targeted_current_owner = !matches!(
+ action.target_effect,
+ PageConnectedStyleEventTargetEffect::DiscardedStaleOwner
+ );
+ self.finish_selected_page_task_completion(action.into_page_task_completion(), loader)
+ .await?;
+ if targeted_current_owner {
+ // A load/error listener or one of its Promise reactions can
+ // connect another style/link owner. Preserve the established
+ // connected-style task boundary by admitting that follow-up only
+ // after the callback checkpoint and runtime reconciliation.
+ self.vm_mut()
+ .prime_document_lifecycle_processing_and_record_stylesheet_network_results();
+ }
+ Ok(())
+ }
+
pub(in crate::runtime) fn apply_selected_page_stylesheet_networking_turn(
&mut self,
task: RendererPageStylesheetNetworkingTask,
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/stylesheet_task.rs b/moli-renderer-v8/src/runtime/page_vm/tests/stylesheet_task.rs
index d5af8a179..c57967e86 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/stylesheet_task.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/stylesheet_task.rs
@@ -1364,6 +1364,69 @@ document.head.appendChild(style);
.expect("connected-style post-checkpoint child synchronization test should run");
}
+#[tokio::test(flavor = "current_thread")]
+async fn connected_modulepreload_event_primes_a_link_appended_by_its_listener() {
+ run_page_vm_async_test(async move {
+ let (base_url, server) = spawn_path_response_http_server(vec![(
+ "/listener-appended.mjs",
+ "HTTP/1.1 200 OK",
+ "export const appended = true;".to_owned(),
+ Duration::ZERO,
+ )])
+ .await;
+ let loader =
+ crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");
+ let document_url = Url::parse(&format!("{base_url}/page.html"))?;
+ let (mut page_vm, _resource_source, mut wake_rx) =
+ page_vm_with_bound_task_sources_and_owner_wake(&loader, document_url);
+ page_vm.vm_mut().eval(&format!(
+ r#"
+globalThis.__modulepreloadListenerEvents = [];
+const first = document.createElement("link");
+first.rel = "modulepreload";
+first.as = "image";
+first.href = "/invalid.bin";
+first.addEventListener("error", () => {{
+ __modulepreloadListenerEvents.push("first-error");
+ const second = document.createElement("link");
+ second.rel = "modulepreload";
+ second.href = "{base_url}/listener-appended.mjs";
+ document.head.append(second);
+}});
+document.head.append(first);
+"queued"
+"#,
+ ))?;
+ page_vm
+ .vm_mut()
+ .prime_document_lifecycle_processing_and_record_stylesheet_network_results();
+
+ wait_for_stylesheet_source(&mut wake_rx, RendererOwnerWakeSource::DomManipulationTask)
+ .await;
+ let event = take_next_link_element_event_task_for_test(&mut page_vm)
+ .expect("the invalid modulepreload must publish one error event");
+ page_vm
+ .run_claimed_dom_manipulation_task_through_selected_dispatcher_for_test(
+ crate::page_task_queue::RendererPageDomManipulationTask::ConnectedStyleEvent(event),
+ &loader,
+ )
+ .await?;
+ assert_eq!(
+ page_vm
+ .vm_mut()
+ .eval("__modulepreloadListenerEvents.join('|')")?,
+ "first-error"
+ );
+ tokio::time::timeout(Duration::from_secs(2), server)
+ .await
+ .expect("the listener-appended modulepreload request must start at task completion")
+ .expect("listener-appended modulepreload server should finish");
+ Ok::<_, anyhow::Error>(())
+ })
+ .await
+ .expect("connected modulepreload listener follow-up test should run");
+}
+
#[tokio::test(flavor = "current_thread")]
async fn failed_inline_import_transitions_from_null_to_an_empty_child_stylesheet() {
run_page_vm_async_test(async move {