Skip to content

Angular SSR - only specific routes to be rendered as SSR is iqnored in v18.x; it was working fine in v16.x #28545

@sonnyk22

Description

@sonnyk22

Which @angular/* package(s) are relevant/related to the feature request?

platform-server

Description

In Server.ts, bypassing specific Urls to be rendered only on the client side worked great in Angular 16.x, but the same functionality is ignored in Angular 18.x.

Not sure if:

  • the logic has been changed in v18.x
  • or if it is something that I am doing wrong in the code below
  • or the latest version of Angular is now requiring that everything has be severed as SSR and to use the PLATFORM_ID to determine what to do?

Here is the code.

    // If these routes, then do not serve as SSR
const iqnoreSrrUrls = [
	'/home',
	'/details/requests',
	'/details/notifs',
	'/details/messages'
];
	
// All regular routes use the Angular engine
server.get('**', (req, res, next) => {
	const { protocol, originalUrl, baseUrl, headers } = req;

	// This does not work any longer
	const found = iqnoreSrrUrls.some((url) => req.url.toLowerCase().lastIndexOf(url));
	if (found) {
		res.sendFile(join(browserDistFolder, 'index.html'));
		return;
	}

	commonEngine.render({
		bootstrap: AppServerModule,
		documentFilePath: indexHtml,
		url: `${protocol}://${headers.host}${originalUrl}`,
		publicPath: browserDistFolder,
		providers: [
			{ provide: APP_BASE_HREF, useValue: baseUrl }
		],
	})
	.then((html) => res.send(html))
	.catch((err) => next(err));
});

Proposed solution

Its not very intuitive on how to do this in Server.ts. Would be great to leave a small "commented" code sample in this file.

Alternatives considered

Even this did not work for me:

// Serve static files from /browser
server.get('/home/**', express.static(browserDistFolder, {
	maxAge: '1y',
	index: 'index.html',
}));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions