Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular universal only to specific routes

I'm trying to implement angular universal. But I want to limit SSR only in 2 specific routes for the time being. The reasons are .
1) Universal is something still developing and causes some unexpected behaviors with @angular/flex-layout and nested lazy loading

2) I don t need SEO on all pages

So i've tried something like this

app.get('/campaign/*/*', (req, res) => {
   res.render(join(DIST_FOLDER, 'browser', 'index.html'), { req });
});

app.get('/', (req, res) => {
   res.render(join(DIST_FOLDER, 'browser', 'index.html'), { req });
});

app.get('*', (req, res) => {

});

But when i try to navigate in page /campaign/new-channel pages reloads forever.
The expected behavior I expect is the page to normally without any content be rendered .

I suppose I have to do something different since node express should pass the handling of routing to angular .
Any idea how to implement this ?
The rest code of server.ts is copied from here https://github.com/angular/universal-starter/blob/master/server.ts

P.S There is this post on stackoverflow Angular universal rendering for some routes only but no solution is provided .So I made this one since I don't know if i m allowed to open new thread in answers

like image 607
Dionisis K Avatar asked Mar 16 '18 16:03

Dionisis K


People also ask

Does angular have SSR?

To enable SSR in Angular, Angular should be able to rendered in the server. To make it happen, Angular provides a special technology called Angular Universal.

Should I use angular universal?

A primary benefit for using Angular Universal is that it improves web crawler support for enhanced Search Engine Optimization (SEO). With traditional client-side rendered SPAs, anything that is not in that shell of an . html is all rendered by the JavaScript.

What is angular universal SSR?

In a nutshell, Angular Universal is a Pre-Rendering solution for Angular. To understand what this means, let's remember that in a normal single page application, we usually bring the data to the client and then build the HTML that represents that data last second on the client side.


1 Answers

Finally response.sendFile is what I was searching for

app.get('/campaign/new-channel', function (req, res) {
   res.sendFile(join(DIST_FOLDER, 'browser', 'index.html'));
});

What you want is just to return the index.html without any rendered content

like image 192
Dionisis K Avatar answered Oct 23 '22 18:10

Dionisis K