Steps to Create a React App

Step 1: Create a React application using the following command:

npx create-react-app pwa-application

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd pwa-application

Step 3: Install the required dependencies in your project using the following command:

npm install axios 

Project Structure:

Project Structure:

The updated dependencies in your packge.json file is:

"dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "axios": "^1.6.7",
},
CSS
/* App.css */

.app-container {
    padding: 20px;
}

.movie-list {
    list-style: none;
    padding: 0;
}

.movie-item {
    margin-bottom: 20px;
    display: flex;
    align-items: center;
}

.movie-poster {
    margin-right: 10px;
    width: 100px;
    /* Set a fixed width for the poster images */
    height: auto;
    /* Maintain aspect ratio */
}
JavaScript
//App.js

import React, { useEffect, useState } from 'react';
import axios from 'axios';
import './App.css';

const API_KEY = 'paste your api key here';
const MOVIE_API_URL = `https://api.themoviedb.org/3/movie/popular?api_key=${API_KEY}&language=en-US&page=1`;
const BASE_IMAGE_URL = 'https://image.tmdb.org/t/p/w200';

function App() {
    const [movies, setMovies] = useState([]);

    useEffect(() => {
        axios.get(MOVIE_API_URL)
            .then((response) => {
                setMovies(response.data.results);
            })
            .catch((error) => {
                console.error('Error fetching the movies:', error);
            });
    }, []);

    return (
        <div className="app-container">
            <h1>Popular Movies</h1>
            <ul className="movie-list">
                {movies.map((movie) => (
                    <li key={movie.id} className="movie-item">
                        <img
                            src={`${BASE_IMAGE_URL}${movie.poster_path}`}
                            alt={movie.title}
                            className="movie-poster"
                        />
                        <strong>{movie.title}</strong>
                        (Release Date: {movie.release_date})
                    </li>
                ))}
            </ul>
        </div>
    );
}

export default App;
JavaScript
// serviceWorker.js

//STORAGE OF BROWSER
const CACHE_NAME = "version-1";
const urlsToCache = ["index.html", "offline.html"];
const self = this;

//installation
self.addEventListener("install", (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => {
      console.log("Opened cache");

      return cache.addAll(urlsToCache);
    })
  );
});

// listen for request
self.addEventListener("fetch", (event) => {
  event.respondWith(
    caches.match(event.request).then((res) => {
      return fetch(event.request).catch(() => caches.match("offline.html"));
    })
  );
});

// actitivate the service worker
self.addEventListener("activate", (event) => {
    const cacheWhitelist = [];
    cacheWhitelist.push(CACHE_NAME);
    event.waitUntil(
        caches.keys().then((cacheNames) => Promise.all(
            cacheNames.map((cacheName) => {
                if(!cacheWhitelist.includes(cacheName)){
                    return caches.delete(cacheName);
                }
            })
        ))
    )
});

Start your application using the following command:

npm start

Output:

Final output

How do you Make React App a PWA ?

Progressive Web Apps (PWAs) have emerged as a game-changer, offering a blend of website accessibility and native mobile app capabilities, thereby presenting the best of both worlds.

Table of Content

  • What is a Progressive Web App (PWA)?
  • Key Features of PWA
  • Steps to Transform Your React App into a PWA
  • Get TMDb API Key
  • Enable HTTPS
  • Add a Web App Manifest (WAM): (manifest.json)
  • Register a Service Worker:(optional, but recommended for core PWA features)
  • Implementing Offline Functionality
  • Register the Service Worker (index.js)
  • Enabling Push Notifications (Optional)
  • Conclusion

Similar Reads

What is a Progressive Web App (PWA)?

Progressive Web Apps (PWAs) are web applications that offer a native app-like experience on the web. They are reliable, fast, and engaging, offering features such as offline access, push notifications, and home screen installation. In this article, we’ll explore how to make a React application a PWA....

Key Features of PWA

Offline capability: Works even without a network connection.App-like experience: Offers native-like navigation and interaction.Home screen installation: Users can install the app on their devices.Push notifications: Allows for re-engagement with users....

Steps to Transform Your React App into a PWA

We will Create a simple React-based web application that fetches a list of popular movies from The Movie Database (TMDb) API and displays them in a user-friendly format. The app demonstrates basic concepts in React, including state management, component rendering, and API integration....

Get TMDb API Key

Follow these steps to get an API key from TMDb:...

Enable HTTPS

Security is crucial for PWAs. Configure your web server to serve your app over HTTPs. Let’s assume you have a mechanism in place (like a free SSL certificate from Let’s Encrypt) to handle this....

Add a Web App Manifest (WAM): (manifest.json)

The WAM is a JSON file that provides essential information about your PWA, including its name, icons, theme color, and startup screen. Create a file named manifest.json at the root of your project directory and add the following content, customizing it for your app:...

Register a Service Worker:(optional, but recommended for core PWA features)

A service worker is a script that runs in the background, enabling functionalities like offline access, push notifications, and background sync. Create a file named serviceWorker.js in your public directory and add the following code:...

Implementing Offline Functionality

Service Worker Caching: Utilize the Service Worker Caching API to cache static assets like HTML, CSS, JavaScript, and images....

Register the Service Worker (index.js)

In your src/index.js file, import and register the service worker:...

Enabling Push Notifications (Optional)

Push notifications are a powerful way to re-engage users and keep them informed even when they’re not actively using your app. By implementing push notifications in your React PWA with Firebase Cloud Messaging (FCM), you can:...

Steps to Create a React App

Step 1: Create a React application using the following command:...

Conclusion

By following these steps, you’ve converted the “Movie List Application” into a Progressive Web App. This PWA has a service worker for offline support, a manifest file for metadata and icons, and HTTPS for security. It’s installable on users’ devices and works even when offline, providing a more engaging user experience....