1

I do have angular 10 application and added routing resolver for one of its competent. When I navigate to the resolver added component via different service, observed that routing resolver is calling twice for each request (problem is not the routing resolver is not working, but its executing twice per request)

customer-resolver.ts

@Injectable({
    providedIn: 'root'
})
export class CustomerResolver implements Resolve<any> {
    constructor(
        private serviceApi: ApiService,
        private stateService: StateService
    ) { }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> {
        return this.persistToState();
    }

    private async persistToState(): Promise<void> {
        let custId = this.stateService.custId;
        const data = await this.serviceApi.getCustomerData(custId);
        this.stateService.customerData = data;
    }
}

app-routing.module.ts

const routes: Routes = [
  { ... },
  {
    path: "customer",
    component: CustomerComponent,
    canActivate: [CanActivateCustomer],
    resolve: { cust: CustomerResolver }
  },
  { ... }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Any hint why routing resolver get execute twice ?

4
  • 3
    show some of your code. Commented Mar 23, 2021 at 14:33
  • @KrishnaMohan added some code the question Commented Mar 23, 2021 at 15:32
  • Check the returning value in persistToState(): Promise<void> is missing. A function whose declared type is neither 'void' nor 'any' must return a value. Commented Mar 23, 2021 at 15:53
  • 1
    @JuanPabloMorenoMartín async function is always returning a promise Commented Mar 23, 2021 at 16:53

1 Answer 1

0

this is apparently a known issue, after few searches I found the following that may be helpful, Note, If I use page refresh the issue shows resolver "receives" two calls as a result of HttpClient.get(..) But if you use a new tab, or on new browser window, it only receives the results once, and if you refresh, it gets twice, Apparently it is an activeRouter issue, and I hope they get it fixed. in my case, this problem shows only if there is non-200OK! status returned from the backend server, in successful (200OK) results, I only get 1 result, I am not sure about your particular case. Check here for known bug issue since router 3.2.0 onward

Conclusion, the behavior can be described as The resolver catches the error twice.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.