Repository: angular
Issue: Router's popstate listener incompatible with asynchronous route configs and when popstate fires on page load (a safari bug)
Labels: area: router,browser: safari,P5

### Which @angular/* package(s) are the source of the bug?

router

### Is this a regression?

No

### Description

We're loading routes via CMS before manually triggering an initialNavigation. Generally this works but if the browser tab crashes due to using too much memory, the page will reload but perform an initial navigation before the routes are registered, causing a white screen of death. After spending days debugging this (due to tab crashing and not being able to log anything) I've worked out that this is the ordering of events:
 - NavigationStart (popstate with restoredState) 
 - NavigationError (Cannot match any routes)
 - Add routes and call `router.initialNavigation()`

This is how we configure the router:
```ts
RouterModule.forRoot([], {
  initialNavigation: 'disabled',
  onSameUrlNavigation: 'reload',
  paramsInheritanceStrategy: 'always',
  scrollPositionRestoration: 'top',
  relativeLinkResolution: 'corrected',
}),
```

When comparing our site to competitors in the same industry, every other website is able to gracefully recover from a crashed tab except for ours which is the only Angular site. 

If it's expected behaviour then I'm not sure how we're meant to workaround it apart from get a white screen and expect the user ot refresh.

### Please provide a link to a minimal reproduction of the bug

https://github.com/intellix/angular-router-crash-initial-nav-bug

### Please provide the exception or error you saw

I've downloaded the Tour of Heroes zip, pushed it as a commit and then my reproduction on top of that here for the exact changes: https://github.com/intellix/angular-router-crash-initial-nav-bug/commit/107da944332a627316fda72dfcdd7e2a02dae600#diff-2d2675bb4687601a5c7ccf707455132f90f3516a33716185687e5c41df59ac7d

To reproduce:

```sh
git clone git@github.com:intellix/angular-router-crash-initial-nav-bug.git
cd angular-router-crash-initial-nav-bug
npm i
npm run dev:ssr --host=0.0.0.0
ifconfig | grep inet // grab your IP address
```

Then go to it on your iPhone using your IP address on the same network (if you do it on desktop, the tab doesn't really crash and restart the same).
 
Go to heroes and click the "Crash browser tab" button. You'll notice that it didn't refresh on the current route and it couldn't match a route. If you go to connect with Safari Developer Tools, you can see the list of events that occured as they're being stored (you can't use logs as the browser crashes entirely):
```
window.appEvents
```

That will look something like so:

<img width="1016" alt="Screenshot 2022-10-17 at 18 14 18" src="https://user-images.githubusercontent.com/1162531/196243833-fbd77409-a475-4916-9968-629f355f1994.png">


###
Please provide the environment you discovered this bug in (run `ng version`)

```true
Angular: 13.1.3
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic
... platform-server, router

Also tried with 14 and it's the same thing.
```


### Anything else?

Temporary workaround:

**route.service.ts:**
```ts
export class RouteService {
  events$ = this.router.events.pipe(shareReplay({ refCount: true, bufferSize: 1 }));
  constructor(private router: Router) {}
}
```

**cms-route.service.ts:**
```ts
 setup() {
  // Add routes from CMS
  this.router.config.unshift(...configs);
  this.router.config.push({
    path: '**',
    outlet: 'primary',
    pathMatch: 'full',
    redirectTo: '',
  });
  this.router.initialNavigation();

  // Force a navigation if an initial navigation happened before routes were registered
  this.routeService.events$
    .pipe(take(1), filter(isNavigationError))
    .subscribe(event => this.router.navigateByUrl(event.url));
}
```

Sorry for opening a nearly identical issue to https://github.com/angular/angular/issues/47785 - I was asked to do so with a reproduction so here it is