Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router.update() state option #108

Merged
merged 2 commits into from
Jul 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RouterConfig = {
export type RouterUpdateOptions = {
push?: boolean
force?: boolean
state?: { [prop: string]: any }
with?: { [prop: string]: any }
}

Expand Down Expand Up @@ -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 = {}
}
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -309,13 +313,7 @@ export class Router {

public static async update(
url: string,
_args?:
| boolean
| {
push?: boolean
force?: boolean
with?: { [prop: string]: any }
}
_args?: boolean | RouterUpdateOptions
): Promise<boolean> {
return await Router.head.update(url, _args)
}
Expand Down Expand Up @@ -379,9 +377,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()
}

Expand Down