PWA的学习之路
PWA Overview
What is PWA?
MDN explains
PWA(Progressive Web App) leverages modernWeb APIand traditional progressive enhancement strategies to create cross-platformWebapplications. These applications are ubiquitous and feature-rich, offering the same user experience advantages as native applications.
- A new method to enhance
WebAppexperience, providing users the same native app experience - Creating cross-platform
Webdesktop applications
Summary: PWA achieves this because it doesn't refer to a specific technology, but rather adds Manifest configuration files and Service Worker along with caching, allowing existing applications to have install and offline functionality.
PWA Advantages
- Progressive
- Adding
PWAfunctionality on top of the original application without affecting it - Installable/ native experience
- Can be added to desktop as a native
App, with startup animations and hidden address bars - Offline functionality
- Implemented via
Service Workerand cacheapito enable offline access to some features - Push notifications
- Click on push messages to quickly open the app, promoting user engagement
This demonstrates the mobile Demo running effect

PWA Technical Points
The source code can be downloaded here.
A standard PWA application should have the following 3 characteristics:
httpsaccess orlocalhostService WorkerManifest
Manifest
manifest.json allows the application to add a desktop icon. Configuration file content includes application name, icon, startup screen, theme color, and application style.
Compared to traditional WebApp entry points (URL, bookmark, bookmark), PWA applications have a more direct and convenient entry point.
- Add desktop icon
- Add startup animation to avoid white screen
- Hide default browser UI to look more like a native app
Import manifest.json file in index.html
<link rel="manifest" href="/manifest.json">
Common configuration information in manifest.json:
{
"name": "pwa-basic",
"short_name": "PWA Basic",
"start_url": "/index.html",
"icons":[
{
"src": "/images/icon128.png",
"sizes": "128x128",
"type":"image/png"
},
{
"src": "/images/icon144.png",
"sizes": "144x144",
"type":"image/png"
}
],
"description": "This is a basic PWA demo",
"background_color": "skyblue",
"theme_color": "black",
"display": "standalone"
}
-
nameis the application name, displayed when the app is installed, and shown below the startup screen -
short_nameis the displayed name when the application name is too long -
start_urlis the entry URL when the desktop icon is clicked -
iconsare the desktop icons, displayed in the center of the startup screen description-
background_coloris the background color of the startup screen -
theme_coloris the theme color -
display-
fullscreendisplays the application full screen, using all available display areas and excluding the status bar -
standaloneexcludes the address bar, with the status bar visible -
minimal-uihas the status bar and the browser address bar
-
More configuration options can be found here.
Cache API
Service Worker
What is a Service Worker?
MDN explains
Service workersact as a proxy server betweenWebapplications, browsers, and the network (available when online). ThisAPIaims to create an effective offline experience, intercepting network requests within the scope and taking appropriate actions based on whether the network is available.
Functions of Service Worker (in addition to Web Worker functionality)
- Installed successfully, running in the browser background, intercepting all
httprequests in the scope - Using
Cache APIto control offline content (controlled by developers) - Must use
https. Local development environments can uselocalhostinstead
Service Worker compatibility: 94% of browsers support it

Usage of Service Worker
Below is referred to as SW
SW is independent of the lifecycle of web pages. It is divided into 5 steps: registration, installation (success/failure), activation, running, and destruction. The following diagram shows this:

SW Usage
Installation
npm install -D vite-plugin-pwa
vite.config.js configuration
import { VitePWA } from 'vite-plugin-pwa'
export default {
plugins: [
VitePWA({
mode: 'development',
workbox: {
// 省略
}
})
]
}
More details on vite-plugin-pwa plugins
Webpack Projects
Vue Projects
A new Vue project can choose to support PWA as shown in the image

If not, add the @vue/cli plugin to enable PWA support. Run vue add pwa to add support for PWA.
See the configuration documentation for details.
React Projects
New React project using cra scaffolding with PWA template, as follows:
create-react-app my-app --template cra-template-pwa
Old projects can add webpack configuration and manifest.json to register sw.js manually.
Vite Projects
Installation
npm install -D vite-plugin-pwa
vite.config.js configuration
import { VitePWA } from 'vite-plugin-pwa'
export default {
plugins: [
VitePWA({
mode: 'development',
workbox: {
// 省略
}
})
]
}
More details on vite-plugin-pwa plugins
