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

Basic usage of wget command under Linux

Table of contents Preface 1. Download a single fi...

MySQL5.7 single instance self-starting service configuration process

1.MySQL version [root@clq system]# mysql -v Welco...

What qualities should a good advertisement have?

Some people say that doing advertising is like bei...

How to use VUE to call Ali Iconfont library online

Preface Many years ago, I was a newbie on the ser...

Vue3 AST parser-source code analysis

Table of contents 1. Generate AST abstract syntax...

How to implement Linux deepin to delete redundant kernels

The previous article wrote about how to manually ...

How to install MySQL 8.0.13 in Alibaba Cloud CentOS 7

1. Download the MySQL installation package (there...

Common parameters of IE web page pop-up windows can be set by yourself

The pop-up has nothing to do with whether your cur...

Summary of some common uses of refs in React

Table of contents What are Refs 1. String type Re...

Detailed explanation of vue-router 4 usage examples

Table of contents 1. Install and create an instan...

Implementation of vite+vue3.0+ts+element-plus to quickly build a project

Table of contents vite function Use Environment B...

Summary of tips for setting the maximum number of connections in MySQL

Method 1: Command line modification We only need ...