Fix for long shown launch screen on Meteor app

January 26, 2016 in #Misc

In one recent project I came across an issue of app's launch screen being shown for quite a long time. To be exact - 6 seconds. Each and every time the app was started.

Looking into the Meteor's default packages, I found out it was the launch-screen package (now part of mobile-experience) responsible for showing the launch screen while the app is starting. After a bit of digging into the package, I found this code block:

Meteor.startup(function () {  
    if (!Template) {
    } else if (Package['iron:router']) {
            .onAfterAction(function () {
    } else {
        Template.body.onRendered(function () {

        setTimeout(function () {
        }, 6000);

Project did not use the iron:router package, instead it was using kadira:blaze-layout + kadira:flow-router, so the else if (Package['iron:router']) part was irrelevant. Final else statement is the point of interest - Template.body.onRendered(function () {...} was supposed to remove launch screen once body template is rendered, however, it did not.

The reason for it was quite simple - project did not have any <template name="body"> template - kadira:blaze-layout appends the template contents to body tag automatically, so it makes it natural to not create one.

Since there is no body template, Template.body.onRendered(function () {...} callback never gets executed and launch screen gets removed only after 6 seconds, which is a fallback behavior in order to avoid launch screen being shown for ever. There are 2 ways how to fix it.

1) Do create the body template and include the rest of templates within it - this will result in body template function Template.body.onRendered(function () {...} being run and launch screen removed. However, this solution has a side effect - blank page flicker.

Apparently, page contents do not get completely rendered when the function is executed and for just a short moment you see blank page. Not nice user experience, therefore consider second fix.

2) Manually remove the launch screen using template render callback function on home page / landing page template. If you have a template called "homepage", which is the first shown template in your app, following code will remove the launch screen 100 milliseconds after it is ready.

Template.homepage.onRendered(function () {  
    if (Meteor.isCordova
        && typeof navigator !== 'undefined'
        && navigator.splashscreen)
        setTimeout(function () {
        }, 100);

100 millisecond wait is enough for contents to be completely rendered while the launch screen is still visible, therefore, avoiding blank page flicker.

Launch screen should now be shown for just about 2 seconds - you'll notice the difference right away. App startup will feel much faster. So, if you are having same issue on Meteor app, just use the code above, replace the template name and done!