Use WRY for prerender purposes possible? #761
Replies: 4 comments 15 replies
-
This is really nice use-case and I think WRY already supports that, just point it to the desired URL or load your HTML/CSS/JS directly, wait for |
Beta Was this translation helpful? Give feedback.
-
Here's an updated code supporting wry v0.34. use serde::{Deserialize, Serialize};
use serde_json;
use std::env;
use std::fs::{canonicalize, read_to_string};
use std::process::exit;
use wry::{
application::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
},
webview::WebViewBuilder,
};
#[derive(Debug, Serialize, Deserialize)]
struct MessageParameters {
message: String,
data: String,
}
const RETURN_OUTER_HTML: &str = r#"
document.addEventListener("DOMContentLoaded", () => {
let html = document.documentElement.outerHTML
const message = JSON.stringify({ message: "dom-loaded", data: html })
ipc.postMessage(message)
})
"#;
fn main() -> wry::Result<()> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_visible(false)
.build(&event_loop)?;
let args: Vec<String> = env::args().collect();
let file_path = &args[1];
let html = read_to_string(canonicalize(file_path)?)?;
let handler = move |_window: &Window, req: String| {
let msg: MessageParameters = serde_json::from_str(&req).unwrap();
if &msg.message == "dom-loaded" {
println!("{}", &msg.data);
}
exit(0);
};
let _webview = Some(
WebViewBuilder::new(window)?
.with_html(&html)?
.with_initialization_script(RETURN_OUTER_HTML)
.with_ipc_handler(handler)
.build()?,
);
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
}
})
} Let's check how it works, using the Google Shopping results page (the HTML file is taken via the our Google Shopping Results scraper). $ time cargo run -r ~/serpapi/tmp/big_shopping.html 2>/dev/null > ~/serpapi/tmp/big_shopping_rendered.html
real 0m1.193s
user 0m0.360s
sys 0m0.199s The source and resulting HTML files differ. $ du -sh ~/serpapi/tmp/big_shopping*.html
1.9M /home/serpapi/tmp/big_shopping.html
2.4M /home/serpapi/tmp/big_shopping_rendered.html Comparing actual HTML. In the original HTML (on the left), thumbnails are 1x1 placeholder pixels — In the resulting HTML (on the right), thumbnails are actual images, that are JavaScript-rendered. Closing thoughts
|
Beta Was this translation helpful? Give feedback.
-
I still think WRY would be perfect for helping with "prerender" tasks. Current solutions involve using puppeteer and actually opening up a browser, navigate to the page, wait for navigation to finish (among other things), and then getting the content and returning it to the bots. Seems it would perform way better with WRY if we can get it working correctly (eg. waiting for JS to finish loading completely) |
Beta Was this translation helpful? Give feedback.
-
The same happens with wry, except it uses webkit instead of chromium. |
Beta Was this translation helpful? Give feedback.
-
Hi
Is it possible to use Wry to fetch completed html and return the html?
We're currently using prerender (https://github.com/prerender/prerender), which uses headless Chrome - and we all know how bad Chrome actually is when it comes to resource usage.
What I'm thinking is; make a rust application that exposes an API that accepts an URL (the page we want to prerender), run it through the WebView and then get back the html and return it through the API.
Beta Was this translation helpful? Give feedback.
All reactions