From 28becc8fafe0c481ba82b0608c3a27e8910100c2 Mon Sep 17 00:00:00 2001 From: Casey Webb Date: Thu, 19 Jul 2018 17:38:06 -0500 Subject: [PATCH 1/2] feat(router): Add state option for Router.update(); Use popstate event's state on back navigation closes #107 --- packages/router/src/router.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 88681e0a9..d37f3ec6c 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -13,6 +13,7 @@ export type RouterConfig = { export type RouterUpdateOptions = { push?: boolean force?: boolean + state?: { [prop: string]: any } with?: { [prop: string]: any } } @@ -150,6 +151,9 @@ export class Router { if (typeof args.push === 'undefined') { args.push = true } + if (typeof args.state === 'undefined') { + args.state = history.state + } if (typeof args.with === 'undefined') { args.with = {} } @@ -189,7 +193,7 @@ export class Router { await fromCtx.runBeforeDispose() history[args.push ? 'pushState' : 'replaceState']( - args.push ? {} : history.state, + args.state, document.title, toCtx.base + toCtx.path + search + hash ) @@ -314,6 +318,7 @@ export class Router { | { push?: boolean force?: boolean + state?: { [prop: string]: any } with?: { [prop: string]: any } } ): Promise { @@ -379,9 +384,10 @@ export class Router { } private static onpopstate(e: PopStateEvent) { - Router.update(Router.getPathFromLocation(), false).catch((err) => - log.error('Error navigating back', err) - ) + Router.update(Router.getPathFromLocation(), { + push: false, + state: e.state + }).catch((err) => log.error('Error navigating back', err)) e.preventDefault() } From ed6875d5a5a9b5a770d7bf30d1f6dd6b1d7bba78 Mon Sep 17 00:00:00 2001 From: Casey Webb Date: Thu, 19 Jul 2018 17:42:16 -0500 Subject: [PATCH 2/2] refactor(router): Use RouterUpdateOptions type in non-static update method definition --- packages/router/src/router.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index d37f3ec6c..e3dbe26b1 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -313,14 +313,7 @@ export class Router { public static async update( url: string, - _args?: - | boolean - | { - push?: boolean - force?: boolean - state?: { [prop: string]: any } - with?: { [prop: string]: any } - } + _args?: boolean | RouterUpdateOptions ): Promise { return await Router.head.update(url, _args) }