Hello, in this tutorial, I will show you why and how to get your Angular project ready for Server-Side Rendering using Angular Universal and how to publish it onto Firebase using Hosting and Cloud functions.
Hello, in this tutorial, I will show you why and how to get your Angular project ready for Server-Side Rendering using Angular Universal and how to publish it onto Firebase using Hosting and Cloud functions.
What is the difference between a Normal Angular App vs. a Server-Side Rendered Angular app?
When you publish a normal Angular application on the web, it will get downloaded and executed in the browser.
Normal Angular App
This causes search engines and social media sites to think that your site only has one page because they rely heavily on meta elements.
Server-Side Rendered App
With Server-Side Rendering, you’ll get a different HTML page for each page in the application because they are created on the server. This way, you can have different meta elements on each page.
Page 1
Page 2
Begin Converting Your Angular App to SSR
To begin, open a new or existing Angular project in a code editor and add Angular Universal to the project.
ng add @nguniversal/express-engine.
The command will create and update a couple of files in the project and that’s all you have to do. The application is now ready for Server-Side Rendering.
Publishing the App to Firebase
To publish the project to Firebase, go the angular.json file and change the outputPath for the build.
By doing this, it’ll place our production files in a folder call functions when the project is built.
npm run build:ssr
Once the project is built, you can see a folder call dist. Inside the dist folder is another folder call functions. And inside the functions folder are the production files.
Now locate the file call server.ts. This file is added to the project when you run the command to add Angular Universal. The code in this file is what the server will look at when generating the web pages.
const distFolder = join(process.cwd(), "dist/functions/browser");
However, this path will only work if we’re testing locally on the computer.
When we deploy the project to Cloud functions, the root directory will change and it’ll be the functions directory. The website files will no longer be in the path we defined earlier.
const websiteFileLocation = environment.production? "browser" : "dist/functions/browser";
Replace the string for the distFolder with the variable and save the project.
const distFolder = join(process.cwd(), websiteFileLocation );
In the terminal, instead of using ng serve to start the local server, use npm run dev:ssr.
cd dist
Then initialize Firebase hosting.
Initializing Firebase Hosting
firebase init
For the public directory, use the location of the browser folder and then configure as a single-page application.
Initializing Firebase Cloud Functions
Once Hosting is ready, initialize Cloud Functions.
We want to install the dependencies.
When Cloud Functions is done, go to the package.json file for the project and copy all the dependencies.
Cloud Functions package.json
Save the project and in the terminal, change to the Functions directory and install the packages.
npm i
Once the packages are installed, go to the index.js file and create a request function for Cloud Functions.
Inside the parenthesis of the request, we want to use the express app function from the server.ts file.
Server.ts
Modifying the Firebase.json File
The last thing we need to do is to go to the firebase.json file and change destination to function in the rewrites array. This will tell Firebase Hosting to call a function from Cloud Functions instead of using an HTML page.
Save the project and run firebase emulators:start in the terminal.
firebase emulators:start
This will help us test our code in a Firebase environment before uploading it to Firebase. Copy the hosting address that is generated and paste it in the browser.
localhost:5000
firebase deploy --only hosting,functions