Brewery List
Beer List
Age Verification
Living in San Diego, we are surrounded by over 140 craft breweries. Once a year, Stone Brewing hosts an annual celebration, where they invite a bunch of their brewery friends to town and showcase their brews. I have been attending for a number of years, and one of the challenges in attending was having to find the beer on Untappd and check-in. So this year I thought I would whip up a quick Progressive Web App (PWA), that would allow me to quickly locate my beer and check in. Here is a link to the working Stone Fest 21 app.
After assembling the data set of the breweries, the beers, their logos (thanks, Untappd!), I generated the actual Ionic application. Since my buddies were on both iOS and Android, I knew the web was my only publishing option. But before I uploaded the code from Ionic to my server, there were some additional steps required to improve the performance of the PWA. Here is a list of the changes I made to project:
Enable gzip
By default, my Dreamhost account that I hosted Stone Fest 21 on did not have this enabled. So, I had to create a .htaccess file and enable it on the server.
# BEGIN GZIP
< ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
< /ifmodule>
# END GZIP
Improving iOS support
Since PWA support on iOS is not on par with Android was to include some Apple specific meta tags in the head of the index.html file:
< meta name="apple-mobile-web-app-capable" content="yes">
< meta name="apple-mobile-web-app-status-bar-style" content="black">
< meta name="apple-mobile-web-app-title" content="Stone Fest 21">
For more on these tags, see the Apple documentation.
App Icons
Although the manifest.json defines our app icon, not all platform understand this. So as a backup, I add this snippet in the index.html file to assist with that issue:
< link rel="apple-touch-icon" sizes="57x57" href="apple-icon-57x57.png">
< link rel="apple-touch-icon" sizes="60x60" href="apple-icon-60x60.png">
< link rel="apple-touch-icon" sizes="72x72" href="apple-icon-72x72.png">
< link rel="apple-touch-icon" sizes="76x76" href="apple-icon-76x76.png">
< link rel="apple-touch-icon" sizes="114x114" href="apple-icon-114x114.png">
< link rel="apple-touch-icon" sizes="120x120" href="apple-icon-120x120.png">
< link rel="apple-touch-icon" sizes="144x144" href="apple-icon-144x144.png">
< link rel="apple-touch-icon" sizes="152x152" href="apple-icon-152x152.png">
< link rel="apple-touch-icon" sizes="180x180" href="apple-icon-180x180.png">
I used the $ ionic cordova resources
command to generate them, then renamed the manually. Since the $ ionic build command will wipe out the www directory, I added them directly to the server.
Updating the BODY tag
One of the items that the Lighthouse test measures is “First Meaningful Paint”. For those who don’t know this term, First Meaningful Paint is the time when page’s primary content appeared on the screen. I added inline CSS to the body tag, so that the browser would render something while the app was starting.
< body style="background-color: #1c1b17; background-image: url('assets/imgs/logo.jpg'); background-position: center; background-repeat: no-repeat;">
Handling the no JavaScript case
Lighthouse also checks what you do if JavaScript was disabled by the user. Remember, this is a Progressive web app. In my case, there was not a lot you can do without JavaScript, but I included this tag to meet the requirement:
< noscript>
< h1>Stone Fest 21 requires JavaScript< /h1>
< /noscript>
Removing Cordova
When you generate an Ionic application, Cordova plugins are automatically integrated, specifically the Splash Screen and Status Bar plugins. Since we are deploying to the web, we can remove Cordova and these plugins from our project. Using $ npm uninstall @ionic-native/splash-screen
and $ npm uninstall @ionic-native/status-bar
Edit the app.module.ts to remove the import statements for these two plugins and remove them from the providers array.
In the app.component.ts file also remove the imports. Also, you will remove the injected imports from the arguments in the constructor, as well as the two references to the plugins in the platform.ready check.
If you generated your Ionic application with Cordova integration, there are other plugins and modules that you should remove as well. If you open your package.json file you will several references to cordova-* items. Go ahead and use npm to uninstall them:
- cordova-plugin-console
- cordova-plugin-device
- cordova-plugin-splashscreen
- cordova-plugin-statusbar
- cordova-plugin-whitelist
- and ionic-plugin-keyboard
Depending on what platforms you may have installed, you might also have these modules:
- cordova-ios
- cordova-android
Remove them as well.
Finally, we can also remove @ionic-native/core, as we have now scrubbed our Ionic application of any Ionic Native code.
Image Paths
As I checked my Lighthouse score, I noticed that it was flagging how my brewery logos were being called, as well as an accessibility issue around them. To quickly solve this, I adjusted the img tag to reference the full path to the logo and added the alt attribute
< img src="https://aj-software.com/apps/stone/assets/breweries/{{brewery.logo}}" alt="Company Logo">
Enabling the Serviceworker.js code
By default, this code block is commented out in the index.html. Uncomment this block of code and you have a nicely configured service worker ready to go.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js')
.then(() => console.log('service worker installed'))
.catch(err => console.error('Error', err));
}
Updating the manifest.json file
The last tweak is updating some of the items in the default manifest.json file.
- Update the name and short name. Note: the short name should not be more than 12 characters in length.
- Ensure there is an icon available in the proper directory and of the proper size.
- Update the theme and background colors to something that matched our app.
- Add an orientation value of portrait
{
"name": "Stone Fest 21",
"short_name": "Stone Fest",
"start_url": "index.html",
"display": "standalone",
"icons": [{
"src": "assets/imgs/logo.jpg",
"sizes": "512x512",
"type": "image/jpg"
}],
"background_color": "#1c1b17",
"theme_color": "#1c1b17",
"orientation": "portrait"
}
Summary
After implementing these steps, the Stone Fest 21 app received the following Lighthouse score:
Hopefully, these guidelines can help you create better PWA using Ionic. Until stencil.js is ready, this is about a good as I can make an Ionic-Angular’s performance. I will be porting this application to stencil shortly and will update this post with the results. The source code is available on my GitHub account.
You must be logged in to post a comment.