Angular2SPA is slow to startup webapp and render data page
We have compared the Angular2SPA web app, against the generic MVC WebApp generated by Visual Studio 2015. We found that the start up time for the Angular2SPA web app is more than double the time required to start a regular MVC app.
We have also modified the Angular2SPA web app and added Entity Framework 7 and test against SQL server. Comparing the time against the "Fetch Data" page, it take over 100% longer to load the same data page as, compared to Angular 1.0 using Entity Framework 6.
I have test this on Internet Explore 10 and 11.
Is there any reason why serving Angular 2 pages are so much slower than Angular 1.0 pages with ASP.Net 5? Is there any plans for the team to improve the performance , either by enhancing the Angular2SPA template, or IE updates, or both?
What is the best version of IE required for Angular2SPA to get the best performance out?
Are you running it with enableProdMode() in both the boot-client.ts and boot-server.ts ?
This will help create faster renders at least from the Universal stand-point, but there might be something else .NET Core wise bottlenecking the rest of it, no doubt.
import { enableProdMode } from '@angular/core';
enableProdMode();
@nklt From your phrasing, I'm assuming you're talking exclusively about the startup time (i.e., time to load the first page), and not about the speed of execution beyond that point. The execution time beyond that point is somewhat outside our control as that's primarily just Angular 2 code.
The reason you're seeing much slower startup time with these templates is because of the whole build phase (e.g., typescript compilation) that happens on app startup. Assuming you're using the Angular2Spa template, then:
- In production mode, the prerendering system is doing TypeScript compilation on startup so it can run your components on the server
- In development mode, as well as the prerendering stuff, you've also got Webpack dev middleware which runs TypeScript compilation so it can serve compiled JavaScript without you having to precompile it
These features are excellent from a convenience point of view (especially at development time) but this is at the cost of slower app startup. If you need to make your app start up as fast as possible, then:
- In development mode, don't use webpack dev middleware. Instead, precompile all your TypeScript so that you don't think of it as contributing to the app startup time (even though in reality you're waiting at least as long, because now you have to compile before you refresh your page after each change). For example, remove the
UseWebpackDevMiddlewareline from yourStartup.cs, and run something likewebpack --watchin a command line to keep auto-recompiling your TypeScript each time it changes. - In production mode, either don't use server-side prerendering, or consider precompiling a server-side build of your app so there's no need to invoke the TypeScript compiler as it starts up (see info below).
I hope this explains the situation and gives you some useful options. If you have reason to think the issue is something else, please reopen this issue or post a new one with further details.
Appendix: precompiling a server-side build of your app to enable prerendering without any runtime compilation
- At the root of your project, add a further Webpack config file called
webpack.config.server.js, containing this:
var path = require('path');
var webpack = require('webpack');
var ExternalsPlugin = require('webpack-externals-plugin');
module.exports = {
target: 'node',
devtool: 'inline-source-map',
resolve: {
extensions: [ '', '.js', '.ts' ]
},
module: {
loaders: [
{ test: /\.ts$/, include: /ClientApp/, loader: 'ts-loader?silent=true' },
{ test: /\.html$/, loader: 'raw-loader' }
]
},
entry: {
'boot-server': ['./ClientApp/boot-server.ts']
},
output: {
path: path.join(__dirname, 'ClientApp', 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs'
},
plugins: [
new ExternalsPlugin({ type: 'commonjs', include: /node_modules/ })
]
};
- Run
webpack --config webpack.config.server.js. This will create a server-side build of your TypeScript code, and will output it atClientApp/dist/boot-server.js. - Change
Views/Home/Index.cshtmlso that the prerendering tag helper doesn't load your boot module via Webpack. That is, remove theasp-prerender-webpack-configattribute altogther, so the<app>element looks like this:
<app asp-prerender-module="ClientApp/dist/boot-server">Loading...</app>
- Run your app. It no longer has to compile the TypeScript on the server to do prerendering (although it will still do so if you have webpack dev middleware enabled, so you might want to disable that too).
Now when you deploy, you no longer need to deploy any of your .ts files to the server either - it just needs the boot-server.js (plus your node_modules dir) to run.
Thank you for the detailed comment. I will test the recommended changes and let you know our results. Thanks.
@SteveSandersonMS can you please confirm if the information you mentioned above applies to the latest Angular Spa template available in VS2017 with .Net Core 2.0 as well?
With Debug mode and SSR, I am able to run app in browser, though little slow but it works. But when I Publish the project with SSR and deploy it to server or IIS on my local machine it crashes (server 500 error) probably the request times out.
I tried to redeploy after removing SSR, it started working.
When I publish with SSR enabled, the size of files in ClientApp\dist is 27.6 Mb and the size of files in wwwroot\dist is 7.67 Mb.
When I compiled the same project with Angular CLI without SSR, the output bundles were ~4 Mb in size.
I have Lazy loading enabled in my project.
Right now I have also enclosed http calls in all components in if (isPlatformBrowser(this.platformId)){} so data is fetched from api on client side only but still no luck.
I know this issue is closed but wanted to add a comment to @SteveSandersonMS
We run into the same issue of debug load times being extremely slow - strictly before the angular app takes over in the browser. But for us, this didn't start appearing until the project reached a certain size.
We installed a file watcher on windows (Directory Monitor for example) to see what was getting done on the filesystem when we compiled.
We found out two things with the filesystem watcher
- the entire typescript code base gets recompiled to javascript - even if there's been no change in the typescript files. With our 300 typescript files (models, services, components, controls, etc), it takes time even on decent workstations (say 1 to 4 minutes). I am sure this can get tweaked - not sure how yet.
- we needed to disable js.map generation ("sourceMap": false in compilerOptions ) in tsconfig.json - otherwise, we doubled the files generated. So removing that option helped greatly. Literally cut our build time in half, plus it's very rare that we need to debug the generated javascript in chrome, so turning that off is fine in our case.
Then, the next thing we did which had a huge impact, in the project Debug settings, was to change the launch option from IISExpress (default) to Project. And the net result is closer to production and it was much, much faster (no symbol loading). We also preferred that mode as a console window pops up with error messages and etc.
We've been using your template (thank you so much) as a jumpstart in February of 2017 and it has been of great value so far - thank you for this great work.
We hope the above tips will help folks with large Angular apps and speed up their debug times.
Cheers