Skip to content

Commit

Permalink
[branch] Add dontUpdateUrl option to Router.navigate
Browse files Browse the repository at this point in the history
  • Loading branch information
mduan committed Jun 17, 2022
1 parent 3b2ac99 commit f989d8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ export interface AnyAttrs {
[attr: string]: any;
}

export interface NavigateOptions {
// Passing this flag to navigate() will cause the matching router handler to execute, but won't update the url
// with the passed fragment.
dontUpdateUrl?: boolean;
}

export class Component<
StateT,
AttrsT = AnyAttrs,
Expand Down Expand Up @@ -240,7 +246,7 @@ export class Component<
* Executes the route handler matching the given URL fragment, and updates
* the URL, as though the user had navigated explicitly to that address.
*/
navigate(fragment: string, stateUpdate?: Partial<StateT>): void;
navigate(fragment: string, stateUpdate?: Partial<StateT>, navigateOptions?: NavigateOptions): void;

/** Run a user-defined hook with the given parameters */
runHook: (
Expand Down
10 changes: 7 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ export default class Router {
this.window.history[this.historyMethod] = this.origChangeStateMethod;
}

navigate(fragment, stateUpdate = {}) {
navigate(fragment, stateUpdate = {}, navigateOptions = {}) {
const {dontUpdateUrl = false} = navigateOptions;

fragment = stripHash(fragment);
if (decodedFragmentsEqual(this.app.state.$fragment, fragment) && !Object.keys(stateUpdate).length) {
return;
}

stateUpdate.$fragment = fragment;
if (!dontUpdateUrl) {
stateUpdate.$fragment = fragment;
}
for (const route of this.compiledRoutes) {
const matches = route.expr.exec(fragment);
if (matches) {
Expand All @@ -93,7 +97,7 @@ export default class Router {
if (!routeHandler) {
throw `No route handler defined for #${fragment}`;
}
const routeStateUpdate = routeHandler.call(this.app, stateUpdate, ...params);
const routeStateUpdate = routeHandler.call(this.app, stateUpdate, ...params, navigateOptions);
if (routeStateUpdate) {
// don't update if route handler returned a falsey result
this.app.update(Object.assign({}, stateUpdate, routeStateUpdate));
Expand Down
8 changes: 8 additions & 0 deletions test/browser/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ describe(`Router`, function () {
await nextAnimationFrame();
expect(this.routerApp.textContent).to.equal(`Number: 42`);
});

it(`does not update url if dontUpdateUrl is passed`, async function () {
expect(window.location.hash).to.equal(``);
this.routerApp.router.navigate(`numeric/42`, {}, {dontUpdateUrl: true});
await retryable(() => expect(this.routerApp.textContent).to.equal(`Number: 42`));
expect(this.routerApp.textContent).to.equal(`Number: 42`);
expect(window.location.hash).to.equal(``);
});
});

describe(`replaceHash()`, function () {
Expand Down

0 comments on commit f989d8c

Please sign in to comment.