#webapplication
#webdevelopment
#progressivewebapp

How to convert a website into PWA

Anonymous

AnonymousOct 17, 2023

How to convert a website into PWA

A Progressive Web App (PWA) is a website that works like a mobile app, giving you a similar experience. It can be used offline, receive notifications, and be installed on your device's home screen for quick access.

Steps to converting a website into PWA:

1. Create a index.html file.

Note: If you already have an Website then follow step from 2.

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>Welcome to the Progressive Web App</title>
</head>

<body>
    <h1>Hi, Welcome to the Progressive Web App..!</h1>
</body>

</html>

2. Create a manifest.json file in root folder of project.

The manifest file defines how your PWA appears when installed on a user's device. It contains information like the app name, icons, and the starting URL. Here's an example:

{
  "name": "PWA-App",
  "short_name": "PWA",
  "icons": [
    {
      "src": "./logo.svg",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "./logo.svg",
      "type": "image/png",
      "sizes": "435x435"
    },
    {
      "src": "./logo.svg",
      "type": "image/png",
      "sizes": "512x512"
    }
  ],
  "start_url": ".",
  "display": "standalone",
  "theme_color": "#000000",
  "background_color": "#ffffff"
}

Note: Use .svg icons instead of png its better.

3. Link the manifest.json file in the <head> tag of your HTML document.

<link rel="manifest" href="./manifest.json">

4. Create a service_worker.js file in root folder of project.

Service workers are a crucial part of PWAs. They enable caching and offline capabilities. Here's a simple service worker:

const CACHE_NAME = 'v1'
const urlsToCache = ['/']
const self = this
// Install SW
self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => {
      return cache.addAll(urlsToCache)
    })
  )
})
// Listen for requests
self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request).then(() => {
      return fetch(event.request).catch(() => caches.match(urlsToCache))
    })
  )
})

// Activate the SW
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)
          }
        })
      )
    )
  )
})

5. To load the service worker, Add this script at the end of your <body> tag.

  <script>
        if ('serviceWorker' in navigator) {
            window.addEventListener('load', () => {
                navigator.serviceWorker.register('/service_worker.js')
                    .then((reg) => console.log('Success: ', reg.scope))
                    .catch((err) => console.log('Failure: ', err));
            })
        }
    </script>

So finally our index.html file looks like:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
    <meta charset="utf-8">
    <title>Welcome to the Progressive Web App</title>
    <link rel="manifest" href="manifest.json">
</head>

<body>
    <h1>Hi, Welcome to the Progressive Web App..!</h1>
     <script>
        if ('serviceWorker' in navigator) {
            window.addEventListener('load', () => {
                navigator.serviceWorker.register('/service_worker.js')
                    .then((reg) => console.log('Success: ', reg.scope))
                    .catch((err) => console.log('Failure: ', err));
            })
        }
    </script>
</body>

</html>

Ouput:

You can see installation icon on the right side in the search bar:

You can install the application by clicking on the Install button and this will be installed in our system. You can access it using the app icon/logo.

PWA application will looks like:

Happy Coding! ❤️