@@ -22,9 +22,12 @@ import {
22
22
ɵINTERNAL_SERVER_PLATFORM_PROVIDERS as INTERNAL_SERVER_PLATFORM_PROVIDERS ,
23
23
} from '@angular/platform-server' ;
24
24
import { Route , Router , ɵloadChildren as loadChildrenHelper } from '@angular/router' ;
25
+ import { ServerAssets } from '../assets' ;
25
26
import { Console } from '../console' ;
27
+ import { AngularAppManifest , getAngularAppManifest } from '../manifest' ;
26
28
import { AngularBootstrap , isNgModule } from '../utils/ng' ;
27
29
import { joinUrlParts } from '../utils/url' ;
30
+ import { RouteTree } from './route-tree' ;
28
31
29
32
/**
30
33
* Result of extracting routes from an Angular application.
@@ -257,3 +260,39 @@ export async function getRoutesFromAngularRouterConfig(
257
260
platformRef . destroy ( ) ;
258
261
}
259
262
}
263
+
264
+ /**
265
+ * Asynchronously extracts routes from the Angular application configuration
266
+ * and creates a `RouteTree` to manage server-side routing.
267
+ *
268
+ * @param url - The URL for server-side rendering. The URL is used to configure `ServerPlatformLocation`. This configuration is crucial
269
+ * for ensuring that API requests for relative paths succeed, which is essential for accurate route extraction.
270
+ * See:
271
+ * - https://github.com/angular/angular/blob/d608b857c689d17a7ffa33bbb510301014d24a17/packages/platform-server/src/location.ts#L51
272
+ * - https://github.com/angular/angular/blob/6882cc7d9eed26d3caeedca027452367ba25f2b9/packages/platform-server/src/http.ts#L44
273
+ * @param manifest - An optional `AngularAppManifest` that contains the application's routing and configuration details.
274
+ * If not provided, the default manifest is retrieved using `getAngularAppManifest()`.
275
+ *
276
+ * @returns A promise that resolves to a populated `RouteTree` containing all extracted routes from the Angular application.
277
+ */
278
+ export async function extractRoutesAndCreateRouteTree (
279
+ url : URL ,
280
+ manifest : AngularAppManifest = getAngularAppManifest ( ) ,
281
+ ) : Promise < RouteTree > {
282
+ const routeTree = new RouteTree ( ) ;
283
+ const document = await new ServerAssets ( manifest ) . getIndexServerHtml ( ) ;
284
+ const { baseHref, routes } = await getRoutesFromAngularRouterConfig (
285
+ manifest . bootstrap ( ) ,
286
+ document ,
287
+ url ,
288
+ ) ;
289
+
290
+ for ( let { route, redirectTo } of routes ) {
291
+ route = joinUrlParts ( baseHref , route ) ;
292
+ redirectTo = redirectTo === undefined ? undefined : joinUrlParts ( baseHref , redirectTo ) ;
293
+
294
+ routeTree . insert ( route , { redirectTo } ) ;
295
+ }
296
+
297
+ return routeTree ;
298
+ }
0 commit comments