Progressive Web Apps in React
Making your React app a PWA with Vite
The two main requirements of a PWA are a Service Worker and a Web Application Manifest. While it's possible to add both of these to an app manually, we recommend using the Vite PWA Plugin instead.
To get started, install the vite-plugin-pwa
package:
npm install -D vite-plugin-pwa
Next, update your vite.config.js
or vite.config.ts
file and add vite-plugin-pwa
:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { VitePWA } from 'vite-plugin-pwa';
export default defineConfig({
plugins: [react(), VitePWA({ registerType: 'autoUpdate' })],
});
This minimal configuration allows your application to generate the Web Application Manifest and Service Worker on build.
For more information on configuring the Vite PWA Plugin, see the Vite PWA "Getting Started" Guide.
See the Vite PWA "Deploy" Guide for information on how to deploy your PWA.
Making your React app a PWA with Create React App
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 for Vite instructions.
The two main requirements of a PWA are a Service Worker and a Web Application Manifest. 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.
PWA の主な要件は、 Service Worker と Web Manifest の 2 つです。これらの両方を手動でアプリに追加することは可能ですが、Create React App (CRA)と Ionic CLI のベースプロジェクトがこれらをすでに提供しています。
アプリケーションの index.ts
には、serviceWorker.unregister()
関数の呼び出しがあります。基本 CRA が提供する service workers はオプトイン機能なので、有効にする必要があります。
有効にするには、serviceWorker.register ()
を呼び出します。
import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App';
import * as serviceWorkerRegistration from './serviceWorkerRegistration';
const container = document.getElementById('root');
const root = createRoot(container!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://cra.link/PWA
serviceWorkerRegistration.register();