Skip to content
Open
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
29 changes: 17 additions & 12 deletions packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type EntryPointToBrowserMapping = AngularAppManifest['entryPointToBrowserMapping
async function* handleRoute(options: {
metadata: ServerConfigRouteTreeNodeMetadata;
currentRoutePath: string;
parentRoute: string;
route: Route;
compiler: Compiler;
parentInjector: Injector;
Expand All @@ -158,6 +159,7 @@ async function* handleRoute(options: {
const {
metadata,
currentRoutePath,
parentRoute,
route,
compiler,
parentInjector,
Expand All @@ -176,6 +178,7 @@ async function* handleRoute(options: {
yield* handleSSGRoute(
serverConfigRouteTree,
typeof redirectTo === 'string' ? redirectTo : undefined,
parentRoute,
metadata,
parentInjector,
invokeGetPrerenderParams,
Expand All @@ -191,7 +194,7 @@ async function* handleRoute(options: {
} else if (typeof redirectTo === 'string') {
yield {
...metadata,
redirectTo: resolveRedirectTo(metadata.route, redirectTo),
redirectTo: resolveRedirectTo(parentRoute, redirectTo),
};
} else {
yield metadata;
Expand Down Expand Up @@ -392,6 +395,7 @@ function appendPreloadToMetadata(
*
* @param serverConfigRouteTree - The tree representing the server's routing setup.
* @param redirectTo - Optional path to redirect to, if specified.
* @param parentRoute - The path of the parent route.
* @param metadata - The metadata associated with the route tree node.
* @param parentInjector - The dependency injection container for the parent route.
* @param invokeGetPrerenderParams - A flag indicating whether to invoke the `getPrerenderParams` function.
Expand All @@ -401,6 +405,7 @@ function appendPreloadToMetadata(
async function* handleSSGRoute(
serverConfigRouteTree: RouteTree<ServerConfigRouteTreeAdditionalMetadata> | undefined,
redirectTo: string | undefined,
parentRoute: string,
metadata: ServerConfigRouteTreeNodeMetadata,
parentInjector: Injector,
invokeGetPrerenderParams: boolean,
Expand All @@ -420,7 +425,7 @@ async function* handleSSGRoute(
}

if (redirectTo !== undefined) {
meta.redirectTo = resolveRedirectTo(currentRoutePath, redirectTo);
meta.redirectTo = resolveRedirectTo(parentRoute, redirectTo);
}

const isCatchAllRoute = CATCH_ALL_REGEXP.test(currentRoutePath);
Expand Down Expand Up @@ -478,7 +483,10 @@ async function* handleSSGRoute(
redirectTo:
redirectTo === undefined
? undefined
: resolveRedirectTo(routeWithResolvedParams, redirectTo),
: resolveRedirectTo(
parentRoute.replace(URL_PARAMETER_GLOBAL_REGEXP, replacer),
Comment thread
bschaeublin marked this conversation as resolved.
redirectTo,
),
};
}
} catch (error) {
Expand Down Expand Up @@ -533,24 +541,21 @@ function handlePrerenderParamsReplacement(
* Resolves the `redirectTo` property for a given route.
*
* This function processes the `redirectTo` property to ensure that it correctly
* resolves relative to the current route path. If `redirectTo` is an absolute path,
* it is returned as is. If it is a relative path, it is resolved based on the current route path.
* resolves relative to the parent route path. If `redirectTo` is an absolute path,
* it is returned as is. If it is a relative path, it is resolved based on the parent route path.
*
* @param routePath - The current route path.
* @param parentRoute - The path of the parent route.
* @param redirectTo - The target path for redirection.
* @returns The resolved redirect path as a string.
*/
function resolveRedirectTo(routePath: string, redirectTo: string): string {
function resolveRedirectTo(parentRoute: string, redirectTo: string): string {
if (redirectTo[0] === '/') {
// If the redirectTo path is absolute, return it as is.
return redirectTo;
}

// Resolve relative redirectTo based on the current route path.
const segments = routePath.replace(URL_PARAMETER_GLOBAL_REGEXP, '*').split('/');
segments.pop(); // Remove the last segment to make it relative.

return joinUrlParts(...segments, redirectTo);
// Match Angular router behavior: replace all segments matched by the route.
return joinUrlParts(parentRoute.replace(URL_PARAMETER_GLOBAL_REGEXP, '*'), redirectTo);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions packages/angular/ssr/test/app_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('AngularServerApp', () => {
{ path: 'redirect/relative', redirectTo: 'home' },
{ path: 'redirect/:param/relative', redirectTo: 'home' },
{ path: 'redirect/absolute', redirectTo: '/home' },
{ path: 'redirect-nested/:param', children: [{ path: 'old', redirectTo: 'new' }] },
{
path: 'redirect-to-function',
redirectTo: () => 'home',
Expand Down Expand Up @@ -206,13 +207,19 @@ describe('AngularServerApp', () => {

it('should correctly handle relative nested redirects', async () => {
const response = await app.handle(new Request('http://localhost/redirect/relative'));
expect(response?.headers.get('location')).toBe('/redirect/home');
expect(response?.headers.get('location')).toBe('/home');
expect(response?.status).toBe(302);
});

it('should correctly handle relative nested redirects with parameter', async () => {
const response = await app.handle(new Request('http://localhost/redirect/param/relative'));
expect(response?.headers.get('location')).toBe('/redirect/param/home');
expect(response?.headers.get('location')).toBe('/home');
expect(response?.status).toBe(302);
});

it('should correctly handle relative redirects of parameterized parent routes', async () => {
const response = await app.handle(new Request('http://localhost/redirect-nested/x/old'));
expect(response?.headers.get('location')).toBe('/redirect-nested/x/new');
expect(response?.status).toBe(302);
});

Expand Down
93 changes: 93 additions & 0 deletions packages/angular/ssr/test/routes/ng-routes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,76 @@ describe('extractRoutesAndCreateRouteTree', () => {
{ route: '/*/thing', renderMode: RenderMode.Server },
]);
});

it('should resolve multi-segment relative redirects of prerendered routes', async () => {
setAngularAppTestingManifest(
[
{
path: ':param',
children: [
{ path: 'old/page', redirectTo: 'new/page' },
{ path: 'new/page', component: DummyComponent },
],
},
],
[
{
path: ':param/old/page',
renderMode: RenderMode.Prerender,
async getPrerenderParams() {
return [{ param: 'some' }];
},
},
{ path: '**', renderMode: RenderMode.Server },
],
);

const { routeTree, errors } = await extractRoutesAndCreateRouteTree({
url,
invokeGetPrerenderParams: true,
includePrerenderFallbackRoutes: true,
});

expect(errors).toHaveSize(0);
expect(routeTree.toObject()).toEqual([
{ route: '/*', renderMode: RenderMode.Server },
{ route: '/*/old/page', renderMode: RenderMode.Server, redirectTo: '/*/new/page' },
{ route: '/*/new/page', renderMode: RenderMode.Server },
{ route: '/some/old/page', renderMode: RenderMode.Prerender, redirectTo: '/some/new/page' },
]);
});

it('should resolve relative redirects of prerendered catch-all routes', async () => {
setAngularAppTestingManifest(
[
{ path: 'docs/**', redirectTo: 'new' },
{ path: 'new', component: DummyComponent },
],
[
{
path: 'docs/**',
renderMode: RenderMode.Prerender,
async getPrerenderParams() {
return [{ '**': 'a/b' }];
},
},
{ path: '**', renderMode: RenderMode.Server },
],
);

const { routeTree, errors } = await extractRoutesAndCreateRouteTree({
url,
invokeGetPrerenderParams: true,
includePrerenderFallbackRoutes: true,
});

expect(errors).toHaveSize(0);
expect(routeTree.toObject()).toEqual([
{ route: '/docs/a/b', renderMode: RenderMode.Prerender, redirectTo: '/new' },
{ route: '/docs/**', renderMode: RenderMode.Server, redirectTo: '/new' },
{ route: '/new', renderMode: RenderMode.Server },
]);
});
});

it('should extract routes with a route level matcher', async () => {
Expand Down Expand Up @@ -531,6 +601,29 @@ describe('extractRoutesAndCreateRouteTree', () => {
]);
});

it('should resolve multi-segment relative redirects against the parent route', async () => {
setAngularAppTestingManifest(
[
{
path: ':param',
children: [
{ path: 'old/page', redirectTo: 'new/page' },
{ path: 'new/page', component: DummyComponent },
],
},
],
[{ path: '**', renderMode: RenderMode.Server }],
);

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

it('should not resolve parameterized routes for SSG when `invokeGetPrerenderParams` is false', async () => {
setAngularAppTestingManifest(
[
Expand Down
Loading