Skip to content

Commit 811959b

Browse files
committed
feat(router config): ignoreUnknownRoutes option
1 parent 6912e0a commit 811959b

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/app-router.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ export class AppRouter extends Router {
4343
return this._createNavigationInstruction(url)
4444
.then(instruction => this._queueInstruction(instruction))
4545
.catch(error => {
46-
logger.error(error);
47-
restorePreviousLocation(this);
46+
if (!this.ignoreUnknownRoutes) {
47+
logger.error(error);
48+
restorePreviousLocation(this);
49+
}
4850
});
4951
}
5052

src/router-configuration.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class RouterConfiguration {
1212
title: string;
1313
unknownRouteConfig: any;
1414
viewPortDefaults: any;
15+
ignoreUnknownRoutes: boolean;
1516

1617
/**
1718
* Adds a step to be run during the [[Router]]'s navigation pipeline.
@@ -94,7 +95,7 @@ export class RouterConfiguration {
9495
/**
9596
* Configures defaults to use for any view ports.
9697
*
97-
* @param viewPortConfig a view port configuration object to use as a
98+
* @param viewPortConfig a view port configuration object to use as a
9899
* default, of the form { viewPortName: { moduleId } }.
99100
* @chainable
100101
*/
@@ -177,6 +178,10 @@ export class RouterConfiguration {
177178
router.useViewPortDefaults(this.viewPortDefaults);
178179
}
179180

181+
if (this.ignoreUnknownRoutes) {
182+
router.ignoreUnknownRoutes = this.ignoreUnknownRoutes;
183+
}
184+
180185
router.options = this.options;
181186

182187
let pipelineSteps = this.pipelineSteps;

src/router.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class Router {
2121
history: History;
2222
viewPorts: Object;
2323
routes: RouteConfig[];
24+
ignoreUnknownRoutes: boolean;
2425

2526
/**
2627
* The [[Router]]'s current base URL, typically based on the [[Router.currentInstruction]].
@@ -112,6 +113,7 @@ export class Router {
112113
this._configuredPromise = new Promise(resolve => {
113114
this._resolveConfiguredPromise = resolve;
114115
});
116+
this.ignoreUnknownRoutes = false;
115117
}
116118

117119
/**
@@ -186,7 +188,7 @@ export class Router {
186188
*
187189
* @param route The name of the route to use when generating the navigation location.
188190
* @param params The route parameters to be used when populating the route pattern.
189-
* @param options The navigation options. See [[History.NavigationOptions]] for all available options.
191+
* @param options The navigation options. See [[History.NavigationOptions]] for all available options.
190192
*/
191193
navigateToRoute(route: string, params?: any, options?: any): boolean {
192194
let path = this.generate(route, params);
@@ -385,9 +387,9 @@ export class Router {
385387
}
386388
}
387389

388-
/**
390+
/**
389391
* Sets the default configuration for the view ports. This specifies how to
390-
* populate a view port for which no module is specified. The default is
392+
* populate a view port for which no module is specified. The default is
391393
* an empty view/view-model pair.
392394
*/
393395
useViewPortDefaults(viewPortDefaults: any) {

test/app-router.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ describe('app-router', () => {
208208
.then(done);
209209
});
210210

211+
it('does not restore previous location when route not found and unknown routes are ignored', (done) => {
212+
spyOn(history, 'navigate');
213+
214+
router.history.previousLocation = 'prev';
215+
router.ignoreUnknownRoutes = true;
216+
router.loadUrl('next')
217+
.then(result => {
218+
expect(result).toBeFalsy();
219+
expect(history.navigate).not.toHaveBeenCalledWith('#/prev', { trigger: false, replace: true });
220+
})
221+
.catch(result => expect(true).toBeFalsy('should have succeeded'))
222+
.then(done);
223+
});
224+
211225
it('navigate to fallback route when route not found and there is no previous location', (done) => {
212226
spyOn(history, 'navigate');
213227

0 commit comments

Comments
 (0)