Building a Progressive Web App with React and Vite

Building a Progressive Web App with React and Vite

Introduction

Progressive Web Apps (PWAs) combine the best of web and mobile applications, offering a native app-like experience through web browsers. In this guide, we'll walk through creating a basic PWA using React and Vite, making your web application installable on both desktop and mobile devices.

Step 1: Creating a New Vite Project

Start by creating a new Vite project using yarn/npm:

yarn create vite

Follow the CLI prompts to:

  • Choose a project name

  • Select React as your framework

  • Choose your preferred variant (JavaScript/TypeScript)

Step 2: Setting Up PWA Support

Install the necessary PWA plugin:

yarn add vite-plugin-pwa

Step 3: Generating PWA Icons

  • Visit a favicon.io

  • Find the icon upload section on the website

  • Drag and drop your application's icon into the generator

  • Download the generated icon package

  • Extract the downloaded folder

  • You should receive the following files:

  • Create a public folder in your project root if it doesn't exist

  • Copy all the generated icon files into the public folder

Step 4: Configuring Vite for PWA

Create or update your vite.config.js file with the following configuration:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { VitePWA } from "vite-plugin-pwa";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    VitePWA({
      registerType: "autoUpdate",
      devOptions: {
        enabled: true,
      },
      includeAssets: ["favicon.ico", "apple-touch-icon.png", "masked-icon.svg"],
      manifest: {
        name: "App Name",
        short_name: "Short App Name",
        description: "App Description",
        theme_color: "#ffffff",
        icons: [
          {
            src: "android-chrome-192x192.png",
            sizes: "192x192",
            type: "image/png",
            purpose: "any",
          },
          {
            src: "android-chrome-512x512.png",
            sizes: "512x512",
            type: "image/png",
            purpose: "maskable",
          },
        ],
      },
    }),
  ],
});

Step 5: Deployment and Testing

  • Deploy your application to a hosting service (e.g., Vercel)

  • Access your deployed site through a web browser

  • You should see an install option in the browser's address bar or menu

Step 6: Converting to Mobile Apps

To create native mobile applications from your PWA:

  1. Visit a PWA Builder

  2. Enter your deployed PWA URL in the input field

  3. Click on "Start" to begin the conversion process

  4. Wait for the processing to complete

  5. Click on "Package for Stores" button

  6. For iOS:

    • Download the generated source code

    • Use Xcode to build the application locally

  7. For Android:

    • Download the APK file directly for immediate use

    • Optionally, download the source code for customization

Conclusion

You've now created a basic Progressive Web App using React and Vite! This foundation can be enhanced with additional PWA features like push notifications, offline support, and background sync. The combination of React, Vite, and PWA capabilities provides a powerful starting point for building modern, installable web applications.