Skip to content

Commit

Permalink
✨ Add trait fn that can inject custom HTML raw.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Dec 20, 2023
1 parent 0d5c2e4 commit f452415
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
25 changes: 25 additions & 0 deletions packages/macro-types/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,29 @@ where
</>
}
}

fn render_to_string_outside(
style_raw: String,
html_raw: String,
state: &Self::AppStates,
) -> String {
let state = ::serde_json::to_string(state).unwrap();

format!("
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style>{style_raw}</style>
</head>
<body>
<textarea id='ssr_data' style='display: none;'>{state}</textarea>
<div id='app'>{html_raw}</div>
<script src='/a.js'></script>
<script>(async () => {{await wasm_vendor_entry('/a.wasm');(await (new wasm_vendor_entry.WebHandle())).start();}})()</script>
</body>
</html>
")
}
}
31 changes: 10 additions & 21 deletions packages/macro/src/utils/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ pub fn root(input: DeriveApp) -> TokenStream {
use ::yew::ServerRenderer;

let (writer, reader) = render_static();
let renderer = ServerRenderer::<HikariServerApp>::with_props(move || {

let renderer = ServerRenderer::<HikariServerApp>::with_props({
let states = states.clone();
move || {
let style_manager = StyleManager::builder().writer(writer).build().unwrap();
::hikari_boot::AppContext {
style_manager,
url,
states,
::hikari_boot::AppContext {
style_manager,
url,
states,
}
}
});
let html_raw = renderer.render().await;
Expand All @@ -170,22 +174,7 @@ pub fn root(input: DeriveApp) -> TokenStream {
let mut style_raw = String::new();
style_data.write_static_markup(&mut style_raw).unwrap();

format!("
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style>{style_raw}</style>
</head>
<body>
<textarea id='ssr_data' style='display: none;'>{{}}</textarea>
<div id='app'>{html_raw}</div>
<script src='/a.js'></script>
<script>(async () => {{await wasm_vendor_entry('/a.wasm');(await (new wasm_vendor_entry.WebHandle())).start();}})()</script>
</body>
</html>
")
<#ident as ::hikari_boot::DeclType>::render_to_string_outside(style_raw, html_raw, &states)
}
}
}
Expand Down

0 comments on commit f452415

Please sign in to comment.