{"record":{"id":"48bdeb381de4a39c","repo":"yewstack/yew","slug":"failed-to-prepare-state","errorCode":null,"errorMessage":"failed to prepare state","messagePattern":"failed to prepare state","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/yew/src/functional/hooks/use_prepared_state/mod.rs","lineNumber":136,"sourceCode":"        #[cfg(feature = \"hydration\")]\n        pub has_buf: bool,\n        pub _marker: PhantomData<(T, D)>,\n    }\n\n    impl<T, D> PreparedState for PreparedStateBase<T, D>\n    where\n        D: Serialize + DeserializeOwned + PartialEq + 'static,\n        T: Serialize + DeserializeOwned + 'static,\n    {\n        #[cfg(feature = \"ssr\")]\n        fn prepare(&self) -> String {\n            use base64ct::{Base64, Encoding};\n\n            let state = bincode::serde::encode_to_vec(\n                (self.state.as_deref(), self.deps.as_deref()),\n                bincode::config::standard(),\n            )\n            .expect(\"failed to prepare state\");\n\n            Base64::encode_string(&state)\n        }\n    }\n}\n\n#[cfg(any(feature = \"hydration\", feature = \"ssr\"))]\nuse feat_any_hydration_ssr::PreparedStateBase;\n","sourceCodeStart":118,"sourceCodeEnd":145,"githubUrl":"https://github.com/yewstack/yew/blob/0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3/packages/yew/src/functional/hooks/use_prepared_state/mod.rs#L118-L145","documentation":"On the SSR path, use_prepared_state serializes (state, deps) with bincode so the value can be embedded in the page for hydration; this expect fires when that encode returns an error. Bincode only fails when the serde Serialize impl itself errors or the value cannot be represented - typical cases are custom Serialize impls returning Err for edge-case data and non-finite floats, which bincode's standard configuration rejects.","triggerScenarios":"use_prepared_state with a T or deps D containing a hand-written Serialize impl that returns an error for some values, NaN/Infinity floats, or constructs that bincode's standard config cannot encode - so SSR works locally but panics on production data.","commonSituations":"Custom Serialize/Deserialize impls for domain types; sensor or financial data containing NaN/infinite values; state types that grew unsupported constructs over time.","solutions":["Add a unit test that bincode-encodes realistic values of T and D (same call as the hook) and run it in CI","Fix or replace the failing custom serde impls; prefer derived Serialize/Deserialize","Sanitize non-finite floats (map to 0.0 or a string) before they enter prepared state","Keep prepared state to plain data - numbers, strings, Vec, HashMap - and do complex computation in components"],"exampleFix":"// before: NaN slips into prepared state, bincode rejects it\nlet price = Price { value: input.unwrap_or(f64::NAN) };\n\n// after: sanitize before the hook encodes it\nlet price = Price { value: input.filter(|v| v.is_finite()).unwrap_or(0.0) };\n\n// CI guard mirroring the hook's encode:\n#[test]\nfn prepared_state_encodes() {\n    let bytes = bincode::serde::encode_to_vec(\n        (Some(Price { value: 0.0 }), Some(())),\n        bincode::config::standard(),\n    );\n    assert!(bytes.is_ok());\n}","handlingStrategy":"validation","validationCode":"// Mirror the hook's encode in CI so bad types fail at test time, not in SSR:\n#[test]\nfn prepared_state_encodes() {\n    let bytes = bincode::serde::encode_to_vec(\n        (Some(sample_state()), Some(sample_deps())),\n        bincode::config::standard(),\n    );\n    assert!(bytes.is_ok());\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer derived Serialize/Deserialize over hand-written impls for prepared state","Sanitize NaN/Infinity floats before they reach use_prepared_state","Keep prepared state limited to plain data types"],"tags":["ssr","bincode","serialization","use-prepared-state"],"backgroundTag":"serialization-failed","analyzedSha":"0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3","analyzedAt":"2026-08-22T21:16:31.212Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}