I work on a pretty old angular project, we started working on it on 2.0 release, and upgraded it release after release. We're on 4.3 for now
Now, the project is very complex, with a lot of features, and a lot of (old) peer dependencies, not maintained anymore for some of them.
I tried to implement angular universal on this project, to improve SEO, but I come accross errors one after another, and it looks like there's no end at this.
So my questions are :
All answer and hints are welcome !
Implementing Angular universal on old project can be really, really painful :
I have to implement it on a old project too, and, like you, I had a lot of compilation errors. So I decided to split my app between two parts, two projects :
The bad part is that the 2 projects are independent from each others, so you can't use things like localStorage, routerLink etc. But they're connected to the same API, so you can always share data between them
Technically, I did it like this :
app-universal folder, with the app-universal.module.ts<base href=""> of the simplified app to <base href="/content">angular-cli.json: "apps": [
{
"name": "app",
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "app/main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
...
},
{
"name": "universal",
"root": "src",
"outDir": "dist-universal",
"assets": [
"assets",
"favicon.ico"
],
"index": "app-universal/index.html",
"main": "app-universal/main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app-universal",
"...
},
{
"name": "server",
"platform": "server",
"root": "src",
"outDir": "src/app-universal/dist-universal-server",
"assets": [
"assets",
"favicon.ico"
],
"index": "app-universal/index.html",
"main": "app-universal/main.server.ts",
"test": "test.ts",
"tsconfig": "app-universal/tsconfig.server.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app-universal",
...
}
],
package.json :"scripts": {
"prebuild-server": "ng build --prod --aot --app server && ngc -p src/app-universal/tsconfig.server.json",
"build-server": "ts-node src/server.ts",
...
},
and running npm run build-server to compile your simplified app and render it with universal.
Location and ReverseProxy on my server to launch my main app on the / path, and my simplified app on /content or /blog or /whatever path :In the /etc/apache2/site-availables/your-project.conf file (on the <VirtualHost:.80> block, after DocumentRoot for example):
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /content>
ProxyPass http://127.0.0.1:4000
ProxyPassReverse http://1127.0.0.1:4000
</Location>
And then you can use pm2 to keep your Node.js application up in the background: How To Set Up a Node.js Application for Production on Ubuntu 16.04
And that waq done. I had split my project into two sub-project independent from each-other and run them on the same domain, with all the /content/* url SEO friendly
To help you pass trough current universal errors : Angular with universal does not work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With