← Back to React Course | Chapter 14: Performance & Production Deployment | Lesson 7 of 13

React को PWA के रूप में बनाना

एक React app को PWA में बदलना एक website को home screen पर भी रहने और एक real app जैसा काम करने के लिए passport देने जैसा है, even internet के बिना।
Syntax
markup
{
  "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 कैसा दिखे।

Note: Icons को कई sizes में provide करें (जैसे 192x192 और 512x512) क्योंकि अलग devices/contexts अलग icon resolutions request करते हैं।
Warning: आपके HTML से linked एक valid manifest.json के बिना, ज़्यादातर browsers बिल्कुल भी install/'Add to Home Screen' prompt offer नहीं करेंगे।

उदाहरण: manifest.json Content

bash
$ 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।

Note: Vite का official PWA plugin (vite-plugin-pwa) आपके लिए एक working service worker generate और register करता है, caching logic हाथ से लिखने के बजाय।
Warning: एक misconfigured service worker accidentally STALE cached content serve कर सकता है even आपके real updates deploy करने के बाद — cache invalidation को careful handling चाहिए।

उदाहरण: Adding a Service Worker for Offline Support

markup
// 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।

Note: DevTools के Network tab में Offline checkbox उपयोग करें एक lost connection simulate करने और verify करने के लिए कि आपका app फिर भी कुछ useful दिखाता है, कोई browser error page नहीं।
Warning: सिर्फ एक real, working connection के साथ testing आपके offline fallback behavior में bugs hide कर सकती है जो सिर्फ तब दिखते हैं जब network actually drop हो जाए।
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. manifest.json file भूल जाना, जो यह है जो एक browser को बिल्कुल 'Add to Home Screen' offer करने देती है।
  2. Offline behavior test न करना, मान लेना कि service worker 'just works' actually verify किए बिना कि cached content बिना connection के load होती है।
  3. 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 करते हैं)।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.