Giter VIP home page Giter VIP logo

angular-universal's Introduction

AngularTourOfHeroes

This project was generated with Angular CLI version 7.1.4.

Development server

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Code scaffolding

Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.

Build

Run ng build to build the project. The build artifacts will be stored in the dist/ directory. Use the --prod flag for a production build.

Running unit tests

Run ng test to execute the unit tests via Karma.

Running end-to-end tests

Run ng e2e to execute the end-to-end tests via Protractor.

Further help

To get more help on the Angular CLI use ng help or go check out the Angular CLI README.

Demo

Server Side Rendering

Server Side Rendering (SSR) is the process of taking a client-side JavaScript Framework website and rendering it to static HTML and CSS on the server. Why is this important? We all want fast loading websites and SSR is a tool to help you get your website rendered faster.

Simply, when a client access the application, request passes to browser, browser load(download) all html, css and js code (converted code of Angular code) and then show it to users. To do all process it takes a long time when application is big, so meanwhile an empty template is vissible which reduces SEO and Performance capibilities of application. So SSR works like, it shows static html, css and in background browser download the js file when this process completes then server render the fully functioned page to user.

SSR flips the process of rendering a JavaScript framework app and when done right can give your users a better page load.

Why use Server Side Rendering

Here are three main reasons to create a Universal version of your app.

  1. Facilitate web crawlers (SEO)
  2. Improve performance on mobile and low-powered devices
  3. Show the first page quickly

SSR with Angular Universal

An Angular application is a Single-page App - it runs in a client’s browser. Angular Universal, however, let’s you also run your Angular app on the server. This enables you serve static HTML to the client. With Angular Universal, the server will pre-render pages and show your users something, while the client-side app loads in the background. Then, once everything is ready client-side, it will seamlessly switch from showing the server-rendered pages to the client-side app. Your users shouldn’t notice a difference, beyond the fact that instead of waiting for your “Loading” spinner to finish, they at least can have some content to keep them engaged until they can start using the fully-featured client-side application.

Adding Universal to Your App

From your app directory open a terminal and run the following command:

$ ng add @nguniversal/express-engine --clientProject {{ name of your project }}

Start Your Universal App

From a command line, run the following command:

$ npm run build:ssr && npm run serve:ssr

  • Open your browser to http://localhost:4000 (or whatever port is configured for you), and you should see your Universal app in action! It won’t look any different, but the first page should load much quicker than your regular Angular application.

  • Also, try viewing the page source (right-click on page and select “View Page Source”). You’ll see all normal HTML in the tag that matches what is displayed on your page - meaning, your application can be meaningfully scraped by a web crawler. Compare this with the page source of a non-Universal application, and all you’ll see in the tag is (or whatever you’ve called the selector for your bootstrapped AppComponent).

Deploying an Angular Universal App on Heroku

  1. Add a Procfile
  • How to create a Procfile?
    • The same way you create any file, just name it Procfile with no extension, none at all.
  • What goes in the Procfile?
    • web: npm run start:heroku
  1. Edit package.json
  • Add a line under the scripts object of our package.json and put in the following code: “start:heroku”: “node dist/server”
    • This command will eventually start the node server that will be built by Angular Universal.
  • create a postinstall script
    • What is a postinstall script?

      A “postinstall” script is a script that is run automatically by Heroku after npm has finished installing all the dependencies on each build. In this case, we want to make sure that our dist server is built and ready for deployment by Heroku, so what we can do is kick off the build:ssr command on Heroku’s end, after Heroku has received the code.

    • How do I create it?

      To do this, add another line under the “scripts” object of our package.json file and put in the following code: “postinstall”: “npm run build:ssr”.

What is PWA ?

A Progressive Web App or PWA is a web application that has a set of capabilities (similar to native apps) which provide an app-like experience to users. PWAs need to meet a set of essential requirements that we’ll see next. PWAs are similar to native apps but are deployed and accessible from web servers via URLs, so we don’t need to go through app stores.

A pwa need to be:

  • Progressive: Work for every user, regardless of browser choice, because they are built with progressive enhancement as a core tenet.
  • Responsive: Fit any form factor, desktop, mobile, tablet, or whatever is next.
  • Connectivity independent Enhanced with service workers to work offline or on low-quality networks.
  • App-like: Use the app-shell model to provide app-style navigation and interactions.
  • Fresh: Always up-to-date thanks to the service worker update process.
  • Safe: Served via HTTPS to prevent snooping and ensure content has not been tampered with.
  • Discoverable: Are identifiable as “applications” thanks to W3C manifests and service worker registration scope allowing search engines to find them.
  • Re-engageable: Make re-engagement easy through features like push notifications.
  • Installable: Allow users to “keep” apps they find most useful on their home screen without the hassle of an app store.
  • Linkable: Easily share via URL and not require complex installation.

Add Progressive Web App (pwa) in angular

$ ng add @angular/pwa

The command automatically adds PWA features to our Angular application, such as:

  • A manifest.json file,
  • Different sizes of icons in the src/assets/icons folder,
  • The ngsw-worker.js service worker.

Audit Report before SSR and PWA and after SSR and PWA

angular-universal's People

Contributors

mittalprince avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.