Skip to content

Commit

Permalink
feat(@angular/ssr): export AngularAppEngine as public API
Browse files Browse the repository at this point in the history
Added `AngularAppEngine` to the public API of `@angular/ssr`, allowing users to access it directly for enhanced server-side rendering functionality.
  • Loading branch information
alan-agius4 committed Aug 28, 2024
1 parent a539da3 commit 30c25bf
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
11 changes: 11 additions & 0 deletions goldens/public-api/angular/ssr/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
```ts

// @public
export interface AngularServerAppManager {
render(request: Request, requestContext?: unknown): Promise<Response | null>;
}

// @public
export function destroyAngularAppEngine(): void;

// @public
export function getOrCreateAngularAppEngine(): AngularServerAppManager;

// (No @packageDocumentation comment for this package)

```
2 changes: 2 additions & 0 deletions packages/angular/ssr/private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export {
setAngularAppEngineManifest as ɵsetAngularAppEngineManifest,
} from './src/manifest';

export { AngularAppEngine as ɵAngularAppEngine } from './src/app-engine';

export { InlineCriticalCssProcessor as ɵInlineCriticalCssProcessor } from './src/utils/inline-critical-css';
6 changes: 6 additions & 0 deletions packages/angular/ssr/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
*/

export * from './private_export';

export {
type AngularServerAppManager,
getOrCreateAngularAppEngine,
destroyAngularAppEngine,
} from './src/app-engine';
62 changes: 58 additions & 4 deletions packages/angular/ssr/src/app-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,45 @@ import { Hooks } from './hooks';
import { getPotentialLocaleIdFromUrl } from './i18n';
import { EntryPointExports, getAngularAppEngineManifest } from './manifest';

/**
* Angular server application engine.
* Manages Angular server applications (including localized ones) and handles rendering requests.
* @developerPreview
*/
export interface AngularServerAppManager {
/**
* Renders a response for the given HTTP request using the server application.
*
* This method processes the request, determines the appropriate route and rendering context,
* and returns an HTTP response.
*
* If the request URL appears to be for a file (excluding `/index.html`), the method returns `null`.
* A request to `https://www.example.com/page/index.html` will render the Angular route
* corresponding to `https://www.example.com/page`.
*
* @param request - The incoming HTTP request object to be rendered.
* @param requestContext - Optional additional context for the request, such as metadata.
* @returns A promise that resolves to a Response object, or `null` if the request URL represents a file (e.g., `./logo.png`)
* rather than an application route.
*/
render(request: Request, requestContext?: unknown): Promise<Response | null>;
}

/**
* Angular server application engine.
* Manages Angular server applications (including localized ones), handles rendering requests,
* and optionally transforms index HTML before rendering.
*/
export class AngularAppEngine {
export class AngularAppEngine implements AngularServerAppManager {
/**
* Hooks for extending or modifying the behavior of the server application.
* These hooks are used by the Angular CLI when running the development server and
* provide extensibility points for the application lifecycle.
*
* @internal
* @private
*/
static hooks = new Hooks();
static ɵhooks = new Hooks();

/**
* Provides access to the hooks for extending or modifying the server application's behavior.
Expand All @@ -32,7 +57,7 @@ export class AngularAppEngine {
* @internal
*/
get hooks(): Hooks {
return AngularAppEngine.hooks;
return AngularAppEngine.ɵhooks;
}

/**
Expand Down Expand Up @@ -92,3 +117,32 @@ export class AngularAppEngine {
return entryPoints.get(potentialLocale);
}
}

let angularAppEngine: AngularAppEngine | undefined;

/**
* Retrieves an existing `AngularAppEngine` instance or creates a new one if none exists.
*
* This method ensures that only a single instance of `AngularAppEngine` is created and reused across
* the application lifecycle, providing efficient resource management. If the instance does not exist,
* it will be instantiated upon the first call.
*
* @developerPreview
* @returns The existing or newly created instance of `AngularAppEngine`.
*/
export function getOrCreateAngularAppEngine(): AngularServerAppManager {
return (angularAppEngine ??= new AngularAppEngine());
}

/**
* Destroys the current `AngularAppEngine` instance, releasing any associated resources.
*
* This method resets the reference to the `AngularAppEngine` instance to `undefined`, allowing
* a new instance to be created on the next call to `getOrCreateAngularAppEngine()`. It is typically
* used when reinitializing the server environment or refreshing the application state is necessary.
*
* @developerPreview
*/
export function destroyAngularAppEngine(): void {
angularAppEngine = undefined;
}

0 comments on commit 30c25bf

Please sign in to comment.