diff --git a/packages/simple-router/src/matcher/index.ts b/packages/simple-router/src/matcher/index.ts index 131f189..91e0a8e 100644 --- a/packages/simple-router/src/matcher/index.ts +++ b/packages/simple-router/src/matcher/index.ts @@ -188,6 +188,7 @@ class CreateRouterMatcher { } return { fullPath, + state: location.state, name, path, hash: location.hash, diff --git a/packages/simple-router/src/query.ts b/packages/simple-router/src/query.ts index 509ab81..806fa80 100644 --- a/packages/simple-router/src/query.ts +++ b/packages/simple-router/src/query.ts @@ -99,7 +99,7 @@ export function stringifyQuery(query: LocationQueryRaw): string { export function decode(text: string | number): string { try { return decodeURIComponent(`${text}`); - } catch (err) { + } catch { console.warn(`Error decoding "${text}". Using original value`); } return `${text}`; diff --git a/packages/simple-router/src/router.tsx b/packages/simple-router/src/router.tsx index 61a9936..a83d5e9 100644 --- a/packages/simple-router/src/router.tsx +++ b/packages/simple-router/src/router.tsx @@ -130,11 +130,13 @@ class CreateRouter { } #onBeforeRouteChange = ( - { currentLocation, nextLocation }: Parameters[0], + { nextLocation }: Parameters[0], beforeEach: RouterOptions['beforeEach'], firstInit: (allNames: string[]) => void ) => { - if (nextLocation.pathname === currentLocation.pathname && this.initRoute) { + const to = this.resolve(nextLocation); + + if (to.fullPath === this.currentRoute.fullPath && this.initRoute) { return true; } @@ -147,8 +149,6 @@ class CreateRouter { this.initRoute = true; } - const to = this.resolve(nextLocation); - if (to.redirect) { if (to.redirect.startsWith('/')) { if (to.redirect === this.currentRoute.fullPath) { @@ -299,10 +299,13 @@ class CreateRouter { } push(to: RouteLocationNamedRaw | string | Location, replace?: true) { - const target = typeof to === 'string' ? to : this.resolve(to).fullPath; + const resolved = typeof to === 'string' ? { fullPath: to } : this.resolve(to); + const target = resolved.fullPath; + + const state = resolved.state || null; if (target !== this.currentRoute.fullPath) { - this.reactRouter.navigate(target, { replace }); + this.reactRouter.navigate(target, { replace, state }); } } diff --git a/packages/simple-router/src/types/index.ts b/packages/simple-router/src/types/index.ts index 41e806d..83a609a 100644 --- a/packages/simple-router/src/types/index.ts +++ b/packages/simple-router/src/types/index.ts @@ -97,7 +97,7 @@ export interface Router { go: (delta: number) => void; removeRoute: (name: string) => void; } -export interface HistoryStateArray extends Array {} +export type HistoryStateArray = Array; export type HistoryStateValue = string | number | boolean | null | undefined | HistoryState | HistoryStateArray;