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

Fix hydration by not inserting hydration keys in <head> #137

Merged
merged 6 commits into from
May 10, 2022
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
4 changes: 2 additions & 2 deletions examples/core/basic/.perseus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ edition = "2021"
app = { package = "perseus-example-basic", path = "../" }

perseus = { path = "../../../../packages/perseus" }
sycamore = { version = "=0.8.0-beta.4", features = ["ssr"] }
sycamore-router = "=0.8.0-beta.4"
sycamore = { version = "=0.8.0-beta.5", features = ["ssr"] }
sycamore-router = "=0.8.0-beta.5"
web-sys = { version = "0.3", features = ["Event", "Headers", "Request", "RequestInit", "RequestMode", "Response", "ReadableStream", "Window"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion examples/core/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/core/freezing_and_thawing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/core/global_state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/core/i18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "translator-fluent", "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
fluent-bundle = "0.15"
Expand Down
2 changes: 1 addition & 1 deletion examples/core/idb_freezing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate", "idb-freezing" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
wasm-bindgen-futures = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion examples/core/plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus" }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
toml = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/core/router_state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
2 changes: 1 addition & 1 deletion examples/core/rx_state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/core/set_headers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/core/state_generation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/core/static_content/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion examples/core/unreactive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
perseus = { path = "../../../packages/perseus", features = [] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Expand Down
2 changes: 1 addition & 1 deletion packages/perseus-actix-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ serde_json = "1"
thiserror = "1"
fmterr = "0.1"
futures = "0.3"
sycamore = { version = "=0.8.0-beta.4", features = ["ssr"] }
sycamore = { version = "=0.8.0-beta.5", features = ["ssr"] }
4 changes: 2 additions & 2 deletions packages/perseus-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ syn = "1"
proc-macro2 = "1"
darling = "0.13"
serde_json = "1"
sycamore-reactive = "=0.8.0-beta.4"
sycamore-reactive = "=0.8.0-beta.5"
regex = "1"

[dev-dependencies]
trybuild = { version = "1.0", features = ["diff"] }
sycamore = "=0.8.0-beta.4"
sycamore = "=0.8.0-beta.5"
serde = { version = "1", features = [ "derive" ] }

[features]
Expand Down
2 changes: 1 addition & 1 deletion packages/perseus-warp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ serde_json = "1"
thiserror = "1"
fmterr = "0.1"
futures = "0.3"
sycamore = { version = "=0.8.0-beta.4", features = ["ssr"] }
sycamore = { version = "=0.8.0-beta.5", features = ["ssr"] }
6 changes: 3 additions & 3 deletions packages/perseus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ categories = ["wasm", "web-programming", "development-tools", "asynchronous", "g
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sycamore = { version = "=0.8.0-beta.4", features = [ "ssr" ] }
sycamore-router = "=0.8.0-beta.4"
sycamore-futures = "=0.8.0-beta.4"
sycamore = { version = "=0.8.0-beta.5", features = [ "ssr" ] }
sycamore-router = "=0.8.0-beta.5"
sycamore-futures = "=0.8.0-beta.5"
perseus-macro = { path = "../perseus-macro", version = "0.3.5" }
# TODO review feature flags here
web-sys = { version = "0.3", features = [ "Headers", "Navigator", "NodeList", "Request", "RequestInit", "RequestMode", "Response", "ReadableStream", "Window" ] }
Expand Down
11 changes: 6 additions & 5 deletions packages/perseus/src/template/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::Request;
use crate::SsrNode;
use futures::Future;
use http::header::HeaderMap;
use sycamore::prelude::{Scope, View};
use sycamore::prelude::{create_scope_immediate, Scope, View};
use sycamore::utils::hydrate::with_no_hydration_context;

/// A generic error type that can be adapted for any errors the user may want to return from a render function. `.into()` can be used
/// to convert most error types into this without further hassle. Otherwise, use `Box::new()` on the type.
Expand Down Expand Up @@ -202,17 +203,17 @@ impl<G: Html> Template<G> {

(self.template)(cx, props)
}
/// Executes the user-given function that renders the document `<head>`, returning a string to be interpolated manually. Reactivity
/// in this function will not take effect due to this string rendering. Note that this function will provide a translator context.
/// Executes the user-given function that renders the document `<head>`, returning a string to be interpolated manually.
/// Reactivity in this function will not take effect due to this string rendering. Note that this function will provide a translator context.
pub fn render_head_str(&self, props: PageProps, translator: &Translator) -> String {
sycamore::render_to_string(|cx| {
// The context we have here has no context elements set on it, so we set all the defaults (job of the router component on the client-side)
// We don't need the value, we just want the context instantiations
let _ = RenderCtx::default().set_ctx(cx);
// And now provide a translator separately
provide_context_signal_replace(cx, translator.clone());

(self.head)(cx, props)
// We don't want to generate hydration keys for the head because it is static.
with_no_hydration_context(|| (self.head)(cx, props))
})
}
/// Gets the list of templates that should be prerendered for at build-time.
Expand Down
12 changes: 2 additions & 10 deletions packages/perseus/src/utils/context.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use sycamore::prelude::{
create_signal, provide_context_ref, try_use_context, use_context, Scope, Signal,
};
use sycamore::prelude::{create_signal, use_context_or_else_ref, Scope, Signal};

/// Adds the given value to the given reactive scope inside a `Signal`, replacing a value of that type if one is already present. This returns a reference to the `Signal` inserted.
pub(crate) fn provide_context_signal_replace<T: 'static>(cx: Scope, val: T) -> &Signal<T> {
if let Some(ctx) = try_use_context::<Signal<T>>(cx) {
ctx.set(val);
} else {
provide_context_ref(cx, create_signal(cx, val));
}

use_context(cx)
use_context_or_else_ref(cx, || create_signal(cx, val))
}