Angular generic rendering for some routes only
I've been playing around with angular generic but couldn't find an option to use server side rendering for only some pages like the home page and display all other routes in the standard angular way. I don't want to use server side rendering for private pages where SEO is not needed. I can set up routes in an expression like this
// send all requests to Angular Universal
// if you want Express to handle certain routes (ex. for an API) make sure you adjust this
app.get('/', ngApp);
app.get('/home', ngApp);
app.get('/about', ngApp);
Ideally I don't want to know about NodeJs at all and set it up in angular config config with a property like serverSide: true
const appRoutes: Routes = [
//public route, I want server rendering for SEO
{ path: 'home', component: HomeComponent, serverSide: true },
//private user profile page, SEO is not needed
{ path: 'user/profile/:id', component: UserProfileComponent },
];
+3
source to share
1 answer
On the server.ts for routes you don't want to be displayed, just do as below
app.get('/api/**', (req, res) => { });
app.get('/app/**', (req, res) => {
console.log('not rendering app pages');
});
app.get('/auth/**', (req, res) => {
console.log('not rendering auth page');
});
// All regular routes use a universal engine
app.get('*', (req, res) => {
res.render('index', { req });
});
Hope it helps.
0
source to share