React को PWA के रूप में बनाना
In this page:
{
"name": "App Name",
"short_name": "Short",
"start_url": "/",
"display": "standalone",
"theme_color": "#000000",
"icons": [
{ "src": "icon-192.png", "sizes": "192x192", "type": "image/png" }
]
}
Web App Manifest
manifest.json एक JSON file है जो आपके app का name, icons, theme color, और install होने पर यह कैसा दिखना चाहिए (जैसे fullscreen या standalone) describe करती है।
Browsers इस file का उपयोग यह decide करने के लिए करते हैं कि 'Add to Home Screen' offer करें या नहीं और installed app कैसा दिखे।
उदाहरण: manifest.json Content
$ cat public/manifest.json
{
"short_name": "MyApp",
"name": "My React App",
"icons": [
{ "src": "icon-192.png", "sizes": "192x192", "type": "image/png" },
{ "src": "icon-512.png", "sizes": "512x512", "type": "image/png" }
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
⚠️ Run this in your own terminal or Node.js environment.
Offline Support के लिए एक Service Worker जोड़ना
एक service worker एक script है जो आपके page से separately run होती है, network requests intercept करने और network unavailable होने पर cached responses serve करने में सक्षम।
यह वह है जो एक app को (at least partially) offline काम करते रहने देता है, एक true PWA की एक defining feature।
उदाहरण: Adding a Service Worker for Offline Support
// Run in your local React project (npm install required)
// vite.config.js (using vite-plugin-pwa)
import { VitePWA } from 'vite-plugin-pwa';
export default {
plugins: [VitePWA({ registerType: 'autoUpdate' })],
};
⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.
PWA Behavior Test करना
Chrome DevTools का Application tab आपको registered service worker inspect करने, offline mode simulate करने, और verify करने देता है कि manifest correctly recognized है — production में इस पर rely करने से पहले आपके PWA setup के actually काम करने की पुष्टि करने के लिए essential।
- manifest.json file भूल जाना, जो यह है जो एक browser को बिल्कुल 'Add to Home Screen' offer करने देती है।
- Offline behavior test न करना, मान लेना कि service worker 'just works' actually verify किए बिना कि cached content बिना connection के load होती है।
- Content को बहुत aggressively cache करना, users को एक नई deployment के लंबे समय बाद भी stale content दिखाने का कारण बनते हुए।
- एक Progressive Web App (PWA) एक web app है जो install हो सकता है और एक native app जैसा behave कर सकता है, offline support सहित।
- एक manifest.json file app का name, icons, और installation के लिए display mode describe करती है।
- एक service worker एक background script है जो assets cache कर सकती है और offline होने पर भी उन्हें serve कर सकती है।
- Vite और Create React App दोनों PWA plugins/templates offer करते हैं जो इसमें से ज़्यादातर automatically setup करते हैं।
Service workers और installability सभी modern browsers में supported हैं (historically older iOS Safari versions में notably limited support, हालांकि modern versions इसमें से ज़्यादातर support करते हैं)।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Why Performance Matters
- Optimizing with React.memo
- Avoiding Unnecessary Re-renders
- Using the React Profiler
- Analyzing Bundle Size
- Image Optimization Techniques
- React as a PWA
- Creating a Production Build
- Environment Variables in React
- Deploying to Netlify
- Deploying to Vercel
- Basic CI/CD for React Apps
- SEO Basics for React SPAs