Progressive Web Apps – Web App Manifest

The web app manifest is a simple JSON file that gives you, the developer, the ability to control how your app appears to users in areas where they would normally see apps in mobile devices.

Screenshot_20180821-000049

 

Web app manifests are part of a collection of web technologies called progressive web apps, which are websites that can be installed to a device’s homescreen without an app store, along with other capabilities like working offline and receiving push notifications.

And, more importantly, how it behaves when it’s launched from the home screen.

Installs to the homescreen

When a user clicks “Add to homescreen”, they will see the app being added on the homescreen.

At a minimum, the manifest must contain the name of the app and a short_name.

The short_name is used on the home screen and in other places where there is limited space.

It also needs the start_url, the URL the app should open when launched from the home screen.

Defining the manifest metadata

 
{
  "short_name": "Pandiyan",
  "name": "Pandiyan Murugan",
  "icons": [{
      "src": "/img/portrait_small.jpg",
      "type": "image/jpg",
      "sizes": "192x192"
    },
    {
      "src": "/img/portrait.jpg",
      "type": "image/jpg",
      "sizes": "512x512"
    }
  ],
  "start_url": "/?source=pwa",
  "background_color": "#3367D6",
  "display": "standalone",
  "scope": "/",
  "theme_color": "#3367D6"
}

Make sure, the page you specify is cached as part of the app shell. Otherwise, you won’t get the benefits of a cached app shell and your app won’t work offline.

One quick tip.

To track the number of users who are launching your app from their home screen, you can add a query string to the end of your URL.

And use any analytics to track launches, with that query string.

But don’t forget to ensure that you’ve cached the URL with the query string, in your service worker.

Adding icons and Splash screen color

Finally, we need to provide a set of icons for things like the home screen icon and the tab switcher, and splash screen.

The icons parameter takes an array of icons and must include the source, the size of the icon, and the type.

For example, image/jpg.

I recommend providing eight icon sizes,

48 x 48, 96 bx 96, 128 bx 128, 144 x 144,

192 x 192, 256 x 256, 384 x 384 and 512 x 512.

Just make sure you have icons for 1x, 2x, 3x and 4x devices.

Chrome uses the 48 device independent pixel icons for the home screen icon and the tabs footer. And the 128 device pixel icons for the splash screen.

Those are the minimum requirements. But there are a few other helpful things that you should set in the manifest.

The background color, and theme color are used by the browser along with an icon, as part of the splash screen.

Shown the instant the web app is launched, until its first render. As we provide the splash screen color as blue in manifest. The splash screen, icon and short name is displayed while the browser is rendering the application. (http://pandiyan.cool)

Once the app is loaded, the theme color tells the browser, what color to display in the UI elements such as the address bar or the notification tray.

Display and orientation property give you control over how the app is displayed. For example, you can hide the address bar and the back and forward buttons, by setting “display”: “standalone”. Or if you’re building a game that works better in landscape, you can force landscape view by specifying, “orientation”: “landscape”.

Web apps will launch full-screen with no vestiges of a browser. The URL will not be present, nor will traditional browser actions such as bookmarking and navigation controls.

Linking the manifest

When you have created the manifest add a link tag to all the pages that encompass your web app:

<link rel="manifest" href="/manifest.json">

Manifest metadata in browser

To confirm whether the manifest is properly, we can use the chrome DevTools to view the manifest as below

(Switch to Application tab in the Chrome Dev Tools)

manifest

Happy Coding!

The Debouncing technique

Event Listeners are really a great bonus for JavaScript. They help in improving the user’s experience and interaction with the web page. JavaScript has a lot of event listeners, a whole lot of them. These event listeners are very active and they keep watching your every move on a webpage (Beware! They are watching you!). When the desired action/event occurs, they immediately call the function that is attached to them.

As you know, computers are very fast, incredibly fast, a simple event can be fired a zillion times. For example, a scroll event listener will fire a million times for every scroll that you do, the same goes for the resize event. Just imagine how much load it can put on browsers if a function is called million times continuously? Phew! Poor browser 😔 Here comes our savior and hope “The debouncing and throttling function”.

Debouncing is a technique that is used to limit the rate of execution of a function. This drastically improves the performance of a browser, when a tedious function is attached to an event Listener like onScroll, onResize, etc. A debounce function in minimum takes two parameters – the function to be debounced and the wait time in milliseconds and in turn returns a debounced function. Now with this returned function you can debounce a scroll/resize event handler to be fired once for every specified wait time. A simple debounce function as taken from Underscore.js is given below, it is very simple, it simply calls the function passed to it at regular intervals using the setTimeout and clearTimeout functions.

// From Underscore.js library
function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this,
            args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

 

You have read till here and still clueless about what a debouncing function is? I have an analogy for you 😃 Read until the end!I know a teacher back in my school, she was very good in teaching but at the same time, she had eyes all-over the school, in the walls, canteens, classrooms and literally everywhere. She used to watch all my mischiefs and report them back to my parents immediately. In short, she was a mischief listener (a kind of event Listener). As soon as I do a mischief and before I could reach home, there would be a great welcome ceremony organized by my mom with dad as a chief guest along with spicy scoldings and sometimes with more delightful trashings.

Let me write the above in JavaScript using JQuery

$('Samuel').on('mischief', reportHisParents());
As days went by, I became naughtier and thus she had to often report back to my parents. This was going on until one day, my parents became tired and annoyed by my mischiefs and her reportings. So, one day they visited my school and told her “Hello madam, It is good that you report our sons mistake every now and then, but we are tired of hearing them every day, can you please report them on a monthly basis, during every parents-teachers meet up?”. She replied “I would be glad to”, with an evil smile. From then on, my mischiefs were not reported immediately to my parents but only once in a month, during the Parents-teachers meetup. 😃
Then it became
var reportMonthly = debounce(reportHisParents(), 2629800000); 
// 2629800000ms = 1 month

$('Samuel').on('mischief', reportMonthly);

This is what debouncing does, it does not fire a function as soon as it is invoked, instead, it waits for the specified time and then fires the function.

Hope you understood the concept of debouncing. If you like this post and want more posts like this on the web and tech-related topics, please subscribe (there is a button in the bottom right corner). Also feel free to share your thoughts in the comment section 😃

Meet you with a next post about throttling function!

PS: I was a very good kid during my school days. You can find me on the first bench, doing all my homework on time and laughing at all the silly jokes that my teachers would crack. The story above was made up just to illustrate the concept of debouncing 😃 To know more about me visit my blog.

References:
JavaScript Debounce Function