Skip to content
Merged
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
14 changes: 12 additions & 2 deletions packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { APP_BASE_HREF, PlatformLocation } from '@angular/common';
import {
ApplicationRef,
Compiler,
EnvironmentInjector,
Injector,
createEnvironmentInjector,
runInInjectionContext,
ɵConsole,
ɵENABLE_ROOT_COMPONENT_BOOTSTRAP,
Expand Down Expand Up @@ -195,14 +197,22 @@ async function* handleRoute(options: {
appendPreloadToMetadata(ɵentryName, entryPointToBrowserMapping, metadata);
}

const routeInjector = route.providers
? createEnvironmentInjector(
route.providers,
parentInjector.get(EnvironmentInjector),
`Route: ${route.path}`,
)
: parentInjector;

const loadedChildRoutes = await loadChildrenHelper(
route,
compiler,
parentInjector,
routeInjector,
).toPromise();

if (loadedChildRoutes) {
const { routes: childRoutes, injector = parentInjector } = loadedChildRoutes;
const { routes: childRoutes, injector = routeInjector } = loadedChildRoutes;
yield* traverseRoutesConfig({
...options,
routes: childRoutes,
Expand Down
41 changes: 39 additions & 2 deletions packages/angular/ssr/test/routes/ng-routes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
import '@angular/compiler';
/* eslint-enable import/no-unassigned-import */

import { Component } from '@angular/core';
import { Routes, provideRouter, withEnabledBlockingInitialNavigation } from '@angular/router';
import { Component, InjectionToken, Injector, inject } from '@angular/core';
import {
Route,
Routes,
provideRouter,
withEnabledBlockingInitialNavigation,
} from '@angular/router';
import { extractRoutesAndCreateRouteTree } from '../../src/routes/ng-routes';
import { PrerenderFallback, RenderMode } from '../../src/routes/route-config';
import { setAngularAppTestingManifest } from '../testing-utils';
Expand Down Expand Up @@ -717,4 +722,36 @@ describe('extractRoutesAndCreateRouteTree', () => {
{ route: '/**', renderMode: RenderMode.Server },
]);
});

it(`should create and run route level injector when 'loadChildren' is used`, async () => {
const ChildRoutes = new InjectionToken<Route[]>('Child Routes');
setAngularAppTestingManifest(
[
{
path: '',
component: DummyComponent,
providers: [
{
provide: ChildRoutes,
useValue: [
{
path: 'home',
component: DummyComponent,
},
],
},
],
loadChildren: () => inject(ChildRoutes),
},
],
[{ path: '**', renderMode: RenderMode.Server }],
);

const { routeTree, errors } = await extractRoutesAndCreateRouteTree({ url });
expect(errors).toHaveSize(0);
expect(routeTree.toObject()).toEqual([
{ route: '/', renderMode: RenderMode.Server },
{ route: '/home', renderMode: RenderMode.Server },
]);
});
});
Loading