6

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

Dionisis K
  • 614
  • 5
  • 17

1 Answers1

8

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

Black Mamba
  • 13,632
  • 6
  • 82
  • 105
Dionisis K
  • 614
  • 5
  • 17
  • on refresh non ssr pages are not loading – Vipul Feb 12 '20 at 13:50
  • showing this error `Cannot GET /user/profile/corporate` my code is like ```server.get('/user/profile/:type', (req, res) => { res.sendFile(join(distFolder, 'browser', 'index.html')); });``` – Vipul Feb 13 '20 at 05:23