Skip to content

Commit 7c23476

Browse files
committed
fix: bug with first navigation using hash router type
1 parent a33a08f commit 7c23476

File tree

2 files changed

+25
-41
lines changed

2 files changed

+25
-41
lines changed

src/router/router.ts

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ export class MobxRouter implements IMobxRouter {
3333
config.location ?? new MobxLocation(this.history, config.abortSignal);
3434
this.queryParams =
3535
config.queryParams ?? new QueryParams(this.location, this.history);
36-
37-
if (!this.location.hash && this.type === 'hash') {
38-
this.hashNavigate('/');
39-
}
4036
}
4137

4238
createPath(to: RouterToConfig): RouterPath {
@@ -81,9 +77,9 @@ export class MobxRouter implements IMobxRouter {
8177
const state = options?.state ?? null;
8278

8379
this.wrapInViewTransition(() => {
84-
this.location.hash = path.hash;
80+
this.location.hash = `#${path.pathname || '/'}`;
8581
this.history.replaceState(state, '', url);
86-
});
82+
}, options?.useStartViewTransition);
8783
}
8884

8985
protected browserNavigate(
@@ -100,13 +96,21 @@ export class MobxRouter implements IMobxRouter {
10096
} else {
10197
this.history.pushState(state, '', url);
10298
}
103-
});
99+
}, options?.useStartViewTransition);
104100
}
105101

106102
protected lastViewTransition?: ViewTransition;
107103

108-
protected wrapInViewTransition(action: () => void) {
109-
if (this.config.useStartViewTransition && document.startViewTransition) {
104+
protected wrapInViewTransition(
105+
action: () => void,
106+
useStartViewTransition?: boolean,
107+
) {
108+
if (
109+
(useStartViewTransition ||
110+
(useStartViewTransition == null &&
111+
this.config.useStartViewTransition)) &&
112+
document.startViewTransition
113+
) {
110114
if (this.lastViewTransition) {
111115
this.lastViewTransition.skipTransition();
112116
}
@@ -122,38 +126,10 @@ export class MobxRouter implements IMobxRouter {
122126
}
123127

124128
navigate(to: RouterToConfig, options?: RouterNavigateParams): void {
125-
const path = this.createPath(to);
126-
const url = this.createUrl(path, this.type);
127-
const state = options?.state ?? null;
128-
const useReplace =
129-
options?.replace || (options?.replace == null && this.type === 'hash');
130-
131-
const navigateAction = () => {
132-
if (this.type === 'hash') {
133-
this.location.hash = path.hash;
134-
}
135-
136-
if (useReplace) {
137-
this.history.replaceState(state, '', url);
138-
} else {
139-
this.history.pushState(state, '', url);
140-
}
141-
};
142-
143-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
144-
// @ts-ignore
145-
if (this.config.useStartViewTransition && document.startViewTransition) {
146-
if (this.lastViewTransition) {
147-
this.lastViewTransition.skipTransition();
148-
}
149-
this.lastViewTransition = document.startViewTransition(() => {
150-
startTransition(navigateAction);
151-
});
152-
this.lastViewTransition.finished.finally(() => {
153-
delete this.lastViewTransition;
154-
});
129+
if (this.type === 'hash') {
130+
this.hashNavigate(to, options);
155131
} else {
156-
navigateAction();
132+
this.browserNavigate(to, options);
157133
}
158134
}
159135

src/router/router.types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,12 @@ export interface RouterConfig {
7676
useStartViewTransition?: boolean;
7777
}
7878

79-
export type RouterNavigateParams = { replace?: boolean; state?: any };
79+
export type RouterNavigateParams = {
80+
replace?: boolean;
81+
state?: any;
82+
/**
83+
* experimental feature
84+
* Navigation will use https://developer.mozilla.org/en-US/docs/Web/API/Document/startViewTransition
85+
*/
86+
useStartViewTransition?: boolean;
87+
};

0 commit comments

Comments
 (0)