Skip to content

Commit

Permalink
feat(router): add "onSameUrlNavigation" router configuration option (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin authored and matsko committed Oct 24, 2017
1 parent adab4f3 commit d3211a2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
11 changes: 10 additions & 1 deletion packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ export class Router {

routeReuseStrategy: RouteReuseStrategy = new DefaultRouteReuseStrategy();

/**
* Define what the router should do if it receives a navigation request to the current URL.
* By default, the router will ignore this navigation. However, this prevents features such
* as a "refresh" button. Use this option to configure the behavior when navigating to the
* current URL. Default is 'ignore'.
*/
onSameUrlNavigation: 'reload'|'ignore' = 'ignore';

/**
* Creates the router service.
*/
Expand Down Expand Up @@ -552,7 +560,8 @@ export class Router {
const url = this.urlHandlingStrategy.extract(rawUrl);
const urlTransition = !this.navigated || url.toString() !== this.currentUrlTree.toString();

if (this.urlHandlingStrategy.shouldProcessUrl(rawUrl)) {
if ((this.onSameUrlNavigation === 'reload' ? true : urlTransition) &&
this.urlHandlingStrategy.shouldProcessUrl(rawUrl)) {
(this.events as Subject<Event>).next(new NavigationStart(id, this.serializeUrl(url)));
Promise.resolve()
.then(
Expand Down
17 changes: 16 additions & 1 deletion packages/router/src/router_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,15 @@ export class RouterModule {
* Creates a module with all the router providers and directives. It also optionally sets up an
* application listener to perform an initial navigation.
*
* Options:
* Options (see {@link ExtraOptions}):
* * `enableTracing` makes the router log all its internal events to the console.
* * `useHash` enables the location strategy that uses the URL fragment instead of the history
* API.
* * `initialNavigation` disables the initial navigation.
* * `errorHandler` provides a custom error handler.
* * `preloadingStrategy` configures a preloading strategy (see {@link PreloadAllModules}).
* * `onSameUrlNavigation` configures how the router handles navigation to the current URL. See
* {@link ExtraOptions} for more details.
*/
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders {
return {
Expand Down Expand Up @@ -267,6 +270,14 @@ export interface ExtraOptions {
* Configures a preloading strategy. See {@link PreloadAllModules}.
*/
preloadingStrategy?: any;

/**
* Define what the router should do if it receives a navigation request to the current URL.
* By default, the router will ignore this navigation. However, this prevents features such
* as a "refresh" button. Use this option to configure the behavior when navigating to the
* current URL. Default is 'ignore'.
*/
onSameUrlNavigation?: 'reload'|'ignore';
}

export function setupRouter(
Expand Down Expand Up @@ -299,6 +310,10 @@ export function setupRouter(
});
}

if (opts.onSameUrlNavigation) {
router.onSameUrlNavigation = opts.onSameUrlNavigation;
}

return router;
}

Expand Down
25 changes: 15 additions & 10 deletions packages/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('Integration', () => {

describe('navigation', function() {
it('should navigate to the current URL', fakeAsync(inject([Router], (router: Router) => {
router.onSameUrlNavigation = 'reload';
router.resetConfig([
{path: '', component: SimpleCmp},
{path: 'simple', component: SimpleCmp},
Expand Down Expand Up @@ -3568,30 +3569,34 @@ describe('Integration', () => {
}]);

const events: any[] = [];
router.events.subscribe(e => onlyNavigationStartAndEnd(e) && events.push(e));
router.events.subscribe(e => e instanceof RouterEvent && events.push(e));

location.go('/include/user/kate(aux:excluded)');
advance(fixture);

expect(location.path()).toEqual('/include/user/kate(aux:excluded)');
expectEvents(
events,
[[NavigationStart, '/include/user/kate'], [NavigationEnd, '/include/user/kate']]);
expectEvents(events, [
[NavigationStart, '/include/user/kate'], [RoutesRecognized, '/include/user/kate'],
[GuardsCheckStart, '/include/user/kate'], [GuardsCheckEnd, '/include/user/kate'],
[ResolveStart, '/include/user/kate'], [ResolveEnd, '/include/user/kate'],
[NavigationEnd, '/include/user/kate']
]);
events.splice(0);

location.go('/include/user/kate(aux:excluded2)');
advance(fixture);
expectEvents(
events,
[[NavigationStart, '/include/user/kate'], [NavigationEnd, '/include/user/kate']]);
events.splice(0);
expectEvents(events, []);

router.navigateByUrl('/include/simple');
advance(fixture);

expect(location.path()).toEqual('/include/simple(aux:excluded2)');
expectEvents(
events, [[NavigationStart, '/include/simple'], [NavigationEnd, '/include/simple']]);
expectEvents(events, [
[NavigationStart, '/include/simple'], [RoutesRecognized, '/include/simple'],
[GuardsCheckStart, '/include/simple'], [GuardsCheckEnd, '/include/simple'],
[ResolveStart, '/include/simple'], [ResolveEnd, '/include/simple'],
[NavigationEnd, '/include/simple']
]);
})));
});
});
Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/router/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface ExtraOptions {
enableTracing?: boolean;
errorHandler?: ErrorHandler;
initialNavigation?: InitialNavigation;
onSameUrlNavigation?: 'reload' | 'ignore';
preloadingStrategy?: any;
useHash?: boolean;
}
Expand Down Expand Up @@ -327,6 +328,7 @@ export declare class Router {
errorHandler: ErrorHandler;
readonly events: Observable<Event>;
navigated: boolean;
onSameUrlNavigation: 'reload' | 'ignore';
routeReuseStrategy: RouteReuseStrategy;
readonly routerState: RouterState;
readonly url: string;
Expand Down

0 comments on commit d3211a2

Please sign in to comment.