Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a global HashSet for void elements lookup in SSR #246

Merged
merged 2 commits into from
Sep 16, 2021
Merged
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
3 changes: 2 additions & 1 deletion packages/sycamore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ahash = "0.7.4"
html-escape = { version = "0.2.9", optional = true }
indexmap = { version = "1.7.0", features = ["std"] }
js-sys = "0.3.55"
once_cell = { version = "1.8.0", optional = true }
smallvec = "1.6.1"
sycamore-macro = { path = "../sycamore-macro", version = "=0.6.0" }
sycamore-reactive = { path = "../sycamore-reactive", version = "=0.6.0" }
Expand Down Expand Up @@ -47,7 +48,7 @@ wasm-bindgen-test = "0.3.28"
[features]
default = ["dom"]
dom = []
ssr = ["html-escape"]
ssr = ["html-escape", "once_cell"]
serde = ["sycamore-reactive/serde"]

[[bench]]
Expand Down
16 changes: 11 additions & 5 deletions packages/sycamore/src/generic_node/ssr_node.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
//! Rendering backend for Server Side Rendering, aka. SSR.

use std::cell::RefCell;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::rc::{Rc, Weak};

use ahash::AHashMap;
use once_cell::sync::Lazy;
use wasm_bindgen::prelude::*;

use crate::generic_node::{EventHandler, GenericNode};
use crate::reactive::create_root;
use crate::template::Template;

static VOID_ELEMENTS: &[&str] = &[
"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source",
"track", "wbr", "command", "keygen", "menuitem",
];
static VOID_ELEMENTS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
vec![
"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param",
"source", "track", "wbr", "command", "keygen", "menuitem",
]
.into_iter()
.collect()
});

/// Inner representation for [`SsrNode`].
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -321,7 +327,7 @@ impl WriteToString for Element {
}

// Check if self-closing tag (void-element).
if self.children.is_empty() && VOID_ELEMENTS.iter().any(|tag| tag == &self.name) {
if self.children.is_empty() && VOID_ELEMENTS.contains(self.name.as_str()) {
s.push_str("/>");
} else {
s.push('>');
Expand Down