Claude Code के साथ Develop a PWA (Progressive Web App) कैसे करें
Claude Code का उपयोग करके develop a pwa (progressive web app) सीखें। Practical code examples और step-by-step guidance शामिल है।
PWA क्या है
PWA(Progressive Web App)はWebの技術でネイティブアプリに近い体験を提供するアプローチ है।Claude Code का उपयोग करके、Service Workerやマニフェストのsettingsをefficientlyimplementationでき है।basic use करने का तरीकाはClaude Codeintroduction guideをदेखें。
Web App Manifest
{
"name": "My PWA Application",
"short_name": "MyPWA",
"description": "fastで信頼性の高いWebapplication",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#3b82f6",
"orientation": "portrait-primary",
"icons": [
{
"src": "/icons/icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/icons/icon-512.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "/icons/icon-maskable-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
]
}
Service Worker のimplementation
// sw.ts
const CACHE_NAME = "app-cache-v1";
const STATIC_ASSETS = [
"/",
"/offline.html",
"/styles/main.css",
"/scripts/app.js",
"/icons/icon-192.png",
];
// Cache static assets on install
self.addEventListener("install", (event: ExtendableEvent) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(STATIC_ASSETS);
})
);
// Activate immediately
(self as any).skipWaiting();
});
// Delete old caches
self.addEventListener("activate", (event: ExtendableEvent) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key))
);
})
);
(self as any).clients.claim();
});
cache戦略
// Stale While Revalidate 戦略
self.addEventListener("fetch", (event: FetchEvent) => {
const { request } = event;
const url = new URL(request.url);
// API request: Network First
if (url.pathname.startsWith("/api/")) {
event.respondWith(networkFirst(request));
return;
}
// 静的アセット: Cache First
if (request.destination === "image" || url.pathname.match(/\.(css|js)$/)) {
event.respondWith(cacheFirst(request));
return;
}
// HTMLpage: Stale While Revalidate
event.respondWith(staleWhileRevalidate(request));
});
async function networkFirst(request: Request): Promise<Response> {
try {
const response = await fetch(request);
const cache = await caches.open(CACHE_NAME);
cache.put(request, response.clone());
return response;
} catch {
const cached = await caches.match(request);
return cached || new Response('{"error": "offline"}', {
status: 503,
headers: { "Content-Type": "application/json" },
});
}
}
async function cacheFirst(request: Request): Promise<Response> {
const cached = await caches.match(request);
if (cached) return cached;
const response = await fetch(request);
const cache = await caches.open(CACHE_NAME);
cache.put(request, response.clone());
return response;
}
async function staleWhileRevalidate(
request: Request
): Promise<Response> {
const cache = await caches.open(CACHE_NAME);
const cached = await cache.match(request);
const fetchPromise = fetch(request).then((response) => {
cache.put(request, response.clone());
return response;
});
return cached || fetchPromise;
}
オフラインpage
// オフライン検出component
import { useState, useEffect } from "react";
function useOnlineStatus() {
const [isOnline, setIsOnline] = useState(navigator.onLine);
useEffect(() => {
const handleOnline = () => setIsOnline(true);
const handleOffline = () => setIsOnline(false);
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);
return () => {
window.removeEventListener("online", handleOnline);
window.removeEventListener("offline", handleOffline);
};
}, []);
return isOnline;
}
function OfflineBanner() {
const isOnline = useOnlineStatus();
if (isOnline) return null;
return (
<div className="bg-yellow-500 text-white text-center p-2">
オフライン है।一部のfeaturesが制限されてい है।
</div>
);
}
Service Worker の登録
// Service Worker登録
async function registerServiceWorker() {
if (!("serviceWorker" in navigator)) {
console.log("Service Worker not supported");
return;
}
try {
const registration = await navigator.serviceWorker.register(
"/sw.js",
{ scope: "/" }
);
// updatecheck
registration.addEventListener("updatefound", () => {
const newWorker = registration.installing;
newWorker?.addEventListener("statechange", () => {
if (
newWorker.state === "installed" &&
navigator.serviceWorker.controller
) {
// नयाversionが利用possible
showUpdateNotification();
}
});
});
console.log("Service Worker registered:", registration.scope);
} catch (error) {
console.error("Registration failed:", error);
}
}
Claude Code सेのimplementationプロンプト
PWAdevelopmentをClaude Code को requestする例 है।performanceके बारे मेंはcode分割・遅延読み込みも合わせてदेखें。
既存のReactアプリをPWA化して。
- Web App Manifestのcreate
- Service Workerでオフラインsupport
- 静的アセットはCache First、APIはNetwork First
- アプリupdate通知のimplementation
- Lighthouse PWAスコア100点を目指して
PWAके details के लिएweb.dev PWAガイドをदेखें。Claude Codeの最新情報はofficial documentationでconfirmでき है।
Summary
PWAはWebとネイティブのअच्छाところ取りができる技術 है।Claude Code का उपयोग करके、Service Workerのcache戦略 सेオフラインsupport तक、一貫したPWAimplementationをefficiently進められ है।
Related Posts
Claude Code से अपने Side Projects को Supercharge कैसे करें [Examples के साथ]
Claude Code से personal development projects को dramatically speed up करना सीखें। Real-world examples और idea से deployment तक practical workflow शामिल है।
Claude Code से Refactoring कैसे Automate करें
Claude Code से efficiently code refactoring automate करना सीखें। Real-world projects के लिए practical prompts और concrete refactoring patterns शामिल हैं।
Claude Code के साथ Complete CORS Configuration Guide
Claude Code का उपयोग करके complete CORS configuration guide सीखें। Practical tips और code examples शामिल हैं।