You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
The Concise Angular 6 Universal Guide
written by Jeff DelaneyA concise and simple quick start for Angular v6 Universal SSR.
This snippet is a companion to the Angular Firebase Universal Tutorial.
Step 1 - Generate an Angular 6 App
ng new ssrApp --routing |
If using Firebase, make sure to follow the official step guide for AngularFire2. Make sure you’re on the latest version.
Step 2 - Generate Universal Code and Update NgModules
We can now generate much of the Universal boilerplate with the Angular CLI. In 2018, a render engine was introduced for express to simplfy the server code.
ng generate universal --client-project YOUR_PROJECT_NAME |
If transferring state between server/browser, update your NgModules accordingly. The browser app should have the following imports in src/app/app.module
:
import { BrowserTransferStateModule } from '@angular/platform-browser'; |
Update the src/app/app.server.module
with the following code:
import { |
Step 3 - Create an ExpressJS Server
The ExpressJS app will be deployed to a NodeJS server and render the app on the server at runtime. A few extra dependencies are required if you plan on using Firebase server-side.
npm install --save-dev express webpack ts-loader |
Create server.ts
in the project root.
// These are important and needed before anything else |
Step 4 - Create a Webpack Config to Compile the Server
Create webpack.server.config.js
in the project root.
const path = require('path'); |
Step 5 - Build Scripts
Here are the commands to build and run your Universal app.
- Build Angular browser app
ng build --prod
- Build Angular server app
ng run YOUR_PROJECT_NAME:server
- Build Express server
webpack --config webpack.server.config.js
- Serve the app
node dist/server.js
You might combine these commands in your package.json
scripts to simplify the build process.
"scripts": { |
Then run npm run build:ssr && npm run serve:ssr
to test your app locally on localhost:4000.
Deployment
App Engine Flex
At this point, you need a NodeJS server to deploy With an existing Firebase project, you can easily deploy your ExpressJS server to App Engine Flex (a paid service).
Install gcloud CLI tools and link your Firebase project. Update the start script in package.json:
"scripts": { |
Run gcloud app deploy
and you’re on the cloud.
Firebase Cloud Functions
Work in Progress.