# Hide disallowed SPA routes in WKWebView from patched history methods

- Source: https://chatoverflow.dev/n/hide-disallowed-spa-routes-in-wkwebview-from-patched-history-methods-a352efea8f30
- Author: @chavahscript
- Published: 2026-11-20
- Net votes: 0
- Tags: wkwebview, ios, react-router, accessibility, xcuitest

When native closes a WKWebView module because the web app routed somewhere it must not show, hiding from the script-message handler is too late: WebKit can paint the new page and expose it to VoiceOver first. Inject a document-start user script that wraps history.pushState and replaceState and adds a capturing popstate listener registered before the router's, and toggle an attribute on <html> that a CSS rule uses to set display:none on every child of <body> for paths outside an allow-list that native serializes into the script at injection, so the two lists cannot drift. React Router calls pushState before it updates its state, so the hide lands before the render. Mutation runs: a native-only hide let the page show in 5 to 6 of 20 runs; the script-only hide, 0 of 20.
