Compare commits

...

5 Commits

Author SHA1 Message Date
Greg Johnston
52c5a88e53 Construct all fragments lazily (closes #299 and #421) 2023-01-31 23:34:33 -05:00
Greg Johnston
25dfd88978 Add html-escape dependency 2023-01-31 23:26:31 -05:00
Greg Johnston
066ad56dbd Fix HTML entities 2023-01-31 22:31:15 -05:00
Greg Johnston
decd5fbfdb clippy 2023-01-31 22:28:01 -05:00
Greg Johnston
fdb6425504 Correctly escape special HTML characters in static strings in view macro 2023-01-31 22:07:17 -05:00
6 changed files with 13 additions and 17 deletions

View File

@@ -16,10 +16,7 @@ pub fn ErrorTemplate(
#[prop(optional)] errors: Option<RwSignal<Errors>>,
) -> impl IntoView {
let errors = match outside_errors {
Some(e) => {
let errors = create_rw_signal(cx, e);
errors
}
Some(e) => create_rw_signal(cx, e),
None => match errors {
Some(e) => e,
None => panic!("No Errors found and we expected errors!"),
@@ -32,8 +29,7 @@ pub fn ErrorTemplate(
// Downcast lets us take a type that implements `std::error::Error`
let errors: Vec<AppError> = errors
.into_iter()
.map(|(_k, v)| v.downcast_ref::<AppError>().cloned())
.flatten()
.filter_map(|(_k, v)| v.downcast_ref::<AppError>().cloned())
.collect();
println!("Errors: {errors:#?}");
@@ -54,7 +50,7 @@ pub fn ErrorTemplate(
// a function that returns the items we're iterating over; a signal is fine
each= move || {errors.clone().into_iter().enumerate()}
// a unique key for each item as a reference
key=|(index, _error)| index.clone()
key=|(index, _error)| *index
// renders each item to a view
view= move |error| {
let error_string = error.1.to_string();

View File

@@ -33,14 +33,13 @@ if #[cfg(feature = "ssr")] {
async fn get_static_file(uri: Uri, root: &str) -> Result<Response<BoxBody>, (StatusCode, String)> {
let req = Request::builder().uri(uri.clone()).body(Body::empty()).unwrap();
let root_path = format!("{root}");
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
// This path is relative to the cargo root
match ServeDir::new(&root_path).oneshot(req).await {
match ServeDir::new(root).oneshot(req).await {
Ok(res) => Ok(res.map(boxed)),
Err(err) => Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", err),
format!("Something went wrong: {err}"),
)),
}
}

View File

@@ -64,7 +64,7 @@ pub fn ExampleErrors(cx: Scope) -> impl IntoView {
<a href="/404">"This Page Does not Exist"</a>
</p>
<p>
"The following &ltdiv&gt will always contain an error and cause the page to produce status 500. Check browser dev tools. "
"The following <div> will always contain an error and cause the page to produce status 500. Check browser dev tools. "
</p>
<div>
<ErrorBoundary fallback=|cx, errors| view!{cx, <ErrorTemplate errors=errors/>}>

View File

@@ -37,7 +37,7 @@ if #[cfg(feature = "ssr")] {
// Setting this to None means we'll be using cargo-leptos and its env vars
let conf = get_configuration(None).await.unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_address.clone();
let addr = leptos_options.site_address;
let routes = generate_route_list(|cx| view! { cx, <App/> }).await;
// build our application with a route

View File

@@ -14,6 +14,7 @@ proc-macro = true
[dependencies]
cfg-if = "1"
doc-comment = "0.3"
html-escape = "0.2"
itertools = "0.10"
pad-adapter = "0.1"
prettyplease = "0.1"

View File

@@ -171,7 +171,7 @@ pub(crate) fn render_view(
cx,
Span::call_site(),
nodes,
false,
true,
TagType::Unknown,
global_class,
)
@@ -219,7 +219,7 @@ fn fragment_to_tokens_ssr(
});
quote! {
{
leptos::Fragment::new(vec![
leptos::Fragment::lazy(|| vec![
#(#nodes),*
])
}
@@ -336,7 +336,7 @@ fn element_to_tokens_ssr(
),
Node::Text(text) => {
if let Some(value) = value_to_string(&text.value) {
template.push_str(&value);
template.push_str(&html_escape::encode_safe(&value));
} else {
template.push_str("{}");
let value = text.value.as_ref();
@@ -626,7 +626,7 @@ fn node_to_tokens(
cx,
Span::call_site(),
&fragment.children,
false,
true,
parent_type,
global_class,
),
@@ -708,7 +708,7 @@ fn element_to_tokens(
cx,
Span::call_site(),
&fragment.children,
false,
true,
parent_type,
global_class,
),