Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/angular/ssr/src/utils/ng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ export async function renderAngular(
// Block until application is stable.
await applicationRef.whenStable();

// This code protect againsts app destruction during bootstrapping which is a
// valid case. We should not assume the `applicationRef` is not in destroyed state.
// Calling `envInjector.get` would throw `NG0205: Injector has already been destroyed`.
if (applicationRef.destroyed) {
return {
hasNavigationError,
redirectTo: undefined,
// TODO(alanagius): let's think on the content?
// or should we use `Promise.resolve('...')`?
content: () => Promise.reject(new Error('Application was destroyed during bootstrapping')),
};
Comment on lines +107 to +113
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could change the return type of this function to

): Promise<
  | { hasNavigationError: boolean; redirectTo?: string; content: () => Promise<string> }
  | { hasNavigationError: true }
> {

and change the return to

Suggested change
return {
hasNavigationError,
redirectTo: undefined,
// TODO(alanagius): let's think on the content?
// or should we use `Promise.resolve('...')`?
content: () => Promise.reject(new Error('Application was destroyed during bootstrapping')),
};
return {
hasNavigationError: true
};

}

// TODO(alanagius): Find a way to avoid rendering here especially for redirects as any output will be discarded.
const envInjector = applicationRef.injector;
const routerIsProvided = !!envInjector.get(ActivatedRoute, null);
Expand Down
21 changes: 17 additions & 4 deletions packages/angular/ssr/test/app_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import '@angular/compiler';
/* eslint-enable import/no-unassigned-import */

import { APP_BASE_HREF } from '@angular/common';
import { Component, REQUEST, RESPONSE_INIT, inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { Component, PlatformRef, REQUEST, RESPONSE_INIT, inject } from '@angular/core';
import { ActivatedRoute, CanActivateFn, Router } from '@angular/router';
import { AngularServerApp } from '../src/app';
import { RenderMode } from '../src/routes/route-config';
import { setAngularAppTestingManifest } from './testing-utils';
Expand All @@ -26,7 +26,13 @@ describe('AngularServerApp', () => {
selector: 'app-home',
template: `Home works`,
})
class HomeComponent {}
class HomeComponent {
constructor() {
if (inject(ActivatedRoute).snapshot.data['destroyApp']) {
inject(PlatformRef).destroy();
}
}
}

@Component({
selector: 'app-redirect',
Expand Down Expand Up @@ -65,7 +71,7 @@ describe('AngularServerApp', () => {
{ path: 'home-ssg', component: HomeComponent },
{ path: 'page-with-headers', component: HomeComponent },
{ path: 'page-with-status', component: HomeComponent },

{ path: 'page-destroy-app', component: HomeComponent, data: { destroyApp: true } },
{ path: 'redirect', redirectTo: 'home' },
{ path: 'redirect-via-navigate', component: RedirectComponent },
{
Expand Down Expand Up @@ -227,6 +233,13 @@ describe('AngularServerApp', () => {
expect(response?.status).toBe(201);
});

it('should not throw an error when app destroys itself', async () => {
const response = await app.handle(new Request('http://localhost/page-destroy-app'));
// The test expects response to be null, which is reasonable - if the app destroys
// itself, there's nothing to render.
expect(response).toBeNull();
});

it('should return static `index.csr.html` for routes with CSR rendering mode', async () => {
const response = await app.handle(new Request('http://localhost/home-csr'));
const content = await response?.text();
Expand Down
Loading