question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Route reuse strategy throwing RangeError: Maximum call stack size exceeded in Angular 13

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

We have a considerable large angular app, where we use custom route reuse strategy for performance gains. The route reuse strategy we use was working perfectly fine in angular 12. However, when we upgraded to Angular 13 we have started seeing the following error:

Error: Uncaught (in promise): RangeError: Maximum call stack size exceeded

The error happens in this scenario:

The inifinite loop is happening inside Angular router’s code (router.mjs) in RouterStateSnapshot’s constructor, that calls into following function:

function setRouterState(state, node) { node.value._routerState = state; node.children.forEach(c => setRouterState(state, c)); }

Here is how we have our custom route reuse strategy:

import {
	RouteReuseStrategy,
	DetachedRouteHandle,
	ActivatedRouteSnapshot,
} from "@angular/router";

export class CustomRouteReuseStrategy implements RouteReuseStrategy {
	private handlers: { [key: string]: DetachedRouteHandle } = {};

	constructor() {}

	public shouldDetach(_route: ActivatedRouteSnapshot): boolean {
		return true;
	}

	public store(
		route: ActivatedRouteSnapshot,
		handle: DetachedRouteHandle
	): void {
		this.handlers[route.data.title] = handle;
	}

	public shouldAttach(route: ActivatedRouteSnapshot): boolean {
		return !!this.handlers[route.data.title];
	}

	public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
		return this.handlers[route.data.title];
	}

	public shouldReuseRoute(
		future: ActivatedRouteSnapshot,
		curr: ActivatedRouteSnapshot
	): boolean {
		const reuse = future.routeConfig === curr.routeConfig;
		return reuse;
	}
}

Here is how we add route to Routing Module:

{ path: "post/:id", loadChildren: () => import("path-to-post-module").then( (module) => module.PostModule ), data: { title: "post" }, },

This may sound like a stack overflow question, but this setup used to work in Angular 12 without any issues. So, I’m not sure what changed in version 13, that’s causing this crash. If I remove the route reuse strategy, this error doesn’t happen, but obviously we want the route reuse in our app.

Please provide a link to a minimal reproduction of the bug

https://github.com/ChabuRah/angular-router-error

Please provide the exception or error you saw

Error: Uncaught (in promise): RangeError: Maximum call stack size exceeded
RangeError: Maximum call stack size exceeded
    at setRouterState (router.mjs:1610)
    at router.mjs:1612
    at Array.forEach (<anonymous>)
    at setRouterState (router.mjs:1612)
    at router.mjs:1612
    at Array.forEach (<anonymous>)
    at setRouterState (router.mjs:1612)
    at router.mjs:1612
    at Array.forEach (<anonymous>)
    at setRouterState (router.mjs:1612)
    at resolvePromise (zone.js:1213)
    at resolvePromise (zone.js:1167)
    at zone.js:1279
    at ZoneDelegate.invokeTask (zone.js:406)
    at Object.onInvokeTask (core.mjs:25463)
    at ZoneDelegate.invokeTask (zone.js:405)
    at Zone.runTask (zone.js:178)
    at drainMicroTaskQueue (zone.js:582)
    at invokeTask (zone.js:491)
    at ZoneTask.invoke (zone.js:476)

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 13.0.3
Node: 14.15.5
Package Manager: npm 6.14.11
OS: linux x64

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1300.3 (cli-only)
@angular-devkit/core         13.0.3 (cli-only)
@angular-devkit/schematics   13.0.3 (cli-only)
@schematics/angular          13.0.3 (cli-only)

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ChabuRahcommented, Dec 16, 2021

Thanks @JoostK for looking into it.

@atscott We have put our update on hold due to this issue, so I’ll be more than happy to know if there is any workaround until the fix is out.

0reactions
angular-automatic-lock-bot[bot]commented, Feb 4, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Maximum call stack size exceeded Lazy routing Angular 2
So I tried to implement newest lazy loading, but I got an issue RangeError: Maximum call stack size exceeded What I'm doing wrong?...
Read more >
maximum call stack size exceeded angular - You.com
1.This error occur when there is an infinite loop. As you have mentioned that the page loads when app-heroes is commented, this might...
Read more >
Router tutorial: tour of heroes - Angular
This guide describes development of a multi-page routed sample application. Along the way, it highlights key features of the router such as:.
Read more >
angular maximum call stack size exceeded Code Example
Error in data(): "RangeError: Maximum call stack size exceeded". ... An unhandled exception occurred: Maximum call stack size exceeded See C:\Users\huawei\AppData ...
Read more >
Maximum call stack size exceeded at MergeMapSubscriber ...
The main problems with routes that I have recently identified, I commented on one lazy loaded route, and everything works fine. const routes: ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found