harden code: don't fail without react-router-dom, when history.state is null
Massimo Melina committed
Jan 1, 2026 at 11:18 UTC
031fc14dcf030a233023d0a5298d51140f7ad1c8
1 file changed
+8
-5
shared/dialogs.ts
+8
-5
@@ -94,20 +94,23 @@ async function back() {
94
}))
95
}
96
97
+const BASE_STATE = 1
98
;(async () => {
98
- while (history.state?.$dialog) { // it happens if the user reloads the browser leaving open dialogs
99
+ // this condition happens if the user reloads the browser leaving open dialogs. Don't pop BASE_STATE as it may contain other state we inherited
100
+ while (history.state?.$dialog !== undefined && history.state.$dialog !== BASE_STATE) {
101
history.back()
102
await wait(1) // history.state is not changed without this, on chrome123
103
}
104
+ history.replaceState({ ...history.state, $dialog: BASE_STATE }, '')
105
})()
106
107
export function Dialogs(props: HTMLAttributes<HTMLDivElement>) {
108
useEffect(() => domOn('popstate', () => {
109
if (ignorePopState)
110
return ignorePopState = false
108
- if (!history.state) return
109
- const { $dialog } = history.state
110
- if ($dialog && !dialogs.find(x => x.$id === $dialog)) // it happens if the user, after closing a dialog, goes forward in the history
111
+ const d = history.state?.$dialog
112
+ if (d === undefined) return // not my state, not my business
113
+ if (d !== BASE_STATE && !dialogs.find(x => x.$id === d)) // it happens if the user, after closing a dialog, goes forward in the history
114
return back()
115
closeDialog(undefined, true)
116
}), [])
@@ -230,7 +233,7 @@ export function closeDialog(v?:any, skipHistory=false) {
233
if (d.reserveClosing)
234
continue
235
if (!skipHistory) {
233
- if (history.state.$dialog !== d.$id) return
236
+ if (history.state?.$dialog !== d.$id) return
237
d.closed = back()
238
}
239
closeDialogAt(i, v)