Angular universal only to specific routes

放肆的年华 提交于 2019-12-11 01:09:14

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/49325054/angular-universal-only-to-specific-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!