Skip to content

Commit

Permalink
Make SsrNode attribute order stable (#323)
Browse files Browse the repository at this point in the history
This makes it possible to use SSR'd output as snapshot test by making the attribute order deterministic.

* Stable output of render_to_string

* Run cargo fmt & cargo clippy

* The output result of `render_to_string` is now fixed

* Rearrange the import order
  • Loading branch information
oovm authored Dec 17, 2021
1 parent 9ccb2d2 commit b9c3089
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ Cargo.lock

# VS Code folder
.vscode

# Jetbrain IDE
.idea/
*.iml
2 changes: 1 addition & 1 deletion examples/hydrate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn app() -> View<G> {
fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

let s = sycamore::render_to_string(|| view! { App() });
log::info!("{}", s);

Expand Down
12 changes: 6 additions & 6 deletions packages/sycamore/src/builder/agnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,17 @@ where
if TypeId::of::<G>() == TypeId::of::<crate::SsrNode>() {
// If Server Side Rendering, insert beginning tag for hydration purposes.
self.element.append_child(&G::marker_with_text("#"));
// Create end marker. This is needed to make sure that the node is inserted into the right
// place.
// Create end marker. This is needed to make sure that the node is inserted into the
// right place.
let end_marker = G::marker_with_text("/");
self.element.append_child(&end_marker);
render::insert(
&self.element,
View::new_dyn(child),
None,
Some(&end_marker),
true, /* We don't know if this is the only child or not so we pessimistically set
* this to true. */
true, /* We don't know if this is the only child or not so we pessimistically
* set this to true. */
);
return self;
}
Expand All @@ -172,8 +172,8 @@ where
View::new_dyn(child),
initial,
None,
true, /* We don't know if this is the only child or not so we pessimistically set
* this to true. */
true, /* We don't know if this is the only child or not so we pessimistically
* set this to true. */
);
return self;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/sycamore/src/generic_node/ssr_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::rc::{Rc, Weak};

use ahash::AHashMap;
use indexmap::map::IndexMap;
use once_cell::sync::Lazy;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -129,7 +129,7 @@ impl GenericNode for SsrNode {

fn element(tag: &str) -> Self {
let hk = get_next_id();
let mut attributes = AHashMap::new();
let mut attributes = IndexMap::new();
if let Some(hk) = hk {
attributes.insert("data-hk".to_string(), format!("{}.{}", hk.0, hk.1));
}
Expand Down Expand Up @@ -363,7 +363,7 @@ impl WriteToString for SsrNode {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Element {
name: String,
attributes: AHashMap<String, String>,
attributes: IndexMap<String, String>,
children: Vec<SsrNode>,
}

Expand Down
3 changes: 2 additions & 1 deletion packages/sycamore/src/utils/hydrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ thread_local! {
static HYDRATION_CONTEXT: RefCell<Option<HydrationRegistry>> = RefCell::new(None);
}

/// Run the closure inside a hydration context. If already inside a hydration context, creates a nested context.
/// Run the closure inside a hydration context. If already inside a hydration context, creates a
/// nested context.
pub fn with_hydration_context<F, R>(f: F) -> R
where
F: FnOnce() -> R,
Expand Down

0 comments on commit b9c3089

Please sign in to comment.