Skip to content

Commit 53b01f7

Browse files
authored
docs(react, vue): add vite pwa docs (#2841)
1 parent d08d138 commit 53b01f7

File tree

3 files changed

+102
-23
lines changed

3 files changed

+102
-23
lines changed

docs/react/pwa.md

+52-12
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,69 @@ sidebar_label: Progressive Web Apps
77
<title>Create Progressive Web Apps (PWA) in React - Ionic Framework</title>
88
<meta
99
name="description"
10-
content="Create progressive web apps in React with Ionic. Read our React PWA documentation for information on how to make React PWAs using the Ionic CLI."
10+
content="Create progressive web apps in React with Ionic. Read our React PWA documentation for information on how to make React PWAs."
1111
/>
1212
</head>
1313

14-
## Making your React app a PWA
14+
## Making your React app a PWA with Vite
1515

16-
The two main requirements of a PWA are a <a href="https://developers.google.com/web/fundamentals/primers/service-workers/" target="_blank">Service Worker</a> and a <a href="https://developers.google.com/web/fundamentals/web-app-manifest/" target="_blank">Web Manifest</a>. While it's possible to add both of these to an app manually, a base project from Create React App (CRA) and the Ionic CLI provides this already.
16+
The two main requirements of a PWA are a <a href="https://developers.google.com/web/fundamentals/primers/service-workers/" target="_blank">Service Worker</a> and a <a href="https://developers.google.com/web/fundamentals/web-app-manifest/" target="_blank">Web Application Manifest</a>. While it's possible to add both of these to an app manually, we recommend using the [Vite PWA Plugin](https://vite-pwa-org.netlify.app/) instead.
17+
18+
To get started, install the `vite-plugin-pwa` package:
19+
20+
```shell
21+
npm install -D vite-plugin-pwa
22+
```
23+
24+
Next, update your `vite.config.js` or `vite.config.ts` file and add `vite-plugin-pwa`:
25+
26+
```javascript
27+
import { defineConfig } from 'vite'
28+
import react from '@vitejs/plugin-react'
29+
import { VitePWA } from 'vite-plugin-pwa'
30+
31+
export default defineConfig({
32+
plugins: [
33+
react(),
34+
VitePWA({ registerType: 'autoUpdate' })
35+
],
36+
})
37+
```
38+
39+
This minimal configuration allows your application to generate the Web Application Manifest and Service Worker on build.
40+
41+
For more information on configuring the Vite PWA Plugin, see the [Vite PWA "Getting Started" Guide](https://vite-pwa-org.netlify.app/guide/).
42+
43+
See the [Vite PWA "Deploy" Guide](https://vite-pwa-org.netlify.app/deployment/) for information on how to deploy your PWA.
44+
45+
## Making your React app a PWA with Create React App
46+
47+
:::note
48+
As of Ionic CLI v7, Ionic React starter apps ship with Vite instead of Create React App. See [Making your React app a PWA with Vite](#making-your-react-app-a-pwa-with-vite) for Vite instructions.
49+
:::
50+
51+
The two main requirements of a PWA are a <a href="https://developers.google.com/web/fundamentals/primers/service-workers/" target="_blank">Service Worker</a> and a <a href="https://developers.google.com/web/fundamentals/web-app-manifest/" target="_blank">Web Application Manifest</a>. While it's possible to add both of these to an app manually, a base project from Create React App (CRA) and the Ionic CLI provides this already.
1752

1853
In the `index.ts` for your app, there is a call to a `serviceWorker.unregister()` function. The base that CRA provides has service workers as an opt-in feature, so it must be enabled. To enable, call `serviceWorker.register()`.
1954

2055
```ts
2156
import React from 'react';
22-
import ReactDOM from 'react-dom';
57+
import { createRoot } from 'react-dom/client';
2358
import App from './App';
24-
import * as serviceWorker from './serviceWorker';
59+
import * as serviceWorkerRegistration from './serviceWorkerRegistration';
2560

26-
ReactDOM.render(<App />, document.getElementById('root'));
61+
const container = document.getElementById('root');
62+
const root = createRoot(container!);
63+
root.render(
64+
<React.StrictMode>
65+
<App />
66+
</React.StrictMode>
67+
);
2768

2869
// If you want your app to work offline and load faster, you can change
2970
// unregister() to register() below. Note this comes with some pitfalls.
30-
// Learn more about service workers: https://bit.ly/CRA-PWA
31-
// serviceWorker.unregister();
32-
serviceWorker.register();
71+
// Learn more about service workers: https://cra.link/PWA
72+
serviceWorkerRegistration.register();
3373
```
3474

3575
Once this package has been added, run `ionic build` and the `build` directory will be ready to deploy as a PWA.
@@ -42,15 +82,15 @@ By default, react apps package comes with the Ionic logo for the app icons. Be s
4282
Features like Service Workers and many JavaScript APIs (such as geolocation) require the app to be hosted in a secure context. When deploying an app through a hosting service, be aware that HTTPS will be required to take full advantage of Service Workers.
4383
:::
4484

45-
## Service Worker configuration
85+
### Service Worker configuration
4686

4787
By default, CRA/React Scripts come with a preconfigured Service Worker setup based on [Workbox's Webpack plugin](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin). This utilises a cache-first strategy, meaning that your app will load from a cache, even if the network returns a newer version of the app.
4888

4989
Because of the nature of CRA/React Scripts, the configuration for this is internal to React Scripts, meaning that it cannot be customized without ejecting from React Scripts. Currently, the Ionic CLI does not support an ejected React App, so if this action is taken, you'll need to use npm/yarn scripts instead of the Ionic CLI.
5090

51-
## Deploying
91+
### Deploying
5292

53-
### Firebase
93+
#### Firebase
5494

5595
Firebase hosting provides many benefits for Progressive Web Apps, including fast response times thanks to CDNs, HTTPS enabled by default, and support for [HTTP2 push](https://firebase.googleblog.com/2016/09/http2-comes-to-firebase-hosting.html).
5696

docs/vue/pwa.md

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
title: Progressive Web Apps in Vue
23
sidebar_label: Progressive Web Apps
34
---
45

@@ -10,11 +11,44 @@ sidebar_label: Progressive Web Apps
1011
/>
1112
</head>
1213

13-
# Progressive Web Apps in Vue
14+
## Making your Vue app a PWA with Vite
1415

15-
## Making your Vue app a PWA
16+
The two main requirements of a PWA are a <a href="https://developers.google.com/web/fundamentals/primers/service-workers/" target="_blank">Service Worker</a> and a <a href="https://developers.google.com/web/fundamentals/web-app-manifest/" target="_blank">Web Application Manifest</a>. While it's possible to add both of these to an app manually, we recommend using the [Vite PWA Plugin](https://vite-pwa-org.netlify.app/) instead.
1617

17-
The two main requirements of a PWA are a <a href="https://developers.google.com/web/fundamentals/primers/service-workers/" target="_blank">Service Worker</a> and a <a href="https://developers.google.com/web/fundamentals/web-app-manifest/" target="_blank">Web Manifest</a>. While it's possible to add both of these to an app manually, the Vue CLI has some utilities for adding this for you.
18+
To get started, install the `vite-plugin-pwa` package:
19+
20+
```shell
21+
npm install -D vite-plugin-pwa
22+
```
23+
24+
Next, update your `vite.config.js` or `vite.config.ts` file and add `vite-plugin-pwa`:
25+
26+
```javascript
27+
import { defineConfig } from 'vite'
28+
import vue from '@vitejs/plugin-vue'
29+
import { VitePWA } from 'vite-plugin-pwa'
30+
31+
export default defineConfig({
32+
plugins: [
33+
vue(),
34+
VitePWA({ registerType: 'autoUpdate' })
35+
],
36+
})
37+
```
38+
39+
This minimal configuration allows your application to generate the Web Application Manifest and Service Worker on build.
40+
41+
For more information on configuring the Vite PWA Plugin, see the [Vite PWA "Getting Started" Guide](https://vite-pwa-org.netlify.app/guide/).
42+
43+
See the [Vite PWA "Deploy" Guide](https://vite-pwa-org.netlify.app/deployment/) for information on how to deploy your PWA.
44+
45+
## Making your Vue app a PWA with Vue CLI
46+
47+
:::note
48+
As of Ionic CLI v7, Ionic Vue starter apps ship with Vite instead of Vue CLI. See [Making your Vue app a PWA with Vite](#making-your-vue-app-a-pwa-with-vite) for Vite instructions.
49+
:::
50+
51+
The two main requirements of a PWA are a <a href="https://developers.google.com/web/fundamentals/primers/service-workers/" target="_blank">Service Worker</a> and a <a href="https://developers.google.com/web/fundamentals/web-app-manifest/" target="_blank">Web Application Manifest</a>. While it's possible to add both of these to an app manually, the Vue CLI has some utilities for adding this for you.
1852

1953
For existing projects, you can run the `vue add` command to install the PWA plugin for Vue.
2054

@@ -117,11 +151,11 @@ In addition to the service worker, the Vue PWA plugin also is responsible for cr
117151

118152
Be sure to update the icons in `public/img/icons` to match your own brand. If you wanted to customize the theme color or name, be sure to read the [PWA plugin docs](https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa#configuration) on GitHub.
119153

120-
## Deploying
154+
### Deploying
121155

122156
You can use various hosts like Firebase, Vercel, Netlify, or even Azure Static Web Apps. All will have similar setup processes that need to be completed. For this guide, Firebase will be used as the hosting example. In addition to this guide, the [Vue CLI docs](https://cli.vuejs.org/guide/deployment.html) also have a guide on how to deploy to various providers.
123157

124-
### Firebase
158+
#### Firebase
125159

126160
Firebase hosting provides many benefits for Progressive Web Apps, including fast response times thanks to CDNs, HTTPS enabled by default, and support for [HTTP2 push](https://firebase.googleblog.com/2016/09/http2-comes-to-firebase-hosting.html).
127161

versioned_docs/version-v6/react/pwa.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ In the `index.ts` for your app, there is a call to a `serviceWorker.unregister()
1919

2020
```ts
2121
import React from 'react';
22-
import ReactDOM from 'react-dom';
22+
import { createRoot } from 'react-dom/client';
2323
import App from './App';
24-
import * as serviceWorker from './serviceWorker';
24+
import * as serviceWorkerRegistration from './serviceWorkerRegistration';
2525

26-
ReactDOM.render(<App />, document.getElementById('root'));
26+
const container = document.getElementById('root');
27+
const root = createRoot(container!);
28+
root.render(
29+
<React.StrictMode>
30+
<App />
31+
</React.StrictMode>
32+
);
2733

2834
// If you want your app to work offline and load faster, you can change
2935
// unregister() to register() below. Note this comes with some pitfalls.
30-
// Learn more about service workers: https://bit.ly/CRA-PWA
31-
// serviceWorker.unregister();
32-
serviceWorker.register();
36+
// Learn more about service workers: https://cra.link/PWA
37+
serviceWorkerRegistration.register();
3338
```
3439

3540
Once this package has been added, run `ionic build` and the `build` directory will be ready to deploy as a PWA.

0 commit comments

Comments
 (0)