Skip to content

Commit

Permalink
fix: Update baseUrl when creating a navigation instruction
Browse files Browse the repository at this point in the history
This change hastens the update of any routers baseUrl from the CommitChangesStep to the BuildNavigationPlanStep.

Closes #378 #605
  • Loading branch information
davismj committed Jul 16, 2018
1 parent c34f6a4 commit e801550
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/navigation-instruction.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export class NavigationInstruction {

this.config.navModel.isActive = true;

router._refreshBaseUrl();
router.refreshNavigation();

let loads = [];
Expand Down
27 changes: 18 additions & 9 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ export class Router {

_refreshBaseUrl(): void {
if (this.parent) {
let baseUrl = this.parent.currentInstruction.getBaseUrl();
this.baseUrl = this.parent.baseUrl + baseUrl;
this.baseUrl = generateBaseUrl(this.parent, this.parent.currentInstruction);
}
}

Expand Down Expand Up @@ -486,6 +485,8 @@ export class Router {
}
};

let result;

if (results && results.length) {
let first = results[0];
let instruction = new NavigationInstruction(Object.assign({}, instructionInit, {
Expand All @@ -495,20 +496,20 @@ export class Router {
}));

if (typeof first.handler === 'function') {
return evaluateNavigationStrategy(instruction, first.handler, first);
result = evaluateNavigationStrategy(instruction, first.handler, first);
} else if (first.handler && typeof first.handler.navigationStrategy === 'function') {
return evaluateNavigationStrategy(instruction, first.handler.navigationStrategy, first.handler);
result = evaluateNavigationStrategy(instruction, first.handler.navigationStrategy, first.handler);
} else {
result = Promise.resolve(instruction);
}

return Promise.resolve(instruction);
} else if (this.catchAllHandler) {
let instruction = new NavigationInstruction(Object.assign({}, instructionInit, {
params: { path: fragment },
queryParams: results ? results.queryParams : {},
config: null // config will be created by the catchAllHandler
}));

return evaluateNavigationStrategy(instruction, this.catchAllHandler);
result = evaluateNavigationStrategy(instruction, this.catchAllHandler);
} else if (this.parent) {
let router = this._parentCatchAllHandler(this.parent);

Expand All @@ -524,11 +525,15 @@ export class Router {
config: null // config will be created by the chained parent catchAllHandler
}));

return evaluateNavigationStrategy(instruction, router.catchAllHandler);
result = evaluateNavigationStrategy(instruction, router.catchAllHandler);
}
}

return Promise.reject(new Error(`Route not found: ${url}`));
if (result && parentInstruction) {
this.baseUrl = generateBaseUrl(this.parent, parentInstruction);
}

return result || Promise.reject(new Error(`Route not found: ${url}`));
}

_findParentInstructionFromRouter(router: Router, instruction: NavigationInstruction): NavigationInstruction {
Expand Down Expand Up @@ -575,6 +580,10 @@ export class Router {
}
}

function generateBaseUrl(router: Router, instruction: NavigationInstruction) {
return `${router.baseUrl || ''}${instruction.getBaseUrl() || ''}`;
}

function validateRouteConfig(config: RouteConfig, routes: Array<Object>): void {
if (typeof config !== 'object') {
throw new Error('Invalid Route Config');
Expand Down

0 comments on commit e801550

Please sign in to comment.