Skip to content

Commit

Permalink
feat: remove localized global state (#268)
Browse files Browse the repository at this point in the history
* feat: removed localized global state

This completely axes the feature, and returns global state to a
one-per-app system.

BREAKING CHANGE: removed localized global state (global state functions
should no longer take a locale)

* fix: transmitted global state in locale redirection pages

This fixes #267, but only when global state is unlocalized.

* fix: fixed broken examples

* fix: fixed broken demo example

* chore(deps): updated dependencies extensively

Not strictly relevant to this PR, but important nonetheless. Note that
this includes a progression of the `minify-html-onepass` crate version,
which *could* cause problems, so that should be reverted to v0.10.1 if
there are issues.
  • Loading branch information
arctic-hen7 authored Apr 3, 2023
1 parent 9aa079b commit 2017f69
Show file tree
Hide file tree
Showing 40 changed files with 118 additions and 153 deletions.
2 changes: 1 addition & 1 deletion examples/core/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/capsules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ serde_json = "1"
lazy_static = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/custom_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/error_views/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
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 @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/freezing_and_thawing/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct AppState {
}

#[engine_only_fn]
async fn get_build_state(_locale: String) -> AppState {
async fn get_build_state() -> AppState {
AppState {
test: "Hello World!".to_string(),
}
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 @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
10 changes: 3 additions & 7 deletions examples/core/global_state/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct AppState {
// Global state will be generated for each locale in your app (but we don't
// worry about that in this example)
#[engine_only_fn]
async fn get_build_state(_locale: String) -> AppState {
async fn get_build_state() -> AppState {
AppState {
test: "Hello from the build process!".to_string(),
}
Expand All @@ -34,19 +34,15 @@ async fn get_build_state(_locale: String) -> AppState {
// prevent your app from accessing global state during the build process, so be
// certain that's what you want if you go down that path.
#[engine_only_fn]
async fn get_request_state(_locale: String, _req: Request) -> AppState {
async fn get_request_state(_req: Request) -> AppState {
AppState {
test: "Hello from the server!".to_string(),
}
}

// You can even combine build state with request state, just like in a template!
#[engine_only_fn]
async fn amalgamate_states(
_locale: String,
build_state: AppState,
request_state: AppState,
) -> AppState {
async fn amalgamate_states(build_state: AppState, request_state: AppState) -> AppState {
AppState {
test: format!(
"Message from the builder: '{}' Message from the server: '{}'",
Expand Down
2 changes: 1 addition & 1 deletion examples/core/helper_build_state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
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 @@ -15,7 +15,7 @@ fluent-bundle = "0.15"
urlencoding = "2.1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
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 @@ -13,7 +13,7 @@ serde_json = "1"
wasm-bindgen-futures = "0.4"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/idb_freezing/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct AppState {
}

#[engine_only_fn]
async fn get_build_state(_locale: String) -> AppState {
async fn get_build_state() -> AppState {
AppState {
test: "Hello World!".to_string(),
}
Expand Down
2 changes: 1 addition & 1 deletion examples/core/index_view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/js_interop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
4 changes: 2 additions & 2 deletions examples/core/plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "^0.8.1"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
toml = "0.5"
toml = "0.7"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/preload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/rx_state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
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 @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"
ureq = "2"

[target.'cfg(engine)'.dependencies]
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 @@ -13,7 +13,7 @@ serde_json = "1"
anyhow = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ async fn get_build_state(
// This tells Perseus to return an error that's the client's fault, with the
// HTTP status code 404 (not found) and the message 'illegal page'. Note that
// this is a `BlamedError<std::io::Error>`, but we could use any error type that
// implements `std::error::Error` or can be converted into a boxed `std::error::Error`.
// implements `std::error::Error` or can be converted into a boxed
// `std::error::Error`.
return Err(BlamedError {
// If we used `None` instead, it would default to 400 for the client and 500 for the
// server
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 @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/core/suspense/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
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 @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
Expand Down
2 changes: 1 addition & 1 deletion examples/demos/auth/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn get_global_state_creator() -> GlobalStateCreator {
}

#[engine_only_fn]
async fn get_build_state(_locale: String) -> AppState {
async fn get_build_state() -> AppState {
AppState {
// We explicitly tell the first page that no login state has been checked yet
auth: AuthData {
Expand Down
2 changes: 1 addition & 1 deletion examples/demos/fetching/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ perseus-axum = { package = "perseus-integration", path = "../../../packages/pers
reqwest = "0.11"

[target.'cfg(client)'.dependencies]
reqwasm = "0.4"
reqwasm = "0.5"
2 changes: 1 addition & 1 deletion examples/demos/full_page_layout/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(engine)'.dev-dependencies]
fantoccini = "0.17"
fantoccini = "0.19"

[target.'cfg(engine)'.dependencies]
tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] }
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 @@ -15,7 +15,7 @@ categories = ["wasm", "web-programming::http-server", "development-tools", "asyn

[dependencies]
perseus = { path = "../perseus", version = "0.4.0-beta.22" }
actix-web = "4.2"
actix-web = "4.3"
actix-files = "0.6"
futures = "0.3"

Expand Down
1 change: 1 addition & 0 deletions packages/perseus-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ categories = ["wasm", "web-programming::http-server", "development-tools", "asyn
[dependencies]
perseus = { path = "../perseus", version = "0.4.0-beta.22" }
axum = "0.6"
# Axum requires v0.3 of this
tower-http = { version = "0.3", features = [ "fs" ] }

[features]
Expand Down
16 changes: 8 additions & 8 deletions packages/perseus-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,36 @@ path = "tests/lib.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
include_dir = "0.6"
include_dir = "0.7"
thiserror = "1"
fmterr = "0.1"
cargo_toml = "0.15"
indicatif = "=0.17.0-beta.1" # Not stable, but otherwise error handling is just about impossible
console = "0.14"
console = "0.15"
serde = "1"
serde_json = "1"
clap = { version = "3.2", features = [ "color", "derive", "unstable-v4" ] }
clap = { version = "4.2", features = [ "color", "derive" ] }
fs_extra = "1"
tokio = { version = "1", features = [ "macros", "rt-multi-thread", "sync" ] }
warp = "0.3"
command-group = "1"
ctrlc = { version = "3.0", features = ["termination"] }
command-group = "2"
ctrlc = { version = "3.2", features = ["termination"] }
notify = "=5.0.0-pre.13"
futures = "0.3"
tokio-stream = "0.1"
reqwest = { version = "0.11", features = [ "json", "stream" ] }
tar = "0.4"
flate2 = "1"
directories = "4"
directories = "5"
cargo_metadata = "0.15"
cargo-lock = "8"
minify-js = "0.4"
minify-js = "=0.4.3" # Be careful changing this, and test extensively!
walkdir = "2"

[dev-dependencies]
assert_cmd = "2"
assert_fs = "1"
predicates = "2"
predicates = "3"
ureq = "2"

[lib]
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ proc-macro = true

[dependencies]
quote = "1"
syn = "1"
syn = "1" # Needs to remain on v1 until `darling` updates
proc-macro2 = "1"
darling = "0.13"
darling = "0.14"

[dev-dependencies]
trybuild = { version = "1.0", features = ["diff"] }
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ http = "0.2"
urlencoding = "2.1"
chrono = "0.4"
# Be very careful about changing this! Patches may be required in Perseus.
minify-html-onepass = "=0.10.1"
minify-html-onepass = "=0.10.8"

# These dependencies will also be available in documentation
[target.'cfg(any(client, clientdoc))'.dependencies]
rexie = { version = "0.4", optional = true, default-features = false }
js-sys = { version = "0.3", optional = true }
# Note that this is not needed in production, but that can't be specified, so it will just be compiled away to nothing
console_error_panic_hook = { version = "0.1.6", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
# TODO review feature flags here
web-sys = { version = "0.3", features = [ "Headers", "Navigator", "NodeList", "Request", "RequestInit", "RequestMode", "Response", "ReadableStream", "Window", "CustomEvent", "CustomEventInit" ] }
wasm-bindgen = "0.2"
Expand Down
1 change: 1 addition & 0 deletions packages/perseus/src/reactor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl<G: Html, M: MutableStore, T: TranslationsManager> TryFrom<PerseusAppBase<G,
return Err(ClientInvariantError::RenderCfg.into())
}
};
// NOTE: This will be transmitted on all pages, including local redirection ones
let global_state_ty = match WindowVariable::<Value>::new_obj("__PERSEUS_GLOBAL_STATE") {
WindowVariable::Some(val) => {
let state = TemplateState::from_value(val);
Expand Down
12 changes: 11 additions & 1 deletion packages/perseus/src/server/html_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,18 @@ impl HtmlShell {
///
/// Further, this will preload the Wasm binary, making redirection snappier
/// (but initial load slower), a tradeoff that generally improves UX.
pub(crate) fn locale_redirection_fallback(mut self, redirect_url: &str) -> Self {
pub(crate) fn locale_redirection_fallback(
mut self,
redirect_url: &str,
global_state: &TemplateState,
) -> Self {
self.locale = "xx-XX".to_string();

// We still have to inject the global state, which is unlocalized (see #267)
let global_state = escape_page_data(&global_state.state.to_string());
let global_state = format!("window.__PERSEUS_GLOBAL_STATE = `{}`;", global_state);
self.scripts_before_boundary.push(global_state);

// This will be used if JavaScript is completely disabled (it's then the site's
// responsibility to show a further message)
let dumb_redirect = format!(
Expand Down
Loading

0 comments on commit 2017f69

Please sign in to comment.