{"record":{"id":"df143fc71cc0373b","repo":"DioxusLabs/dioxus","slug":"history-can-set-scroll-restoration","errorCode":null,"errorMessage":"`history` can set scroll restoration","messagePattern":"`history` can set scroll restoration","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/web/src/history.rs","lineNumber":54,"sourceCode":"\n        let current_route = dioxus_history::History::current_route(&myself);\n        let current_route_str = current_route.to_string();\n        let prefix_str = myself.prefix.as_deref().unwrap_or(\"\");\n        let current_url = format!(\"{prefix_str}{current_route_str}\");\n        let state = myself.create_state();\n        let _ = replace_state_with_url(&myself.history, &state, Some(&current_url));\n\n        myself\n    }\n\n    fn new_inner(prefix: Option<String>, do_scroll_restoration: bool) -> Self {\n        let window = window().expect(\"access to `window`\");\n        let history = window.history().expect(\"`window` has access to `history`\");\n\n        if do_scroll_restoration {\n            history\n                .set_scroll_restoration(ScrollRestoration::Manual)\n                .expect(\"`history` can set scroll restoration\");\n        }\n\n        let prefix = prefix\n            // If there isn't a base path, try to grab one from the CLI\n            .or_else(dioxus_cli_config::web_base_path)\n            // Normalize the prefix to start and end with no slashes\n            .as_ref()\n            .map(|prefix| prefix.trim_matches('/'))\n            // If the prefix is empty, don't add it\n            .filter(|prefix| !prefix.is_empty())\n            // Otherwise, start with a slash\n            .map(|prefix| format!(\"/{prefix}\"));\n\n        Self {\n            do_scroll_restoration,\n            history,\n            prefix,\n            window,","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/DioxusLabs/dioxus/blob/393d190a801ccb441d41923e232289b4f8a5c669/packages/web/src/history.rs#L36-L72","documentation":"When scroll restoration is enabled, the WebHistory constructor calls `history.set_scroll_restoration(ScrollRestoration::Manual).expect(\"`history` can set scroll restoration\")`. In browsers whose History API lacks `scrollRestoration`, the web-sys call throws and the expect panics during router startup - so the app dies at history construction, before any routing happens.","triggerScenarios":"Constructing WebHistory with do_scroll_restoration=true in a browser without History.scrollRestoration support: IE11, old Safari, legacy Android WebView / embedded engines, minimal headless engines with a partial History API.","commonSituations":"Corporate environments pinned to legacy browsers; kiosk or embedded devices shipping old webviews; CI headless engines missing parts of the History API.","solutions":["Construct with scroll restoration disabled: `WebHistory::new(prefix, false)`","Upgrade the target browser/webview - every modern engine supports History.scrollRestoration","Feature-detect `scrollRestoration` before constructing and only pass true when it exists (and report upstream so the constructor degrades gracefully instead of panicking)"],"exampleFix":"// before: panics on browsers without History.scrollRestoration\nlet history = WebHistory::new(None, true);\n// after: enable scroll restoration only when the API exists\nlet supported = web_sys::window()\n    .and_then(|w| w.history().ok())\n    .map(|h| js_sys::Reflect::get(h.as_ref(), &JsValue::from_str(\"scrollRestoration\")).is_ok())\n    .unwrap_or(false);\nlet history = WebHistory::new(None, supported);","handlingStrategy":"validation","validationCode":"fn supports_scroll_restoration() -> bool {\n    web_sys::window()\n        .and_then(|w| w.history().ok())\n        .map(|h| js_sys::Reflect::get(h.as_ref(), &wasm_bindgen::JsValue::from_str(\"scrollRestoration\")).is_ok())\n        .unwrap_or(false)\n}\n// pass supports_scroll_restoration() as do_scroll_restoration","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Feature-detect History.scrollRestoration before enabling scroll restoration; pass false on legacy engines","Track your browser support matrix - scrollRestoration is missing in IE11 and old Safari/WebView builds","Report environments that force the flag off so upstream can consider graceful degradation in the constructor"],"tags":["dioxus-web","router","history","compatibility","legacy-browser","panic"],"backgroundTag":null,"analyzedSha":"393d190a801ccb441d41923e232289b4f8a5c669","analyzedAt":"2026-08-16T11:27:45.815Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}