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

Horizontal header menu implemented with CSS3

Result:Implementation Code html <nav class=&qu...

MySQL performance optimization: how to use indexes efficiently and correctly

Practice is the only way to test the truth. This ...

How to migrate the data directory in Docker

Table of contents View Disk Usage Disk Cleanup (D...

20 Signposts on the Road to Becoming an Excellent UI (User Interface) Designer

Introduction: Interface designer Joshua Porter pub...

MySql quick insert tens of millions of large data examples

In the field of data analysis, database is our go...

Nginx forwarding based on URL parameters

Use scenarios: The jump path needs to be dynamica...

Unicode signature BOM detailed description

Unicode Signature BOM - What is the BOM? BOM is th...

Native js realizes the drag and drop of the nine-square grid

Use native JS to write a nine-square grid to achi...

Some ways to eliminate duplicate rows in MySQL

SQL statement /* Some methods of eliminating dupl...

Build Maven projects faster in Docker

Table of contents I. Overview 2. Conventional mul...

Vue3 navigation bar component encapsulation implementation method

Encapsulate a navigation bar component in Vue3, a...

Do you know how to use Vue to take screenshots of web pages?

Table of contents 1. Install html2Canvas 2. Intro...