Exercise 12 : Bitcoin Value Fetcher
Purpose and learning process
In this exercise you will learn the basics of the PWA development:
- creating a app manifest file
- registering service worker in index.html
- understand install, activate and fetch events in service worker
- caching files to use offline mode
- fetching bitcoin value from the web
- saving data to device's local storage
- basic use of chrome development tools with PWA
In this exercise you will learn how to create a PWA application, which fetches Bitcoin value from CryptoCompare. The main purpose is to learn the basics of PWA. You will test your PWA in web browser and in a real device or mobile emulator.
Example screenshots
Demo app is a Bitcoin Fetch app which fetches bitcoin value from the web.
Example Video
Here is an sample video how the app will/should work when this exercise is finished: https://youtu.be/aKHdwS2VaBM
Project
Create a new project folder for this exercise.
1 |
|
manifest.json
Create a manifest file for the app. You can download icons from here: bitcoin_icons.zip
Tip
Remember check PWA materials to understand manifest settings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
index.html
Create a new index.html file, which now only have a few meta elements, link to styles and manifest and a few basic UI elements. We will later add here a few lines of JavaScript code and register Service Worker.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
styles.css
Create a styles.css file for your project.
1 2 3 4 5 6 |
|
Run local web server
You can use any “tools” to test your project in your local machine. One good way to test is to install Web Server App for Chrome. This tool serves web pages from a local folder over the network, using HTTP. Runs offline. You can install it from here: Web Server for Chrome.
Install it and launch. Choose folder point to your project folder and start web server. Use chrome and go to following address: http://127.0.0.1:8887.
You should see index.html running in Chrome. Of course nothing is working yet – UI is only ready 🙂
Service Worker
Next step is to add a service worker to your project. Create a serviceworker.js to your project.
Cache
First define files to be cached:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Note
Remember, that service worker is running install, activate and fetch events.
Install event
In install event all the cache files will be cached. Add a following code to your service worker file.
1 2 3 4 5 6 7 8 9 |
|
Activate event
Now activate event is not doing anything special, it only removes old cached files if service worker has been updated. Add a following code to your service worker file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Fetch event
A final step with a service worker is to add fetch event listener. Here we have added a few console.log lines that we can see from the console what is happening in our PWA app when it is launched and run in online/offline mode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Register Service Worker in index.html
Service worker is now ready and it need to be registered in index.html file. Add a following script below container div in index.html.
1 2 3 4 5 6 7 8 |
|
Save and test
Open a developer tools in Chrome, select Application tab and select Service Worker. Reload your web page. You should see that your service worker is now running. Note also messages from the console. Service worker has been installed and registered. Cached files will be saved and finally service worker is active.
Click image to see it bigger!
Fetch Bitcoin value
Modify your index.html file and add onclick event handling for the button.
1 |
|
Create a new <script>
element and add fetchBitcoin
function. You can use cryptocompare.com min API to fetch BTC data. Server will send BTC data with a following JSON format:
1 |
|
Now a pure JavaScript is used so you need to use XMLHttpRequest
to fetch data from the web. Finally BTC value will be displayed in UI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Tip
Link : XMLHttpRequest
Save and test
Test your application and it should load and show BTC value.
Save and load data to/from Local Storage
Next step is to save fetched BTC value to local storage. So when app is launched, the last value will be shown. Modify fetchBitcoin
function to save value to local storage:
1 2 |
|
Now BTC value with date will be saved to local storage with a btc
key. Modify index.html body element to load value from the local storage.
1 |
|
And create a lastBTC
function inside a script element.
1 2 3 |
|
Save and test
Your browser might cache your index.html file so you need to clear cache to get it working. Select Clear storage from developer console’s application tab and click Clear site data.
Refresh your page, fetch a new BTC value, refresh it again and it should display previously fetched BTC value from the local storage.
Offline
In this exercise, we don’t implement any fancy offline UI to the app, just only a nice alert prompt. But you can be a innovative and modify it better. Modify your fetchBitcoin
function to display an alert message, if device is offline when a BTC value is tried to fetch from the server side.
1 2 3 4 5 6 |
|
Now use developer tool’s Application tab and switch browser to offline. Refresh your app and see how page contents are loaded from the cache and button gives above alert message.
Publish
Copy your project folder to web server which supports HTTPS-connection. Go to your server with a Chrome and test how it works from remote server. Yes – it should work the same way.
Testing with browser
Open development tools and select Lighthouse. Leave default options as they are and Generate report.
Now your page is tested and you should see there results – more importantly PWA results.
If everything went correctly, you should see that your app is working as a PWA. Scroll results to Progressive Web App and see there detailed results.
Testing with Mobile
Use your mobile device’s browser and go to your page. You will get best results with Android and Chrome. Test Safari with iOS.
You should see the same kind of page what you have seen in the desktop browser.
Chrome detects this page is PWA and ask end user to add this page to the home screen.
User can find a launch icon from the home screen.
PWA is launched, notice there aren't location bar at the top of the app. App looks as a native one!
Push to GitLab
Test your application in emulator, take screenshots (add those to your project folder) and commit/push your mobile-exercises repository back to JAMKIT/GitLab. Remember move your exercise/issue ticket from Doing to In Review in Issues Board and write your learning comments to issue comments.