@angular/ssr 19.0.0-next.0 → 19.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm2022/private_export.mjs +10 -0
- package/esm2022/public_api.mjs +1 -1
- package/esm2022/src/app-engine.mjs +18 -40
- package/esm2022/src/app.mjs +110 -28
- package/esm2022/src/assets.mjs +1 -1
- package/esm2022/src/routes/ng-routes.mjs +6 -4
- package/fesm2022/ssr.mjs +603 -6
- package/fesm2022/ssr.mjs.map +1 -1
- package/index.d.ts +258 -0
- package/package.json +8 -8
- package/esm2022/src/render.mjs +0 -74
package/fesm2022/ssr.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { URL as URL$1 } from 'node:url';
|
|
|
5
5
|
import Critters from 'critters';
|
|
6
6
|
import { readFile } from 'node:fs/promises';
|
|
7
7
|
import { APP_BASE_HREF, PlatformLocation } from '@angular/common';
|
|
8
|
-
import { ɵConsole as _Console, ɵresetCompiledComponents as _resetCompiledComponents, createPlatformFactory, platformCore, ApplicationRef, ɵwhenStable as _whenStable, Compiler } from '@angular/core';
|
|
8
|
+
import { ɵConsole as _Console, ɵresetCompiledComponents as _resetCompiledComponents, createPlatformFactory, platformCore, ApplicationRef, ɵwhenStable as _whenStable, Compiler, InjectionToken } from '@angular/core';
|
|
9
9
|
import { ɵloadChildren as _loadChildren, Router } from '@angular/router';
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -582,10 +582,12 @@ function resolveRedirectTo(routePath, redirectTo) {
|
|
|
582
582
|
* @returns A promise that resolves to an object of type `AngularRouterConfigResult`.
|
|
583
583
|
*/
|
|
584
584
|
async function getRoutesFromAngularRouterConfig(bootstrap, document, url) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
585
|
+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
|
586
|
+
// Need to clean up GENERATED_COMP_IDS map in `@angular/core`.
|
|
587
|
+
// Otherwise an incorrect component ID generation collision detected warning will be displayed in development.
|
|
588
|
+
// See: https://github.com/angular/angular-cli/issues/25924
|
|
589
|
+
_resetCompiledComponents();
|
|
590
|
+
}
|
|
589
591
|
const { protocol, host } = url;
|
|
590
592
|
// Create and initialize the Angular platform for server-side rendering.
|
|
591
593
|
const platformRef = createPlatformFactory(platformCore, 'server', [
|
|
@@ -641,5 +643,600 @@ async function getRoutesFromAngularRouterConfig(bootstrap, document, url) {
|
|
|
641
643
|
}
|
|
642
644
|
}
|
|
643
645
|
|
|
644
|
-
|
|
646
|
+
/**
|
|
647
|
+
* Manages server-side assets.
|
|
648
|
+
*/
|
|
649
|
+
class ServerAssets {
|
|
650
|
+
manifest;
|
|
651
|
+
/**
|
|
652
|
+
* Creates an instance of ServerAsset.
|
|
653
|
+
*
|
|
654
|
+
* @param manifest - The manifest containing the server assets.
|
|
655
|
+
*/
|
|
656
|
+
constructor(manifest) {
|
|
657
|
+
this.manifest = manifest;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Retrieves the content of a server-side asset using its path.
|
|
661
|
+
*
|
|
662
|
+
* @param path - The path to the server asset.
|
|
663
|
+
* @returns A promise that resolves to the asset content as a string.
|
|
664
|
+
* @throws Error If the asset path is not found in the manifest, an error is thrown.
|
|
665
|
+
*/
|
|
666
|
+
async getServerAsset(path) {
|
|
667
|
+
const asset = this.manifest.assets.get(path);
|
|
668
|
+
if (!asset) {
|
|
669
|
+
throw new Error(`Server asset '${path}' does not exist.`);
|
|
670
|
+
}
|
|
671
|
+
return asset();
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Retrieves and caches the content of 'index.server.html'.
|
|
675
|
+
*
|
|
676
|
+
* @returns A promise that resolves to the content of 'index.server.html'.
|
|
677
|
+
* @throws Error If there is an issue retrieving the asset.
|
|
678
|
+
*/
|
|
679
|
+
getIndexServerHtml() {
|
|
680
|
+
return this.getServerAsset('index.server.html');
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Manages a collection of hooks and provides methods to register and execute them.
|
|
686
|
+
* Hooks are functions that can be invoked with specific arguments to allow modifications or enhancements.
|
|
687
|
+
*/
|
|
688
|
+
class Hooks {
|
|
689
|
+
/**
|
|
690
|
+
* A map of hook names to arrays of hook functions.
|
|
691
|
+
* Each hook name can have multiple associated functions, which are executed in sequence.
|
|
692
|
+
*/
|
|
693
|
+
store = new Map();
|
|
694
|
+
/**
|
|
695
|
+
* Executes all hooks associated with the specified name, passing the given argument to each hook function.
|
|
696
|
+
* The hooks are invoked sequentially, and the argument may be modified by each hook.
|
|
697
|
+
*
|
|
698
|
+
* @template Hook - The type of the hook name. It should be one of the keys of `HooksMapping`.
|
|
699
|
+
* @param name - The name of the hook whose functions will be executed.
|
|
700
|
+
* @param context - The input value to be passed to each hook function. The value is mutated by each hook function.
|
|
701
|
+
* @returns A promise that resolves once all hook functions have been executed.
|
|
702
|
+
*
|
|
703
|
+
* @example
|
|
704
|
+
* ```typescript
|
|
705
|
+
* const hooks = new Hooks();
|
|
706
|
+
* hooks.on('html:transform:pre', async (ctx) => {
|
|
707
|
+
* ctx.html = ctx.html.replace(/foo/g, 'bar');
|
|
708
|
+
* return ctx.html;
|
|
709
|
+
* });
|
|
710
|
+
* const result = await hooks.run('html:transform:pre', { html: '<div>foo</div>' });
|
|
711
|
+
* console.log(result); // '<div>bar</div>'
|
|
712
|
+
* ```
|
|
713
|
+
* @internal
|
|
714
|
+
*/
|
|
715
|
+
async run(name, context) {
|
|
716
|
+
const hooks = this.store.get(name);
|
|
717
|
+
switch (name) {
|
|
718
|
+
case 'html:transform:pre': {
|
|
719
|
+
if (!hooks) {
|
|
720
|
+
return context.html;
|
|
721
|
+
}
|
|
722
|
+
const ctx = { ...context };
|
|
723
|
+
for (const hook of hooks) {
|
|
724
|
+
ctx.html = await hook(ctx);
|
|
725
|
+
}
|
|
726
|
+
return ctx.html;
|
|
727
|
+
}
|
|
728
|
+
default:
|
|
729
|
+
throw new Error(`Running hook "${name}" is not supported.`);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Registers a new hook function under the specified hook name.
|
|
734
|
+
* This function should be a function that takes an argument of type `T` and returns a `string` or `Promise<string>`.
|
|
735
|
+
*
|
|
736
|
+
* @template Hook - The type of the hook name. It should be one of the keys of `HooksMapping`.
|
|
737
|
+
* @param name - The name of the hook under which the function will be registered.
|
|
738
|
+
* @param handler - A function to be executed when the hook is triggered. The handler will be called with an argument
|
|
739
|
+
* that may be modified by the hook functions.
|
|
740
|
+
*
|
|
741
|
+
* @remarks
|
|
742
|
+
* - If there are existing handlers registered under the given hook name, the new handler will be added to the list.
|
|
743
|
+
* - If no handlers are registered under the given hook name, a new list will be created with the handler as its first element.
|
|
744
|
+
*
|
|
745
|
+
* @example
|
|
746
|
+
* ```typescript
|
|
747
|
+
* hooks.on('html:transform:pre', async (ctx) => {
|
|
748
|
+
* return ctx.html.replace(/foo/g, 'bar');
|
|
749
|
+
* });
|
|
750
|
+
* ```
|
|
751
|
+
*/
|
|
752
|
+
on(name, handler) {
|
|
753
|
+
const hooks = this.store.get(name);
|
|
754
|
+
if (hooks) {
|
|
755
|
+
hooks.push(handler);
|
|
756
|
+
}
|
|
757
|
+
else {
|
|
758
|
+
this.store.set(name, [handler]);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Checks if there are any hooks registered under the specified name.
|
|
763
|
+
*
|
|
764
|
+
* @param name - The name of the hook to check.
|
|
765
|
+
* @returns `true` if there are hooks registered under the specified name, otherwise `false`.
|
|
766
|
+
*/
|
|
767
|
+
has(name) {
|
|
768
|
+
return !!this.store.get(name)?.length;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* The Angular app manifest object.
|
|
774
|
+
* This is used internally to store the current Angular app manifest.
|
|
775
|
+
*/
|
|
776
|
+
let angularAppManifest;
|
|
777
|
+
/**
|
|
778
|
+
* Sets the Angular app manifest.
|
|
779
|
+
*
|
|
780
|
+
* @param manifest - The manifest object to set for the Angular application.
|
|
781
|
+
*/
|
|
782
|
+
function setAngularAppManifest(manifest) {
|
|
783
|
+
angularAppManifest = manifest;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Gets the Angular app manifest.
|
|
787
|
+
*
|
|
788
|
+
* @returns The Angular app manifest.
|
|
789
|
+
* @throws Will throw an error if the Angular app manifest is not set.
|
|
790
|
+
*/
|
|
791
|
+
function getAngularAppManifest() {
|
|
792
|
+
if (!angularAppManifest) {
|
|
793
|
+
throw new Error('Angular app manifest is not set. ' +
|
|
794
|
+
`Please ensure you are using the '@angular/build:application' builder to build your server application.`);
|
|
795
|
+
}
|
|
796
|
+
return angularAppManifest;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* The Angular app engine manifest object.
|
|
800
|
+
* This is used internally to store the current Angular app engine manifest.
|
|
801
|
+
*/
|
|
802
|
+
let angularAppEngineManifest;
|
|
803
|
+
/**
|
|
804
|
+
* Sets the Angular app engine manifest.
|
|
805
|
+
*
|
|
806
|
+
* @param manifest - The engine manifest object to set.
|
|
807
|
+
*/
|
|
808
|
+
function setAngularAppEngineManifest(manifest) {
|
|
809
|
+
angularAppEngineManifest = manifest;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Gets the Angular app engine manifest.
|
|
813
|
+
*
|
|
814
|
+
* @returns The Angular app engine manifest.
|
|
815
|
+
* @throws Will throw an error if the Angular app engine manifest is not set.
|
|
816
|
+
*/
|
|
817
|
+
function getAngularAppEngineManifest() {
|
|
818
|
+
if (!angularAppEngineManifest) {
|
|
819
|
+
throw new Error('Angular app engine manifest is not set. ' +
|
|
820
|
+
`Please ensure you are using the '@angular/build:application' builder to build your server application.`);
|
|
821
|
+
}
|
|
822
|
+
return angularAppEngineManifest;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* A route tree implementation that supports efficient route matching, including support for wildcard routes.
|
|
827
|
+
* This structure is useful for organizing and retrieving routes in a hierarchical manner,
|
|
828
|
+
* enabling complex routing scenarios with nested paths.
|
|
829
|
+
*/
|
|
830
|
+
class RouteTree {
|
|
831
|
+
/**
|
|
832
|
+
* The root node of the route tree.
|
|
833
|
+
* All routes are stored and accessed relative to this root node.
|
|
834
|
+
*/
|
|
835
|
+
root = this.createEmptyRouteTreeNode('');
|
|
836
|
+
/**
|
|
837
|
+
* A counter that tracks the order of route insertion.
|
|
838
|
+
* This ensures that routes are matched in the order they were defined,
|
|
839
|
+
* with earlier routes taking precedence.
|
|
840
|
+
*/
|
|
841
|
+
insertionIndexCounter = 0;
|
|
842
|
+
/**
|
|
843
|
+
* Inserts a new route into the route tree.
|
|
844
|
+
* The route is broken down into segments, and each segment is added to the tree.
|
|
845
|
+
* Parameterized segments (e.g., :id) are normalized to wildcards (*) for matching purposes.
|
|
846
|
+
*
|
|
847
|
+
* @param route - The route path to insert into the tree.
|
|
848
|
+
* @param metadata - Metadata associated with the route, excluding the route path itself.
|
|
849
|
+
*/
|
|
850
|
+
insert(route, metadata) {
|
|
851
|
+
let node = this.root;
|
|
852
|
+
const normalizedRoute = stripTrailingSlash(route);
|
|
853
|
+
const segments = normalizedRoute.split('/');
|
|
854
|
+
for (const segment of segments) {
|
|
855
|
+
// Replace parameterized segments (e.g., :id) with a wildcard (*) for matching
|
|
856
|
+
const normalizedSegment = segment[0] === ':' ? '*' : segment;
|
|
857
|
+
let childNode = node.children.get(normalizedSegment);
|
|
858
|
+
if (!childNode) {
|
|
859
|
+
childNode = this.createEmptyRouteTreeNode(normalizedSegment);
|
|
860
|
+
node.children.set(normalizedSegment, childNode);
|
|
861
|
+
}
|
|
862
|
+
node = childNode;
|
|
863
|
+
}
|
|
864
|
+
// At the leaf node, store the full route and its associated metadata
|
|
865
|
+
node.metadata = {
|
|
866
|
+
...metadata,
|
|
867
|
+
route: normalizedRoute,
|
|
868
|
+
};
|
|
869
|
+
node.insertionIndex = this.insertionIndexCounter++;
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Matches a given route against the route tree and returns the best matching route's metadata.
|
|
873
|
+
* The best match is determined by the lowest insertion index, meaning the earliest defined route
|
|
874
|
+
* takes precedence.
|
|
875
|
+
*
|
|
876
|
+
* @param route - The route path to match against the route tree.
|
|
877
|
+
* @returns The metadata of the best matching route or `undefined` if no match is found.
|
|
878
|
+
*/
|
|
879
|
+
match(route) {
|
|
880
|
+
const segments = stripTrailingSlash(route).split('/');
|
|
881
|
+
return this.traverseBySegments(segments)?.metadata;
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Converts the route tree into a serialized format representation.
|
|
885
|
+
* This method converts the route tree into an array of metadata objects that describe the structure of the tree.
|
|
886
|
+
* The array represents the routes in a nested manner where each entry includes the route and its associated metadata.
|
|
887
|
+
*
|
|
888
|
+
* @returns An array of `RouteTreeNodeMetadata` objects representing the route tree structure.
|
|
889
|
+
* Each object includes the `route` and associated metadata of a route.
|
|
890
|
+
*/
|
|
891
|
+
toObject() {
|
|
892
|
+
return Array.from(this.traverse());
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Constructs a `RouteTree` from an object representation.
|
|
896
|
+
* This method is used to recreate a `RouteTree` instance from an array of metadata objects.
|
|
897
|
+
* The array should be in the format produced by `toObject`, allowing for the reconstruction of the route tree
|
|
898
|
+
* with the same routes and metadata.
|
|
899
|
+
*
|
|
900
|
+
* @param value - An array of `RouteTreeNodeMetadata` objects that represent the serialized format of the route tree.
|
|
901
|
+
* Each object should include a `route` and its associated metadata.
|
|
902
|
+
* @returns A new `RouteTree` instance constructed from the provided metadata objects.
|
|
903
|
+
*/
|
|
904
|
+
static fromObject(value) {
|
|
905
|
+
const tree = new RouteTree();
|
|
906
|
+
for (const { route, ...metadata } of value) {
|
|
907
|
+
tree.insert(route, metadata);
|
|
908
|
+
}
|
|
909
|
+
return tree;
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* A generator function that recursively traverses the route tree and yields the metadata of each node.
|
|
913
|
+
* This allows for easy and efficient iteration over all nodes in the tree.
|
|
914
|
+
*
|
|
915
|
+
* @param node - The current node to start the traversal from. Defaults to the root node of the tree.
|
|
916
|
+
*/
|
|
917
|
+
*traverse(node = this.root) {
|
|
918
|
+
if (node.metadata) {
|
|
919
|
+
yield node.metadata;
|
|
920
|
+
}
|
|
921
|
+
for (const childNode of node.children.values()) {
|
|
922
|
+
yield* this.traverse(childNode);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Recursively traverses the route tree from a given node, attempting to match the remaining route segments.
|
|
927
|
+
* If the node is a leaf node (no more segments to match) and contains metadata, the node is yielded.
|
|
928
|
+
*
|
|
929
|
+
* This function prioritizes exact segment matches first, followed by wildcard matches (`*`),
|
|
930
|
+
* and finally deep wildcard matches (`**`) that consume all segments.
|
|
931
|
+
*
|
|
932
|
+
* @param remainingSegments - The remaining segments of the route path to match.
|
|
933
|
+
* @param node - The current node in the route tree to start traversal from.
|
|
934
|
+
*
|
|
935
|
+
* @returns The node that best matches the remaining segments or `undefined` if no match is found.
|
|
936
|
+
*/
|
|
937
|
+
traverseBySegments(remainingSegments, node = this.root) {
|
|
938
|
+
const { metadata, children } = node;
|
|
939
|
+
// If there are no remaining segments and the node has metadata, return this node
|
|
940
|
+
if (!remainingSegments?.length) {
|
|
941
|
+
if (metadata) {
|
|
942
|
+
return node;
|
|
943
|
+
}
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
// If the node has no children, end the traversal
|
|
947
|
+
if (!children.size) {
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
const [segment, ...restSegments] = remainingSegments;
|
|
951
|
+
let currentBestMatchNode;
|
|
952
|
+
// 1. Exact segment match
|
|
953
|
+
const exactMatchNode = node.children.get(segment);
|
|
954
|
+
currentBestMatchNode = this.getHigherPriorityNode(currentBestMatchNode, this.traverseBySegments(restSegments, exactMatchNode));
|
|
955
|
+
// 2. Wildcard segment match (`*`)
|
|
956
|
+
const wildcardNode = node.children.get('*');
|
|
957
|
+
currentBestMatchNode = this.getHigherPriorityNode(currentBestMatchNode, this.traverseBySegments(restSegments, wildcardNode));
|
|
958
|
+
// 3. Deep wildcard segment match (`**`)
|
|
959
|
+
const deepWildcardNode = node.children.get('**');
|
|
960
|
+
currentBestMatchNode = this.getHigherPriorityNode(currentBestMatchNode, deepWildcardNode);
|
|
961
|
+
return currentBestMatchNode;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Compares two nodes and returns the node with higher priority based on insertion index.
|
|
965
|
+
* A node with a lower insertion index is prioritized as it was defined earlier.
|
|
966
|
+
*
|
|
967
|
+
* @param currentBestMatchNode - The current best match node.
|
|
968
|
+
* @param candidateNode - The node being evaluated for higher priority based on insertion index.
|
|
969
|
+
* @returns The node with higher priority (i.e., lower insertion index). If one of the nodes is `undefined`, the other node is returned.
|
|
970
|
+
*/
|
|
971
|
+
getHigherPriorityNode(currentBestMatchNode, candidateNode) {
|
|
972
|
+
if (!candidateNode) {
|
|
973
|
+
return currentBestMatchNode;
|
|
974
|
+
}
|
|
975
|
+
if (!currentBestMatchNode) {
|
|
976
|
+
return candidateNode;
|
|
977
|
+
}
|
|
978
|
+
return candidateNode.insertionIndex < currentBestMatchNode.insertionIndex
|
|
979
|
+
? candidateNode
|
|
980
|
+
: currentBestMatchNode;
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Creates an empty route tree node with the specified segment.
|
|
984
|
+
* This helper function is used during the tree construction.
|
|
985
|
+
*
|
|
986
|
+
* @param segment - The route segment that this node represents.
|
|
987
|
+
* @returns A new, empty route tree node.
|
|
988
|
+
*/
|
|
989
|
+
createEmptyRouteTreeNode(segment) {
|
|
990
|
+
return {
|
|
991
|
+
segment,
|
|
992
|
+
insertionIndex: -1,
|
|
993
|
+
children: new Map(),
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Manages the application's server routing logic by building and maintaining a route tree.
|
|
1000
|
+
*
|
|
1001
|
+
* This class is responsible for constructing the route tree from the Angular application
|
|
1002
|
+
* configuration and using it to match incoming requests to the appropriate routes.
|
|
1003
|
+
*/
|
|
1004
|
+
class ServerRouter {
|
|
1005
|
+
routeTree;
|
|
1006
|
+
/**
|
|
1007
|
+
* Creates an instance of the `ServerRouter`.
|
|
1008
|
+
*
|
|
1009
|
+
* @param routeTree - An instance of `RouteTree` that holds the routing information.
|
|
1010
|
+
* The `RouteTree` is used to match request URLs to the appropriate route metadata.
|
|
1011
|
+
*/
|
|
1012
|
+
constructor(routeTree) {
|
|
1013
|
+
this.routeTree = routeTree;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Static property to track the ongoing build promise.
|
|
1017
|
+
*/
|
|
1018
|
+
static #extractionPromise;
|
|
1019
|
+
/**
|
|
1020
|
+
* Creates or retrieves a `ServerRouter` instance based on the provided manifest and URL.
|
|
1021
|
+
*
|
|
1022
|
+
* If the manifest contains pre-built routes, a new `ServerRouter` is immediately created.
|
|
1023
|
+
* Otherwise, it builds the router by extracting routes from the Angular configuration
|
|
1024
|
+
* asynchronously. This method ensures that concurrent builds are prevented by re-using
|
|
1025
|
+
* the same promise.
|
|
1026
|
+
*
|
|
1027
|
+
* @param manifest - An instance of `AngularAppManifest` that contains the route information.
|
|
1028
|
+
* @param url - The URL for server-side rendering. The URL is needed to configure `ServerPlatformLocation`.
|
|
1029
|
+
* This is necessary to ensure that API requests for relative paths succeed, which is crucial for correct route extraction.
|
|
1030
|
+
* [Reference](https://github.com/angular/angular/blob/d608b857c689d17a7ffa33bbb510301014d24a17/packages/platform-server/src/location.ts#L51)
|
|
1031
|
+
* @returns A promise resolving to a `ServerRouter` instance.
|
|
1032
|
+
*/
|
|
1033
|
+
static from(manifest, url) {
|
|
1034
|
+
if (manifest.routes) {
|
|
1035
|
+
const routeTree = RouteTree.fromObject(manifest.routes);
|
|
1036
|
+
return Promise.resolve(new ServerRouter(routeTree));
|
|
1037
|
+
}
|
|
1038
|
+
// Create and store a new promise for the build process.
|
|
1039
|
+
// This prevents concurrent builds by re-using the same promise.
|
|
1040
|
+
ServerRouter.#extractionPromise ??= (async () => {
|
|
1041
|
+
try {
|
|
1042
|
+
const routeTree = new RouteTree();
|
|
1043
|
+
const document = await new ServerAssets(manifest).getIndexServerHtml();
|
|
1044
|
+
const { baseHref, routes } = await getRoutesFromAngularRouterConfig(manifest.bootstrap(), document, url);
|
|
1045
|
+
for (let { route, redirectTo } of routes) {
|
|
1046
|
+
route = joinUrlParts(baseHref, route);
|
|
1047
|
+
redirectTo = redirectTo === undefined ? undefined : joinUrlParts(baseHref, redirectTo);
|
|
1048
|
+
routeTree.insert(route, { redirectTo });
|
|
1049
|
+
}
|
|
1050
|
+
return new ServerRouter(routeTree);
|
|
1051
|
+
}
|
|
1052
|
+
finally {
|
|
1053
|
+
ServerRouter.#extractionPromise = undefined;
|
|
1054
|
+
}
|
|
1055
|
+
})();
|
|
1056
|
+
return ServerRouter.#extractionPromise;
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* Matches a request URL against the route tree to retrieve route metadata.
|
|
1060
|
+
*
|
|
1061
|
+
* This method strips 'index.html' from the URL if it is present and then attempts
|
|
1062
|
+
* to find a match in the route tree. If a match is found, it returns the associated
|
|
1063
|
+
* route metadata; otherwise, it returns `undefined`.
|
|
1064
|
+
*
|
|
1065
|
+
* @param url - The URL to be matched against the route tree.
|
|
1066
|
+
* @returns The metadata for the matched route or `undefined` if no match is found.
|
|
1067
|
+
*/
|
|
1068
|
+
match(url) {
|
|
1069
|
+
// Strip 'index.html' from URL if present.
|
|
1070
|
+
// A request to `http://www.example.com/page/index.html` will render the Angular route corresponding to `http://www.example.com/page`.
|
|
1071
|
+
const { pathname } = stripIndexHtmlFromURL(url);
|
|
1072
|
+
return this.routeTree.match(decodeURIComponent(pathname));
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
/**
|
|
1077
|
+
* Injection token for the current request.
|
|
1078
|
+
*/
|
|
1079
|
+
const REQUEST = new InjectionToken('REQUEST');
|
|
1080
|
+
/**
|
|
1081
|
+
* Injection token for the response initialization options.
|
|
1082
|
+
*/
|
|
1083
|
+
const RESPONSE_INIT = new InjectionToken('RESPONSE_INIT');
|
|
1084
|
+
/**
|
|
1085
|
+
* Injection token for additional request context.
|
|
1086
|
+
*/
|
|
1087
|
+
const REQUEST_CONTEXT = new InjectionToken('REQUEST_CONTEXT');
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* Enum representing the different contexts in which server rendering can occur.
|
|
1091
|
+
*/
|
|
1092
|
+
var ServerRenderContext;
|
|
1093
|
+
(function (ServerRenderContext) {
|
|
1094
|
+
ServerRenderContext["SSR"] = "ssr";
|
|
1095
|
+
ServerRenderContext["SSG"] = "ssg";
|
|
1096
|
+
ServerRenderContext["AppShell"] = "app-shell";
|
|
1097
|
+
})(ServerRenderContext || (ServerRenderContext = {}));
|
|
1098
|
+
/**
|
|
1099
|
+
* Represents a locale-specific Angular server application managed by the server application engine.
|
|
1100
|
+
*
|
|
1101
|
+
* The `AngularServerApp` class handles server-side rendering and asset management for a specific locale.
|
|
1102
|
+
*/
|
|
1103
|
+
class AngularServerApp {
|
|
1104
|
+
/**
|
|
1105
|
+
* Hooks for extending or modifying the behavior of the server application.
|
|
1106
|
+
* This instance can be used to attach custom functionality to various events in the server application lifecycle.
|
|
1107
|
+
*/
|
|
1108
|
+
hooks = new Hooks();
|
|
1109
|
+
/**
|
|
1110
|
+
* The manifest associated with this server application.
|
|
1111
|
+
*/
|
|
1112
|
+
manifest = getAngularAppManifest();
|
|
1113
|
+
/**
|
|
1114
|
+
* An instance of ServerAsset that handles server-side asset.
|
|
1115
|
+
*/
|
|
1116
|
+
assets = new ServerAssets(this.manifest);
|
|
1117
|
+
/**
|
|
1118
|
+
* The router instance used for route matching and handling.
|
|
1119
|
+
*/
|
|
1120
|
+
router;
|
|
1121
|
+
/**
|
|
1122
|
+
* Renders a response for the given HTTP request using the server application.
|
|
1123
|
+
*
|
|
1124
|
+
* This method processes the request and returns a response based on the specified rendering context.
|
|
1125
|
+
*
|
|
1126
|
+
* @param request - The incoming HTTP request to be rendered.
|
|
1127
|
+
* @param requestContext - Optional additional context for rendering, such as request metadata.
|
|
1128
|
+
* @param serverContext - The rendering context.
|
|
1129
|
+
*
|
|
1130
|
+
* @returns A promise that resolves to the HTTP response object resulting from the rendering, or null if no match is found.
|
|
1131
|
+
*/
|
|
1132
|
+
render(request, requestContext, serverContext = ServerRenderContext.SSR) {
|
|
1133
|
+
return Promise.race([
|
|
1134
|
+
this.createAbortPromise(request),
|
|
1135
|
+
this.handleRendering(request, requestContext, serverContext),
|
|
1136
|
+
]);
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Creates a promise that rejects when the request is aborted.
|
|
1140
|
+
*
|
|
1141
|
+
* @param request - The HTTP request to monitor for abortion.
|
|
1142
|
+
* @returns A promise that never resolves but rejects with an `AbortError` if the request is aborted.
|
|
1143
|
+
*/
|
|
1144
|
+
createAbortPromise(request) {
|
|
1145
|
+
return new Promise((_, reject) => {
|
|
1146
|
+
request.signal.addEventListener('abort', () => {
|
|
1147
|
+
const abortError = new Error(`Request for: ${request.url} was aborted.\n${request.signal.reason}`);
|
|
1148
|
+
abortError.name = 'AbortError';
|
|
1149
|
+
reject(abortError);
|
|
1150
|
+
}, { once: true });
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* Handles the server-side rendering process for the given HTTP request.
|
|
1155
|
+
* This method matches the request URL to a route and performs rendering if a matching route is found.
|
|
1156
|
+
*
|
|
1157
|
+
* @param request - The incoming HTTP request to be processed.
|
|
1158
|
+
* @param requestContext - Optional additional context for rendering, such as request metadata.
|
|
1159
|
+
* @param serverContext - The rendering context. Defaults to server-side rendering (SSR).
|
|
1160
|
+
*
|
|
1161
|
+
* @returns A promise that resolves to the rendered response, or null if no matching route is found.
|
|
1162
|
+
*/
|
|
1163
|
+
async handleRendering(request, requestContext, serverContext = ServerRenderContext.SSR) {
|
|
1164
|
+
const url = new URL(request.url);
|
|
1165
|
+
this.router ??= await ServerRouter.from(this.manifest, url);
|
|
1166
|
+
const matchedRoute = this.router.match(url);
|
|
1167
|
+
if (!matchedRoute) {
|
|
1168
|
+
// Not a known Angular route.
|
|
1169
|
+
return null;
|
|
1170
|
+
}
|
|
1171
|
+
const { redirectTo } = matchedRoute;
|
|
1172
|
+
if (redirectTo !== undefined) {
|
|
1173
|
+
// 302 Found is used by default for redirections
|
|
1174
|
+
// See: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect_static#status
|
|
1175
|
+
return Response.redirect(new URL(redirectTo, url), 302);
|
|
1176
|
+
}
|
|
1177
|
+
const platformProviders = [
|
|
1178
|
+
{
|
|
1179
|
+
provide: _SERVER_CONTEXT,
|
|
1180
|
+
useValue: serverContext,
|
|
1181
|
+
},
|
|
1182
|
+
{
|
|
1183
|
+
// An Angular Console Provider that does not print a set of predefined logs.
|
|
1184
|
+
provide: _Console,
|
|
1185
|
+
// Using `useClass` would necessitate decorating `Console` with `@Injectable`,
|
|
1186
|
+
// which would require switching from `ts_library` to `ng_module`. This change
|
|
1187
|
+
// would also necessitate various patches of `@angular/bazel` to support ESM.
|
|
1188
|
+
useFactory: () => new Console(),
|
|
1189
|
+
},
|
|
1190
|
+
];
|
|
1191
|
+
const isSsrMode = serverContext === ServerRenderContext.SSR;
|
|
1192
|
+
const responseInit = {};
|
|
1193
|
+
if (isSsrMode) {
|
|
1194
|
+
platformProviders.push({
|
|
1195
|
+
provide: REQUEST,
|
|
1196
|
+
useValue: request,
|
|
1197
|
+
}, {
|
|
1198
|
+
provide: REQUEST_CONTEXT,
|
|
1199
|
+
useValue: requestContext,
|
|
1200
|
+
}, {
|
|
1201
|
+
provide: RESPONSE_INIT,
|
|
1202
|
+
useValue: responseInit,
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
|
1206
|
+
// Need to clean up GENERATED_COMP_IDS map in `@angular/core`.
|
|
1207
|
+
// Otherwise an incorrect component ID generation collision detected warning will be displayed in development.
|
|
1208
|
+
// See: https://github.com/angular/angular-cli/issues/25924
|
|
1209
|
+
_resetCompiledComponents();
|
|
1210
|
+
}
|
|
1211
|
+
const { manifest, hooks, assets } = this;
|
|
1212
|
+
let html = await assets.getIndexServerHtml();
|
|
1213
|
+
// Skip extra microtask if there are no pre hooks.
|
|
1214
|
+
if (hooks.has('html:transform:pre')) {
|
|
1215
|
+
html = await hooks.run('html:transform:pre', { html });
|
|
1216
|
+
}
|
|
1217
|
+
return new Response(await renderAngular(html, manifest.bootstrap(), new URL(request.url), platformProviders), responseInit);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
let angularServerApp;
|
|
1221
|
+
/**
|
|
1222
|
+
* Retrieves or creates an instance of `AngularServerApp`.
|
|
1223
|
+
* - If an instance of `AngularServerApp` already exists, it will return the existing one.
|
|
1224
|
+
* - If no instance exists, it will create a new one with the provided options.
|
|
1225
|
+
* @returns The existing or newly created instance of `AngularServerApp`.
|
|
1226
|
+
*/
|
|
1227
|
+
function getOrCreateAngularServerApp() {
|
|
1228
|
+
return (angularServerApp ??= new AngularServerApp());
|
|
1229
|
+
}
|
|
1230
|
+
/**
|
|
1231
|
+
* Destroys the existing `AngularServerApp` instance, releasing associated resources and resetting the
|
|
1232
|
+
* reference to `undefined`.
|
|
1233
|
+
*
|
|
1234
|
+
* This function is primarily used to enable the recreation of the `AngularServerApp` instance,
|
|
1235
|
+
* typically when server configuration or application state needs to be refreshed.
|
|
1236
|
+
*/
|
|
1237
|
+
function destroyAngularServerApp() {
|
|
1238
|
+
angularServerApp = undefined;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
export { CommonEngine, ServerRenderContext as ɵServerRenderContext, destroyAngularServerApp as ɵdestroyAngularServerApp, getOrCreateAngularServerApp as ɵgetOrCreateAngularServerApp, getRoutesFromAngularRouterConfig as ɵgetRoutesFromAngularRouterConfig, setAngularAppEngineManifest as ɵsetAngularAppEngineManifest, setAngularAppManifest as ɵsetAngularAppManifest };
|
|
645
1242
|
//# sourceMappingURL=ssr.mjs.map
|