fix router
This commit is contained in:
1 parent
612baa461e
commit
594f6779e6
1 file changed
+19
-11
+19
-11
@@ -39,9 +39,21 @@ function createRouter() {
|
||||
let route = $state<Route>(parseHash(window.location.hash));
|
||||
|
||||
// Sync from URL to state
|
||||
const handleHashChange = () => {
|
||||
route = parseHash(window.location.hash);
|
||||
};
|
||||
$effect(() => {
|
||||
const handleHashChange = () => {
|
||||
route = parseHash(window.location.hash);
|
||||
};
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('hashchange', handleHashChange);
|
||||
window.addEventListener('popstate', handleHashChange);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('hashchange', handleHashChange);
|
||||
window.removeEventListener('popstate', handleHashChange);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Sync from state to URL
|
||||
const navigate = (newRoute: Route) => {
|
||||
@@ -54,16 +66,12 @@ function createRouter() {
|
||||
route = newRoute;
|
||||
};
|
||||
|
||||
// Initialize and listen
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('hashchange', handleHashChange);
|
||||
window.addEventListener('popstate', handleHashChange);
|
||||
|
||||
// Ensure initial URL is clean if empty
|
||||
if (!window.location.hash && route.page !== 'overview') {
|
||||
// Ensure initial URL is clean if empty
|
||||
$effect(() => {
|
||||
if (typeof window !== 'undefined' && !window.location.hash && route.page !== 'overview') {
|
||||
navigate(route);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
get current() { return route; },
|
||||
|
||||
Reference in new issue
Block a user