Day 13

The sounds of the crickets have been replaced by a babbling brook and blankets for a sleeping bag. Next to me are H — and O — . They fell asleep not too long ago. H — has his four stuffed-animals…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating Your First Vue.js PWA Project

In our previous article, Why Progressive Web Apps, we have identified a few benefits that PWA provides to improve web applications for online and offline usage. With a bit of responsive design, Progressive Web Apps would be perfect for both mobile and desktop markets.

In this article, we will cover how to build our first PWA project with the Vue.js framework by implementing the PWA minimum requirements, configure Webpack to build a caching services worker for offline support and run the Google Lighthouse audit tool to identify improvement points.

Anywhere on your computer, let’s create a directory named pwa-vue-app-1.

Making a new directory in iTerm2 (terminal application)

All work will be performed within this directory so with our preferred choice of editor and terminal, open this directory.

Visual Studio Code is my choice of editor. It comes with an integrated terminal so I will not need to use an external terminal application. This article does not need anything fancy long as you can create files, folders, and execute terminal commands.

Before writing code, we need to perform a few pre-setup tasks. The first task is to create the folder structure to organize source and distribution code. Structures will vary per project and even user preference.

For this project, we will keep the structure simple. In the root directory, we will create directories dist, src, and static.

Creating the directory structure

Additionally, we will create the components directory inside the src directory. The components directory contains all of the individual components view, styling, and logic that our application uses.

The second pre-setup task is to initialize our project with NPM and install dependencies.

To initialize our project with NPM, in the pwa-vue-app-1 directory, execute in a terminal the command:

The initialize process will ask a few questions about the project. For this article, default values are OK. The package.json file is created once completed. The values can be customized, later, from the package.json file.

NPM initialize with user input

Once initialized, we can start installing the build and third-party dependencies. For the build dependencies, we will execute the following command in the root directory:

Build dependencies consist of:

Next, third-party dependencies, which are used by the application code, will be installed with the following command in the root directory:

In the root directory of our project, create a file called webpack.config.js and add the contents of the configuration posted below. This will allow us to build and preview our application.

I will point out a few of the options that are important to know but not cover all configurations detail.

Now that we have our structure, dependencies, and build tool configured, we can begin adding our application code.

index.html

First, create the file index.html in the static directory with the content below.

There are two important things about this file.

app.js

In the src folder, create the file app.js and add the contents below.

The app.js file is our applications entry file which initializes an instance of our Vue app.

During the instance creation, we pass in the targeting element, routes, accessible components, and templates. I will focus on the element and router options.

router.js

In the src folder create the file router.js and add the contents below.

This file contains a collection of known URL routes and maps to its associated component. We also provided a fallback component for unknown routes. This handles the “Page Not Found” use case.

App.vue

In the src folder create the file App.vue and add the contents below.

This file is the base template of the application. It consists of router-link(s) and a single router-view. Once a link, the router-link, is clicked, the associated component loads into the router-view element.

Component Pages

Create the next three files in the src/components/ directory with its respective content. These files are fillers to provide a working example of routing. The difference between these files is the header text and color.

Now that we have finished adding the code to the project, we can preview and audit the application.

To preview the application, we need to first add NPM scripts. In the package.json file, update the property scripts with the following:

The dev script uses the webpack-dev-server dependency to start a development web server.

Let’s preview the application, by running the npm run dev command in the project’s root directory. It should automatically open your default browser to https://localhost:9000. If you need to change the port, you can modify the server configurations in the webpack.config.js file.

App Preview in Google Chrome

Webpack development server is perfect for viewing and testing application code during development!

Google Chrome — Lighthouse Audits

Google Lighthouse

For auditing, I recommend deploying the application to a hosting service such as Firebase. Running an audit against your application on a hosted service can provide a more accurate result. This article will not cover how to deploy but will display the results from each audit.

One of the PWA requirements is that the application is hosted and served from an HTTPS protocol with valid SSL certificates. We had configured webpack-dev-server to use a self-assigned certificate but, it still does not meet the PWA requirements. This tool is strictly for development and testing and will always lack the features and settings from that a production ready web server provides.

To run Google Lighthouse for auditing, we need to open the DevTools. If you are a Mac user, you can use the following shortcut command Command+Option+I. If you use Window, you can press either F12 or Control+Shift+I. Next click on the Audits tab at the top. You should see something like this:

Google Lighthouse in DevTools

Next, click on the Run audits button at the bottom and watch it perform. Once the audit is complete, it will point out areas that need improvement for a higher PWA score as well as performance, accessibility, best practices, and SEO.

Localhost (webpack-dev-server) audit results
Firebase audit results from first run

Comparing the two screenshot above, we can see that our application on Firebase has a higher PWA, performance, and best practices score then locally. Firebase hosting takes care of some of the server side requirements such as HTTP redirect to HTTPS.

Below, is a list of items where the application failed to meet the requirements for a high PWA score.

Firebase audit PWA report from first run

In the next sections, we will implement the minimum requirements to make our application PWA as well as covering the items from the PWA checklist.

Let’s start adding the minimum PWA requirements and correction to increase our PWA score.

manifest.json

First, we will add the manifest.json file in the static directory with the following content:

This file contains the necessary information for installing to the home screen, creating a custom splash screen, and details about the application.

Next, add in the head tag of the index.html file the following line:

Icon

In the manifest.json file, we defined our application icons. In the Google Developers Web Fundamentals documentation, it is recommended that both 512 x 512 px icon and 192 x 192 px icons are provided. These icons are used for the home screen, install notification, and custom splash screen.

For this example, I made the icons with Photoshop and placed them in the static directory. You can get mine from here:

Service Worker

To simplify building our service worker for caching, we will use the Webpack plugin sw-precache-webpack-plugin. This plugin automatically creates a service worker file with a collection of the file paths that had been created by Webpack.

In the root directory of the project run the command.

Add to the top line:

Add in the plugins option array the plugin usage:

Below plugin:

Adding noscript

If for any reason an end user has JavaScript disabled on their browser, we need to notify, with the noscript tag, that our application requires JavaScript.

To fix this:

Open the file static/index.html and add to the body element line:

Set Address Bar Background

Open the file static/index.html and add to the head element line:

Let’s run the second audit to see how our application scores after implementing the minimum PWA requirements and corrections from the first audit.

Firebase audit results from second run

As you can see, creating a PWA project with the Vue.js framework is very easy. The initial setup of the workspace, dependencies, and build configurations only takes a couple of minutes of our time, but after that, it all application code.

Additionally, with the Google Lighthouse audit system, we can periodically test our application to pinpoint areas that need attention and get access to resources that give detail explanation of each issue and possible solutions.

Lastly, using a bundler tool such as Webpack provides many great advantages. Some of these advantages are simplifying and speeding up the development and building. As we saw in this example, we used the pre-cache service worker plugin create our service worker and the clean plugin to keep our distribution package clean.

Hope you find this useful and gives you a great starting point for your first PWA project.

Update 2019/09/02: All dependencies and related webpack configurations, in the sample project, were updated. This included using the latest version of vue and vue-router.

Add a comment

Related posts:

Are Gregarious Personalities Best for Relationship Building Positions?

Extroverted personalities have always been the preferred choice for positions focused on sales and relationship building. Is that the best?

Stop Using Us As Clickbait!

Sex work is a juicy topic, chock full of taboos and sexiness and literal sex (it’s right there! In the job description!) so it makes sense that articles purporting to be nuanced peeks behind the…

4 reasons why men hate shopping with women

Shopping is a favorite pastime for many women. From clothing to accessories, women love to browse and shop for their favorite items. However, this enthusiasm for shopping is not always shared by men…