Steps to introduce PWA into Vue project

Steps to introduce PWA into Vue project

It is very simple to introduce PWA into the Vue project. The steps are as follows:

1. Install dependencies

vue add @vue/pwa

Since the add keyword is used, some files will be created in the project after a successful installation. If the project uses git, you can easily see the file changes:

A registerServiceWorker.js file will be generated in the src folder and imported in main.js. This file automatically generates the code for registering the service worker. The code of registerServiceWorker.js is as follows:

import { register } from 'register-service-worker'

if (process.env.NODE_ENV === 'production') {
  register(`${process.env.BASE_URL}service-worker.js`, {
    ready () {
      console.log(
        'App is being served from cache by a service worker.\n' +
        'For more details, visit https://goo.gl/AFskqB'
      )
    },
    registered () {
      console.log('Service worker has been registered.')
    },
    cached () {
      console.log('Content has been cached for offline use.')
    },
    updatefound() {
      console.log('New content is downloading.')
    },
    updated () {
      console.log('New content is available; please refresh.')
    },
    offline () {
      console.log('No internet connection found. App is running in offline mode.')
    },
    error (error) {
      console.error('Error during service worker registration:', error)
    }
  })
}

2. Configure pwa in the vue.config.js file:

module.exports = {
  pwa:
    workboxOptions: {
      skipWaiting: true,
      clientsClaim: true,
      importWorkboxFrom: 'local',
      importsDirectory: 'js',
      navigateFallback: '/',
      navigateFallbackBlacklist: [/\/api\//]
    }
  }
}

3. Manually add the manifest.json file to the public directory of the project. The content of manifest.json is as follows:

{
  "short_name": "Application short name", // Will be displayed under the mobile desktop application icon in the future "name": "Application full name", // Will be displayed under the computer desktop application icon in the future "icon": [
      {
          "src": "./img/icons/android-chrome-192x192.png",
          "sizes": "192x192",
          "type": "image/png"
      },
      {
          "src": "./img/icons/android-chrome-512x512.png",
          "sizes": "512x512",
          "type": "image/png"
      }
  ], // Desktop icons, an array, pay attention to the image size and format "start_url": "index.html", // URL when the application starts
  "display": "standalone",
  "background_color": "#080403",
  "theme_color": "#080403"
}

The display field indicates the display mode. The specific parameters and descriptions are as follows:

Display Mode describe
fullscreen Full screen, all available display area is used, and no status bar chrome is shown.
standalone Make this app look like a standalone application, including having a different window, its own icon in the application launcher, etc. In this mode, the user agent removes UI elements used to control navigation, but may include other UI elements such as the status bar.
minimal-ui The app will look like a standalone app, but will have a browser address bar, the style of which varies by browser.
browser The application opens in a traditional browser tab or in a new window, depending on the browser and platform; this is the default.

4. Add the following code to the index.html file:

<meta name="theme-color" content="#080403">
<link rel="manifest" href="manifest.json" rel="external nofollow" > 

Next is the moment to witness the miracle. Execute npm run build to check whether the manifest.json file is introduced in the index.html file under dist, and then see if the corresponding configuration is generated. If so, congratulations, your first pwa project is completed!

The above are the details of the steps to introduce PWA into the Vue project. For more information about introducing PWA into the Vue project, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • A very detailed guide to building a VuePress site and enabling PWA and automatic deployment
  • How to introduce Excel table plug-in into Vue
  • Two ways to introduce svg icons in Vue
  • How to introduce bootstrap, elementUI and echarts into Vue project
  • How to introduce and use jQuery in Vue
  • Vue-cli4 configures element-ui to introduce operations on demand
  • Vue project introduces echarts to add click event operation
  • vue-cli3 introduces font-awesome operation
  • Vue page introduces three.js to realize 3D animation scene operation
  • Using Vue to introduce maptalks map and achieve aggregation effect
  • A brief discussion on solving the problem of dynamically importing pictures img404 in the vue-cli3 project

<<:  Mybatis+mysql uses stored procedures to generate serial number implementation code

>>:  Nginx content cache and common parameter configuration details

Recommend

Analysis and solution of MySQL connection throwing Authentication Failed error

[Problem description] On the application side, th...

Error mysql Table 'performance_schema...Solution

The test environment is set up with a mariadb 5.7...

How to quickly clean up billions of data in MySQL database

Today I received a disk alarm exception. The 50G ...

win2008 server security settings deployment document (recommended)

I had been working on the project before the New ...

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

HTML table tag tutorial (20): row background color attribute BGCOLOR

The BGCOLOR attribute can be used to set the back...

Several mistakes that JavaScript beginners often make

Table of contents Preface Confusing undefined and...

Detailed explanation of the use of MySQL DML statements

Preface: In the previous article, we mainly intro...

Rhit efficient visualization Nginx log viewing tool

Table of contents Introduction Install Display Fi...

CSS mimics remote control buttons

Note: This demo is tested in the mini program env...

Learn about CSS label display mode in one article

Tag type (display mode) HTML tags are generally d...

MySQL batch removes spaces in a certain field

Is there any way to remove spaces from a certain ...

Tutorial on installing MySQL8 compressed package version on Win10

1 Download MySQL8 from the official website and i...

Automatic backup of MySQL database using shell script

Automatic backup of MySQL database using shell sc...

Detailed explanation of the construction and use of Docker private warehouse

The image can be saved on hub.docker.com, but the...