Skip to content

Commit adbab25

Browse files
committed
feat(aspnetcore-engine): fixes for delaying platform destruction
With this change we replace the aspnet-engine to use the common engine to fix a number of problems. 1) Injector has already been destroyed. Error: Injector has already been destroyed. 2) Mismatching HTML snapshots. This does come with a drawback, that we needed to remove `moduleRef` from the `IEngineRenderResult` this however shouldn't cause any problem since this couldn't be used because the platform would have already been destroyed. Fixes angular#1808 Fixes angular#1404
1 parent 664b290 commit adbab25

File tree

4 files changed

+17
-190
lines changed

4 files changed

+17
-190
lines changed

modules/aspnetcore-engine/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"node": ">=10.13.0"
2121
},
2222
"dependencies": {
23+
"domino": "^2.1.6",
2324
"tslib": "TSLIB_VERSION"
2425
},
2526
"peerDependencies": {

modules/aspnetcore-engine/src/interfaces/engine-render-result.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { NgModuleRef } from '@angular/core';
98

109
export interface IEngineRenderResult {
1110
html: string;
12-
moduleRef: NgModuleRef<{}>;
1311
globals: {
1412
styles: string;
1513
title: string;

modules/aspnetcore-engine/src/main.ts

+16-60
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { DOCUMENT } from '@angular/common';
9-
import { ResourceLoader } from '@angular/compiler';
10-
import { Compiler, CompilerFactory, NgModuleFactory, StaticProvider, Type } from '@angular/core';
11-
import { platformDynamicServer } from '@angular/platform-server';
8+
import { StaticProvider } from '@angular/core';
129

1310
import { ORIGIN_URL, REQUEST } from '@nguniversal/aspnetcore-engine/tokens';
14-
import { ɵFileLoader } from '@nguniversal/common/engine';
11+
import { ɵCommonEngine as CommonEngine, ɵRenderOptions as RenderOptions } from '@nguniversal/common/engine';
12+
import { createDocument } from 'domino';
1513
import { IEngineOptions } from './interfaces/engine-options';
1614
import { IEngineRenderResult } from './interfaces/engine-render-result';
17-
import { renderModuleFactory } from './platform-server-utils';
1815

1916
/* @internal */
20-
function _getUniversalData(doc: Document, appSelector: string): Omit<IEngineRenderResult, 'moduleRef'> {
17+
function _getUniversalData(content: string, appSelector: string): IEngineRenderResult {
18+
const doc = createDocument(content, true);
2119

2220
const styles: string[] = [];
2321
const scripts: string[] = [];
@@ -63,6 +61,7 @@ function _getUniversalData(doc: Document, appSelector: string): Omit<IEngineRend
6361
};
6462
}
6563

64+
const commonEngine = new CommonEngine();
6665
export async function ngAspnetCoreEngine(options: Readonly<IEngineOptions>)
6766
: Promise<IEngineRenderResult> {
6867
if (!options.appSelector) {
@@ -71,41 +70,20 @@ export async function ngAspnetCoreEngine(options: Readonly<IEngineOptions>)
7170
for your root App component.`);
7271
}
7372

74-
// Grab the DOM "selector" from the passed in Template <app-root> for example = "app-root"
75-
const appSelector = options.appSelector.substring(1, options.appSelector.indexOf('>'));
76-
77-
const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory);
78-
const compiler: Compiler = compilerFactory.createCompiler([
79-
{
80-
providers: [
81-
{ provide: ResourceLoader, useClass: ɵFileLoader, deps: [] }
82-
]
83-
}
84-
]);
85-
86-
const moduleOrFactory = options.ngModule;
87-
if (!moduleOrFactory) {
88-
throw new Error('You must pass in a NgModule or NgModuleFactory to be bootstrapped');
89-
}
90-
91-
const extraProviders = [
92-
...(options.providers || []),
93-
getReqResProviders(options.request.origin, options.request.data.request),
94-
];
9573

96-
const factory = await getFactory(moduleOrFactory, compiler);
97-
const result = await renderModuleFactory(factory, {
98-
document: options.document || options.appSelector,
74+
const renderOptions: RenderOptions = {
9975
url: options.url || options.request.absoluteUrl,
100-
extraProviders,
101-
});
76+
document: options.document || options.appSelector,
77+
providers: [...(options.providers || []), getReqResProviders(options.request.origin, options.request.data.request)],
78+
bootstrap: options.ngModule,
79+
};
10280

103-
const doc = result.moduleRef.injector.get(DOCUMENT);
10481

105-
return {
106-
moduleRef: result.moduleRef,
107-
..._getUniversalData(doc, appSelector),
108-
};
82+
// Grab the DOM "selector" from the passed in Template <app-root> for example = "app-root"
83+
const appSelector = options.appSelector.substring(1, options.appSelector.indexOf('>'));
84+
const html = await commonEngine.render(renderOptions);
85+
86+
return _getUniversalData(html, appSelector);
10987
}
11088

11189
/**
@@ -126,25 +104,3 @@ function getReqResProviders(origin: string, request: string): StaticProvider[] {
126104
return providers;
127105
}
128106

129-
/* @internal */
130-
const factoryCacheMap = new Map<Type<{}>, NgModuleFactory<{}>>();
131-
async function getFactory(
132-
moduleOrFactory: Type<{}> | NgModuleFactory<{}>, compiler: Compiler
133-
): Promise<NgModuleFactory<{}>> {
134-
// If module has been compiled AoT
135-
if (moduleOrFactory instanceof NgModuleFactory) {
136-
return moduleOrFactory;
137-
} else {
138-
const moduleFactory = factoryCacheMap.get(moduleOrFactory);
139-
// If module factory is cached
140-
if (moduleFactory) {
141-
return moduleFactory;
142-
}
143-
144-
// Compile the module and cache it
145-
const factory = await compiler.compileModuleAsync(moduleOrFactory);
146-
factoryCacheMap.set(moduleOrFactory, factory);
147-
148-
return factory;
149-
}
150-
}

modules/aspnetcore-engine/src/platform-server-utils.ts

-128
This file was deleted.

0 commit comments

Comments
 (0)