How to Build a Progressive Web App (PWA) from Scratch?
Last Updated :
21 Jan, 2025
A Progressive Web App (PWA) is a kind of application software sent through the web, formed using standard web technologies such as HTML, CSS, and JavaScript. PWAs are planned to work on any platform that uses a standards-compliant browser, containing both desktop and portable devices.
Steps to Build a Progressive Web App
Step 1: Setting Up Your Development Environment
Here are the primary tools you will want to set up your development environment:
- A code editor (such as Visual Studio Code, Atom, or Sublime Text)
- A local development server (such as Live Server extension in VS Code or Node.js with Express)
- A web browser (preferably Google Chrome or Firefox for their developer tools)
Step 1.1: Install Node.js and NPM:
Download and install Node.js. This will also install NPM. Verify the installation by running:
node -v
npm -v
Step 1.2: Initialize Your Project
Make a new project directory and initialize it with NPM:
mkdir my-pwa
cd my-pwa
npm init -y
Step 2: Building the User Interface(UI)
Start with the 'index.html' file. This will be the main entry point of your PWA. Here is an easy example:
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>My PWA</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h2>Welcome to GeeksforGeeks</h2>
<p>This is a simple Progressive Web App example.</p>
<script src="app.js"></script>
</body>
</html>
Step 3: Adding Styles
Make a basic 'styles.css' file in the CSS directory to style your app:
CSS
body {
font-family: Helvetica, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
h1 {
color: #333;
}
p {
color: #666;
}
Step 4: Use JavaScript to Show Your Information
Make a basic 'app.js' file:
JavaScript
console.log('Hello, GeeksforGeeks!');
Step 5: Create Web App Manifest
Make a 'manifest.json' file to characterize the app's metadata:
JavaScript
{
"name": "My PWA",
"short_name": "PWA",
"start_url": ".",
"display": "standalone",
"background_color": "#ffffff",
"description": "A simple Progressive Web App example.",
"icons": [
{
"src": "icon.png",
"sizes": "192x192",
"type": "image/png"
}
]
}
Step 6: Add the manifest in your HTML file:
<link rel="manifest" href="manifest.json">
Step 7: Create and Fetch Assets
The service worker is a script that your browser runs in the background, separate from a web page, to manage caching and handle fetch requests. Create 'sw.js' file:
JavaScript
self.addEventListener('install', event => {
event.waitUntil(
caches.open('v1').then(cache => {
return cache.addAll([
'/',
'/index.html' ,
'/styles.css' ,
'/app.js' ,
'/manifest.json' ,
'/icon.png'
]);
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(response => {
return response || fetch(event.request);
})
);
});
Step 7: Registering the Service Worker
In your 'app.js' file, register the service worker:
JavaScript
if ('serviceWorker' in navigator) {
window.addEventListener('load' , () => {
navigator.serviceWorker.register('/sw.js')
.then(registration => {
console.log('Service Worker registered with scope:' , registration.scope);
}).catch(error => {
console.log('Service Worker registration failed:' , error);
});
});
}
Step 8: Testing Your PWA
To test your PWA, you need to serve it over HTTPS. You can use basic Node.js server or any static file server. For development purposes, you can use 'HTTP-server' :
npm install -g http-server
http-server -p 8080
Navigate to http://localhost:8080 in your browser to see your PWA in action. Use Chrome DevTools to simulate different network conditions and test offline feature.
Step 9: Including Additional Features
Improve your PWA by adding push notifications, background sync, or improving performance using tools like Workbox, a set of libraries to simplify service worker development.
npm install workbox-cli --global
workbox generateSW
Conclusion
Making a Progressive Web App consists of creating a responsive web design, including a web app manifest, and executing a service worker for offline potentials and caching. By proceeding these steps, you can make a PWA that gives a stable and fascinating user experience. PWAs are a capable way to present high-quality, solid applications that work over all devices and network conditions.
Similar Reads
How to build progressive web app(PWA) in Angular 9 ?
In this article, we will develop a PWA (Progressive Web App) using Angular. What is PWA ? Progressive Web Apps (PWAs) are web applications that have been designed so that they are capable, reliable, and installable. PWA are built and enhanced with modern APIs to deliver enhanced capabilities, reliab
7 min read
How to develop a Progressive Web App using ReactJS ?
Progressive React Applications respond very fast to user actions. They load fast and are engaging just like a mobile app. They can access Mobile device features, leverage the Operating System, and have a very high reach. It enables Installability, Background Syncing, Caching and Offline Support, and
10 min read
How to Build a Great Product from Scratch?
All of us have great ideas, about which we think that if implemented, can turn into really successful products in the future. So this article will help you how to proceed with your idea and implement it to its full potential. First, think about what problem your idea solves. It may be that you want
7 min read
PWA (Progressive Web App) vs Native App
Progressive Web Apps (PWA) and Native Apps are two different design solutions when it comes to the world of mobile & web development. PWAs (Progressive Web Apps): Native apps that run in the web browser of your choice and deploy some native ones, a middle ground between full-on websites and devi
5 min read
General Introduction to Progressive Web Apps(PWA)
What is an App? Must be findable in App Store Icons must be present on Home Screen Touch and Gesture Controls Works Offline Receives push notifications Background processing Access to hardware features and sensors What is the Web ? Contains URLs and Links Markup and styling that must be readable by
2 min read
What is a Progressive Web App (PWA)?
A Progressive Web App (PWA) is a type of web application that uses modern web technologies to provide a user experience similar to that of a native mobile application. PWAs are designed to work seamlessly across various devices and browsers, offering a responsive and engaging user interface. They co
2 min read
Embracing the Evolution: The Rise of Progressive Web Apps (PWAs)
IntroductionIn the dynamic landscape of a virtual generation, the evolution of net applications has been now not whatever quick or excellent. From static HTML pages to dynamic internet reviews, the adventure has been marked by means of regular innovation and variant. Among the fashionable milestones
5 min read
How to Build Progressive Web Application and Submit it to the Play Store?
Progressive Web Apps(PWA's), are a type of application that is developed using web technologies and can be installed on any device like a traditional application. Creating a simple PWA is very easy as it involves adding two important files to the project which are manifest.json and serviceworker.js.
4 min read
How to Cache Ejs and Built the Progressive Web Application with Nodejs?
To cache EJS (Embedded JavaScript) templates and build a Progressive Web Application (PWA) with Node.js, we need to leverage several key technologies. Caching EJS templates can significantly enhance performance by reducing the need to recompile templates on each request. we'll learn how to cache EJS
4 min read
How to Build a JavaScript App with ChatGPT?
AI is one of the most overhyped techs right now in the market. It is said that it will soon replace Software Engineers in creating complex software applications. In this article, we will test ChatGPT to create a JavaScript application by giving ChatGPT the prompts and will see if it can actually mak
6 min read