Skip to content

Commit ef95fa0

Browse files
committed
fix(bridge): correct unhandled promise rejection: undefined
fact: This error is occurred by aborting a pending navigation. Reason: The first router initialization might override first bridge calling. We use a callback queue to delay all navigations until router initialization completed. - https://github.com/vuejs/vue-router/blob/v3.2.0/src/history/base.js#L187-L189 - vuejs/vue-router#2932 - https://stackoverflow.com/questions/57897511/in-vue-router-why-would-this-happen-this1-pending-route
1 parent a739775 commit ef95fa0

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/bridge/helper.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ interface SignalHandler {
1616
disconnect: Function
1717
}
1818

19+
type BridgeNavigator = () => unknown
20+
21+
let isRouterLoaded = false
22+
const navigatorQueue: BridgeNavigator[] = []
23+
24+
export type Pusher = ReturnType<typeof createPusher>
25+
26+
export function switchRouterLoaded() {
27+
if (isRouterLoaded) return
28+
29+
isRouterLoaded = true
30+
if (navigatorQueue.length) {
31+
navigatorQueue.forEach(navigator => navigator())
32+
navigatorQueue.length = 0
33+
}
34+
}
35+
36+
export function queueNavigator(navigator: BridgeNavigator): void {
37+
if (isRouterLoaded) {
38+
navigator()
39+
return
40+
}
41+
navigatorQueue.push(navigator)
42+
}
43+
1944
export function dispatch(payload: string) {
2045
const { type, ...otherPayload }: DispatchPayload = JSON.parse(payload)
2146

@@ -28,7 +53,13 @@ export function dispatch(payload: string) {
2853
* function will always be invoked.
2954
*/
3055
if (/.+\/#\/$/.test(location.href)) {
31-
RECEIVER_MAP[type] && RECEIVER_MAP[type](otherPayload)
56+
const navigator = RECEIVER_MAP[type]
57+
if (navigator) {
58+
queueNavigator(() => navigator(otherPayload))
59+
} else {
60+
// eslint-disable-next-line no-console
61+
console.error(`[Receiver]: Unregister navigator type -> ${type}`)
62+
}
3263
}
3364
}
3465

src/bridge/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import {
88
createProp,
99
registerSignalListener
1010
} from './helper'
11-
import {
12-
SCOPES,
13-
ScopeName,
14-
SIGNAL_CALLBACKS,
15-
SignalName
16-
} from '@/config/bridge'
11+
import { SCOPES, ScopeName } from '@/config/bridge'
1712

1813
declare global {
1914
interface Window {

src/router/guards.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { switchRouterLoaded } from '@/bridge/helper'
2+
3+
function ensureRouterLoaded() {
4+
switchRouterLoaded()
5+
}
6+
7+
export const afterEach = [ensureRouterLoaded]

src/router/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Vue from 'vue'
22
import Router from 'vue-router'
33
import Home from '@/views/Home.vue'
4+
import { afterEach } from './guards'
45

56
Vue.use(Router)
67

7-
export default new Router({
8+
const router = new Router({
89
routes: [
910
{
1011
path: '/',
@@ -22,3 +23,7 @@ export default new Router({
2223
}
2324
]
2425
})
26+
27+
afterEach.forEach(hook => router.afterEach(hook))
28+
29+
export default router

0 commit comments

Comments
 (0)