Skip to content

Commit 6cf9b8f

Browse files
fix(UrlRouter): Use { location: 'replace' } whenever a url redirect happens
- This eliminates extra entries in the browser history.
1 parent 4ac904e commit 6cf9b8f

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/url/interface.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,10 @@ export interface MatchResult {
389389
/**
390390
* A function that matches the URL for a [[UrlRule]]
391391
*
392-
* Implementations should match against the current
393-
* [[UrlService.path]], [[UrlService.search]], and [[UrlService.hash]]
392+
* Implementations should match against the provided [[UrlParts]] and return the matched value (truthy) if the rule matches.
393+
* If this rule is selected, the matched value is passed to the [[UrlRuleHandlerFn]].
394394
*
395-
* @return truthy or falsey
395+
* @return the matched value, either truthy or falsey
396396
*/
397397
export interface UrlRuleMatchFn {
398398
(url?: UrlParts, router?: UIRouter): any;
@@ -402,7 +402,10 @@ export interface UrlRuleMatchFn {
402402
* Handler invoked when a rule is matched
403403
*
404404
* The matched value from the rule's [[UrlRuleMatchFn]] is passed as the first argument
405-
* The handler should return a string (to redirect), or void
405+
* The handler should return a string (to redirect), a [[TargetState]]/[[TargetStateDef]], or void
406+
*
407+
* If the handler returns a string, the url is replaced with the string.
408+
* If the handler returns a [[TargetState]], the target state is activated.
406409
*/
407410
export interface UrlRuleHandlerFn {
408411
(matchValue?: any, url?: UrlParts, router?: UIRouter): (string|TargetState|TargetStateDef|void);
@@ -447,13 +450,17 @@ export interface UrlRule {
447450
type: UrlRuleType;
448451

449452
/**
450-
* This function should match the url and return match details
453+
* This function should match the url and return the match details
454+
*
455+
* See [[UrlRuleMatchFn]] for details
451456
*/
452457
match: UrlRuleMatchFn;
453458

454459
/**
455-
* This function is called after the rule matched the url.
460+
* This function is called if the rule matched, and was selected as the "best match".
456461
* This function handles the rule match event.
462+
*
463+
* See [[UrlRuleHandlerFn]] for details
457464
*/
458465
handler: UrlRuleHandlerFn;
459466
}

src/url/urlRouter.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class UrlRouter implements UrlRulesApi, UrlSyncApi, Disposable {
135135
let best = this.match(url);
136136

137137
let applyResult = pattern([
138-
[isString, (newurl: string) => $url.url(newurl)],
138+
[isString, (newurl: string) => $url.url(newurl, true)],
139139
[TargetState.isDef, (def: TargetStateDef) => $state.go(def.state, def.params, def.options)],
140140
[is(TargetState), (target: TargetState) => $state.go(target.state(), target.params(), target.options())],
141141
]);
@@ -233,6 +233,9 @@ export class UrlRouter implements UrlRulesApi, UrlSyncApi, Disposable {
233233
* This api can be used directly for more control (to register [[RawUrlRule]], for example).
234234
* Rules can be created using [[UrlRouter.ruleFactory]], or create manually as simple objects.
235235
*
236+
* A rule should have a `match` function which returns truthy if the rule matched.
237+
* It should also have a `handler` function which is invoked if the rule is the best match.
238+
*
236239
* @return a function that deregisters the rule
237240
*/
238241
rule(rule: UrlRule): Function {
@@ -256,7 +259,7 @@ export class UrlRouter implements UrlRulesApi, UrlSyncApi, Disposable {
256259
/** @inheritdoc */
257260
otherwise(handler: string|UrlRuleHandlerFn|TargetState|TargetStateDef) {
258261
if (!isFunction(handler) && !isString(handler) && !is(TargetState)(handler) && !TargetState.isDef(handler)) {
259-
throw new Error("'redirectTo' must be a string, function, TargetState, or have a state: 'newtarget' property");
262+
throw new Error("'handler' must be a string, function, TargetState, or have a state: 'newtarget' property");
260263
}
261264

262265
let handlerFn: UrlRuleHandlerFn = isFunction(handler) ? handler as UrlRuleHandlerFn : val(handler);

0 commit comments

Comments
 (0)