Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Disclaimer: This blog post will be a focused step-by-step tutorial of how to deploy an Angular Universal App using Firebase Hosting. For any explanations about Angular Universal and Server Side Rendering, Angular has a great documentation on theirĀ website.
You can also find the source code onĀ Github.
Requirements
- node.js (I am using v8.11.1 for this tutorial)
- Angular 6+ (I have written a similar article for deploying Angular <Ā v6)
Part I: Set Up Angular AppĀ š
1. Install global dependencies
We are going to use @angular/cli and firebase-tools in command line to build and deploy yourĀ app.
2. Create a new AngularĀ project
Using @angular/cliĀ , we are going to create a new angular app. In this case, I will name it angular-universal-firebaseĀ .
3. Install @angular/platform-server
To build and render your universal app, we need to install @angular/platform-serverĀ .
4. Add Server Side Rendering Config
In @angular/cli@v6.0.0+Ā ,Ā .angular-cli.json is changed to angular.jsonĀ . This defines how our project is structured and the build configurations for this project. We would want to add a server configuration for the project in the projects.PROJECT_NAME.architect path.
Note that weāve added server that defines the builder and options config for the server side version of ourĀ app.
5. Modify project output to FirebaseĀ folder
For simplicity, we will build the browser version of our app in the same directory as where we are building our sever version to be server side rendered in Firebase. To do this, edit angular.json ās PROJECT_NAME.architect.build.options.outputPath to functions/dist/browser.
6. Create necessary files for app serverĀ version
- src/app/app.server.module.ts
Create a new module for the appās serverĀ version.
https://github.com/aaronte/angular-universal-firebase/blob/master/src/app/app.server.module.ts
- src/main-ssr.ts
Create an entry point for the server module. This is the main file we referenced in the server configuration in angular.jsonĀ .
https://github.com/aaronte/angular-universal-firebase/blob/master/src/main-ssr.ts
- src/tsconfig.app-ssr.json
Create the tsconfig for the server version. Similar to the browser version except angularCompilerOptions.entryModule which will reference the entry module for the server version that we just created. This is also referenced in angular.json as tsConfig.
https://github.com/aaronte/angular-universal-firebase/blob/master/src/tsconfig.app-ssr.json7. Include server transition in appās browserĀ module
Since we are sending the server version of your app to the browser before the browser version, we need to addĀ .withServerTransition({ appId }) when adding BrowserModule inĀ imports.
https://github.com/aaronte/angular-universal-firebase/blob/master/src/app/app.module.ts
Now we are ready to build the server and browser versions of ourĀ app!
8. Build browser and server versions of theĀ app
Using @angular/cli, we will build the two versions of theĀ app.
- ng build --prod: This will build the browser version of the app with prod configurations.
- ng run PROJECT_NAME:server: This will build the server version of the app. It will generate a ngFactory file that we can use to render our app usingĀ node.
When both builds are done, you should now have a functions folder in your root directory with browser and server folders in it. Awesome!!! š
Part II: Deploying with FirebaseĀ š
[1] Before continuing, you should have had created a firebase project here. I named mine angular-universal-firebase for thisĀ case.
1. Log in to `firebase` in the commandĀ line
Log in to firebase in the command line with the same google account you used to create your firebase project inĀ [1].
2. Initialize Firebase in the `angular` project
Initialize firebase configurations through the commandĀ line:
- Select Functions and Hosting for features to setĀ up
Firebase set up configuration (Functions andĀ Hosting)Javascript as Cloud function language for simplicity
- Select the firebase project you created in [1]. (In my case, itās angular-universal-firebase.
- Accept all defaults in this stage; we will configure the rest in later steps. (In this tutorial, we will write our functions in Javascript).
3. Add package dependencies to `functions`
Since we are using a node server through firebase-functions, We need to include angular dependencies in functions/package.json to render the server version of theĀ app.
Aside: Right now, I donāt know any way to mitigate this duplication of dependency declaration since as far as I know, you canāt access files outside the functions directory in any firebase-functions javascript files. But if you know a way, please let meĀ know!
https://github.com/aaronte/angular-universal-firebase/blob/master/functions/package.json4. Install packages in `functions` directory
Install da dependencies!
5. Create Firebase function to serve theĀ app
We are going to use functions.https.onRequest Firebase function type to send the response from our express server. There are a lot of things going on in this file but the most notableĀ are:
- Importing AppServerModuleNgFactory which was generated in Part I: Step 8āāāserverĀ version.
- Creating an index variable which is getting the index.html file we generated from Part I: Step 8āāābrowserĀ version.
- Using renderModuleFactory to generate an html file that we send as a response with url and document parameters.
- url parameter determines which route of the app is going to be rendered. Specifying this allows renderModuleFactory to build the html of thatĀ route.
- document is the full document HTML of that page that should be used to render. In this case, it will be the browser version of index.html of theĀ app.
https://github.com/aaronte/angular-universal-firebase/blob/master/functions/index.js7. Configure FirebaseĀ hosting
Now that we have built the function to render pages, we need to change the firebase hosting configuration to use this function. Change the hosting.rewrites in firebase.json.
https://github.com/aaronte/angular-universal-firebase/blob/master/firebase.json8. Rename`public/index.html` from root directory
This is so Firebase wonāt serve the html file but rather run the ssrfunction. You can rename it to whatever name other than index. We canāt simply delete this file since Firebase would not deploy with an empty public directory. For simplicity, I will rename public/index.html to public/index2.html.
9. Deploy to Firebase šĀ š„
If all things went well, you should be able to deploy your app to Firebase:
Thatās it!Ā š
You can check out the source code onĀ Github.
I hope this tutorial was helpful in some way! If you have any feedback or questions, add them on the Github issues to ensure everyone looking at the code would benefit.Ā š
Happy coding!Ā š
Deploying Angular Universal v6+ with Firebase š š„ was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.